-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglob.go
311 lines (261 loc) · 6.68 KB
/
glob.go
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package shared
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"github.com/bmatcuk/doublestar/v4"
"github.com/mattn/go-isatty"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
)
type globFn = func(ignorer ignoreFn, args []string) ([]FileContext, error)
//LATER: regex-based globber
//LATER: glob with https://github.com/gobwas/glob
var globFunctions = map[string]globFn{
"": doublestarExpander,
"doublestar": doublestarExpander,
"golang": golangExpander,
"none": noExpander,
}
// Globber is an optional min/max pair
type Globber struct {
value string `default:"doublestar"`
}
func (g *Globber) String() string {
return g.value
}
// Set the glob algorithm
func (g *Globber) Set(newValue string) error {
if globFunctions[newValue] == nil {
return fmt.Errorf("Invalid glob algorithm '%s'", newValue)
}
g.value = newValue
return nil
}
// Type is a description of range
func (g *Globber) Type() string {
return "Globber"
}
func homedirExpand(arg string) string {
expanded, err := homedir.Expand(arg) //LATER: switch to os.UserHomeDir()?
if err != nil {
if Debug {
fmt.Fprintf(os.Stderr, "DEBUG: error while expanding homedir %s\n", err.Error())
}
return arg
}
return expanded
}
func doublestarExpander(ignorer ignoreFn, args []string) ([]FileContext, error) {
fcs := []FileContext{}
for _, arg := range args {
basepath, pattern := doublestar.SplitPattern(homedirExpand(arg))
if Debug {
fmt.Fprintf(os.Stderr, "DEBUG: doublestar expanding %s at %s\n", pattern, basepath)
}
fsys := os.DirFS(basepath)
argfiles, dsErr := doublestar.Glob(fsys, pattern)
if dsErr != nil {
return nil, fmt.Errorf("Unable to expand %s at %s (doublestarExpander %w)", pattern, basepath, dsErr)
}
for _, argfile := range argfiles {
if ignoreDotFiles {
_, fileName := filepath.Split(argfile)
if fileName[0] == '.' {
if Debug {
fmt.Fprintf(os.Stderr, "DEBUG: skipping dotfile %s\n", argfile)
}
continue
}
}
if ignorer(argfile) == true {
continue
}
fullpath := filepath.Join(basepath, argfile)
fc := FileContext{
FilePath: fullpath,
}
fi, statErr := fc.Stat()
if statErr != nil {
return nil, fmt.Errorf("Unable to stat %s (doublestarExpander, %w)", arg, statErr)
}
if fi.IsDir() {
if Debug {
fmt.Fprintf(os.Stderr, "WARNING: doublestar returned a directory: %s\n", fullpath)
}
continue
}
ProgressCount()
fcs = append(fcs, fc)
}
}
return fcs, nil
}
func noExpander(ignorer ignoreFn, args []string) ([]FileContext, error) {
fcs := []FileContext{}
for _, arg := range args {
if ignorer(arg) == true {
continue
}
fc := FileContext{
FilePath: homedirExpand(arg),
}
fi, statErr := fc.Stat()
if statErr != nil {
return nil, fmt.Errorf("Unable to stat %s (noExpander, %w)", arg, statErr)
}
if fi.IsDir() {
//LATER: or recurse?
continue
}
ProgressCount()
fcs = append(fcs, fc)
}
return fcs, nil
}
func golangExpander(ignorer ignoreFn, args []string) ([]FileContext, error) {
fcs := []FileContext{}
for _, arg := range args {
argfiles, globErr := filepath.Glob(homedirExpand(arg))
if globErr != nil {
return nil, fmt.Errorf("Unable to glob %s (golangExpander, %w)", arg, globErr)
}
for _, argfile := range argfiles {
if ignoreDotFiles {
_, fileName := filepath.Split(argfile)
if fileName[0] == '.' {
if Debug {
fmt.Fprintf(os.Stderr, "DEBUG: skipping dotfile %s\n", argfile)
}
continue
}
}
if ignorer(argfile) == true {
continue
}
fc := FileContext{
FilePath: argfile,
}
fi, statErr := fc.Stat()
if statErr != nil {
//LATER
continue
}
if fi.IsDir() {
//LATER: or recurse?
continue
}
ProgressCount()
fcs = append(fcs, fc)
}
}
return fcs, nil
}
func MakeFileCommand(checkFn func(*FileContext)) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
total := 0
bad := 0
good := 0
if Debug {
fmt.Fprintf(os.Stderr, "DEBUG: %d args\n", len(args))
}
ignorer := loadIgnoreFile(ignoreFile)
fcs := []FileContext{}
useGlobber := true
if len(args) == 1 && args[0] == "-" {
useGlobber = false
if isatty.IsTerminal(os.Stdin.Fd()) {
return errors.New("Attempt to read stdin from terminal")
}
data, stdinReadErr := ioutil.ReadAll(os.Stdin) //LATER: does this work on Windows w/binary files
if stdinReadErr != nil {
return fmt.Errorf("unable to read stdin: %w", stdinReadErr)
}
fcs = append(fcs, FileContext{
FilePath: "stdin",
data: data,
})
} else if len(args) == 1 && args[0] == "@-" {
if isatty.IsTerminal(os.Stdin.Fd()) {
return errors.New("Attempt to read stdin from terminal")
}
scanner := bufio.NewScanner(os.Stdin)
args = args[:0]
for scanner.Scan() {
line := scanner.Text()
args = append(args, line)
}
if Debug {
fmt.Fprintf(os.Stderr, "DEBUG: %d lines read from stdin\n", len(args))
}
useGlobber = false
fcs, _ = noExpander(ignorer, args)
}
//LATER: handle @file, @-0 for names on stdin
if useGlobber {
fcs, _ = globFunctions[globber.String()](ignorer, args)
}
if len(fcs) == 0 {
return fmt.Errorf("No files to lint")
}
if Debug {
fmt.Fprintf(os.Stderr, "DEBUG: %d files after arg expansion\n", len(fcs))
}
sort.Slice(fcs[:], func(i, j int) bool {
return fcs[i].FilePath < fcs[j].FilePath
})
ProgressStart(fcs)
for _, fc := range fcs {
ProgressUpdate(good, bad, fc)
basicTests(&fc)
checkFn(&fc)
total++
success := fc.Success()
if success {
good++
} else {
bad++
}
if showFiles.String() != "none" {
if !success || showFiles.String() == "all" {
if OutputFormat.String() == "json" {
fileData := map[string]interface{}{
"file": fc.FilePath,
"success": success,
}
fmt.Printf("%s\n", EncodeJSON(fileData))
} else if OutputFormat.String() == "text" {
fmt.Printf("%s: %s\n", IfThenElse(success, "INFO", "ERROR"), fc.FilePath)
}
}
} else if OutputFormat.String() == "filenames" {
if !success {
fmt.Printf("%s\n", fc.FilePath)
}
}
}
ProgressUpdate(good, bad, FileContext{})
ProgressEnd()
if showTotal {
if OutputFormat.String() == "json" {
fmt.Printf("%s\n", EncodeJSON(map[string]interface{}{
"total": total,
"good": good,
"bad": bad,
}))
} else if OutputFormat.String() == "text" {
fmt.Printf("INFO: files tested: %d\n", total)
fmt.Printf("INFO: files good: %d\n", good)
fmt.Printf("INFO: files bad: %d\n", bad)
}
}
if bad > 0 {
return fmt.Errorf("%d errors found", bad)
}
return nil
}
}