-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
251 lines (225 loc) · 6.07 KB
/
main.go
File metadata and controls
251 lines (225 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package main
import (
"flag"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
furit "github.com/kemokemo/furit/lib"
"gopkg.in/yaml.v3"
)
// exitCode
const (
exitCodeOK = iota
exitCodeFoundUnreferencedImages
exitCodeInvalidArgs
exitCodeInternalOperation
exitCodeFailedToRemoveFiles
)
const (
usage = `Usage: furit [<option>...] <1st path> <2nd path>...
you can set multiple paths to search invalid images.`
flags = `-d, -delete:
delete unlinked image files (with confirmation)
-f, -force:
delete unlinked image files without prompting for confirmation
-s, -settings:
specify the settings file path to exclude files etc..
-t, -type:
specify the target text format (markdown, html are available)
-h, -help:
display help
-v, -version:
display version`
)
var (
out io.Writer = os.Stdout
outerr io.Writer = os.Stderr
exit = os.Exit
cmdArgs []string
)
// flags
var (
help bool
ver bool
delFlag bool
forceFlag bool
typeFlag string
settingsPath string
)
func init() {
testing.Init() // require Go 1.13 or later
flag.BoolVar(&help, "help", false, "display help")
flag.BoolVar(&help, "h", false, "display help")
flag.BoolVar(&ver, "version", false, "display version")
flag.BoolVar(&ver, "v", false, "display version")
flag.BoolVar(&delFlag, "delete", false, "delete unlinked image files")
flag.BoolVar(&delFlag, "d", false, "delete unlinked image files")
flag.BoolVar(&forceFlag, "force", false, "delete unlinked image files without prompting for confirmation")
flag.BoolVar(&forceFlag, "f", false, "delete unlinked image files without prompting for confirmation")
flag.StringVar(&typeFlag, "type", "markdown", "file type to check links")
flag.StringVar(&typeFlag, "t", "markdown", "file type to check links")
flag.StringVar(&settingsPath, "settings", "", "settings file path")
flag.StringVar(&settingsPath, "s", "", "settings file path")
flag.Parse()
cmdArgs = flag.Args()
}
func main() {
exit(run(cmdArgs))
}
func run(args []string) int {
if help {
_, err := fmt.Fprintf(out, "%s\n\n%s\n", usage, flags)
if err != nil {
fmt.Println("failed to output usage, ", err)
}
return exitCodeOK
}
if ver {
_, err := fmt.Fprintf(out, "%s version %s.%s\n", Name, Version, Revision)
if err != nil {
fmt.Println("failed to output version, ", err)
}
return exitCodeOK
}
if len(args) == 0 {
_, err := fmt.Fprintf(outerr, "path is empty. please set it.\n\n%v\n", usage)
if err != nil {
fmt.Println("failed to output warning, ", err)
}
return exitCodeInvalidArgs
}
linkFinder, err := getFinderByTypeFlag(typeFlag)
if err != nil {
_, err := fmt.Fprintf(outerr, "type error, %v\n", err)
if err != nil {
fmt.Println("failed to output type error, ", err)
}
return exitCodeInvalidArgs
}
var settings settings
if settingsPath != "" {
b, err := os.ReadFile(settingsPath)
if err != nil {
_, err := fmt.Fprintf(outerr, "failed to read settings error, %v\n", err)
if err != nil {
fmt.Println("failed to output reading settings error, ", err)
}
return exitCodeInvalidArgs
}
err = yaml.Unmarshal(b, &settings)
if err != nil {
_, err := fmt.Fprintf(outerr, "failed to unmarshal settings error, %v\n", err)
if err != nil {
fmt.Println("failed to output unmarshal settings error, ", err)
}
return exitCodeInvalidArgs
}
}
exitCode := exitCodeOK
for _, root := range args {
_, err := os.Lstat(root)
if err != nil {
_, err := fmt.Fprintf(outerr, "path is invalid: %v\n", err)
if err != nil {
fmt.Println("failed to output path invalid error, ", err)
}
exitCode = exitCodeInvalidArgs
continue
}
links, err := linkFinder.Find(root)
if err != nil {
_, err := fmt.Fprintf(outerr, "failed to find links: %v\n", err)
if err != nil {
fmt.Println("failed to output finding links error, ", err)
}
exitCode = exitCodeInternalOperation
continue
}
imgPaths, err := furit.Image.Find(root)
if err != nil {
_, err := fmt.Fprintf(outerr, "failed to find image paths: %v\n", err)
if err != nil {
fmt.Println("failed to output finding image paths error, ", err)
}
exitCode = exitCodeInternalOperation
continue
}
imgMap := make(map[string](bool), len(links))
for _, link := range links {
imgMap[link] = false
}
var delPaths []string
for _, imPath := range imgPaths {
_, ok := imgMap[imPath]
if ok {
continue
}
if isExcludePath(imPath, root, settings.Excludes) {
continue
}
delPaths = append(delPaths, imPath)
_, err := fmt.Fprintln(out, imPath)
if err != nil {
fmt.Println("failed to output image paths, ", err)
}
}
if len(delPaths) > 0 {
exitCode = exitCodeFoundUnreferencedImages
}
if !delFlag || len(delPaths) == 0 {
continue
}
if !forceFlag {
res, err := askForConfirmation("Are you sure to delete these unlinked images?", os.Stdin, out, 3)
if !res {
if err != nil {
_, err := fmt.Fprintf(outerr, "the file deletion process has been canceled: %s\n", err)
if err != nil {
fmt.Println("failed to output deletion cancel, ", err)
}
continue
} else {
_, err := fmt.Fprintln(outerr, "the file deletion process has been canceled by user input")
if err != nil {
fmt.Println("failed to output deletion cancel by user, ", err)
}
continue
}
}
}
for _, delPath := range delPaths {
e := os.Remove(delPath)
if e != nil {
_, err := fmt.Fprintf(outerr, "failed to remove file: %s\n", e)
if err != nil {
fmt.Println("failed to output removing file error, ", err)
}
exitCode = exitCodeFailedToRemoveFiles
}
}
}
return exitCode
}
func getFinderByTypeFlag(tf string) (furit.ImageLinkFinder, error) {
tf = strings.ToLower(tf)
switch tf {
case "markdown":
return furit.Markdown, nil
case "html":
return furit.HTML, nil
default:
return nil, fmt.Errorf("unknown type flag: %v", tf)
}
}
func isExcludePath(path string, root string, excludes []string) bool {
for _, ex := range excludes {
exPath := filepath.Join(root, ex)
if exPath == path {
return true
}
}
return false
}