-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
409 lines (330 loc) · 9.98 KB
/
main.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package main
import (
"bufio"
"encoding/json"
"errors"
"flag"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
colorize "github.com/fatih/color"
ignore "github.com/sabhiram/go-gitignore"
)
var (
ignoredColor = colorize.FgGreen
includedColor = colorize.FgHiRed
)
type Config struct {
Color bool
Debug bool
Excluded bool
Fullpath bool
Ignorefile string
Included bool
Invertcolors bool
Nocolor bool
Nofullpath bool
Verbose bool
}
func (c *Config) fillDefaults() {
c.Color = true
c.Fullpath = true
}
// main function is a wrapper on the realMain function and emits OS exit code based on wrapped function
func main() {
os.Exit(realMain())
}
func realMain() int {
var config Config
config.fillDefaults()
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
var verbose bool
flag.BoolVar(&verbose, "verbose", config.Verbose, "emit verbose output")
flag.BoolVar(&verbose, "v", config.Verbose, "emit verbose output")
var debug bool
flag.BoolVar(&debug, "debug", config.Debug, "emit debug messages")
var stdin bool
flag.BoolVar(&stdin, "stdin", false, "read from ignore file from STDIN")
flag.BoolVar(&stdin, "s", false, "read from ignore file from STDIN")
var color bool
flag.BoolVar(&color, "color", config.Color, "enable colors")
flag.BoolVar(&color, "c", config.Color, "enable colors")
var nocolor bool
flag.BoolVar(&nocolor, "nocolor", config.Nocolor, "disable use of colors")
flag.BoolVar(&nocolor, "n", config.Nocolor, "disable use of colors")
var ignorefile string
flag.StringVar(&ignorefile, "ignorefile", "", "a path to a specific ignore file")
flag.StringVar(&ignorefile, "i", "", "a path to a specific ignore file")
var excluded bool
flag.BoolVar(&excluded, "excluded", config.Excluded, "only output excluded files")
flag.BoolVar(&excluded, "x", config.Excluded, "only output excluded files")
var included bool
flag.BoolVar(&included, "included", config.Included, "only output included files")
var invertcolors bool
flag.BoolVar(&invertcolors, "invertcolors", config.Invertcolors, "inverts the used color")
var fullpath bool
flag.BoolVar(&fullpath, "fullpath", config.Fullpath, "emits files and directories with full path")
flag.BoolVar(&fullpath, "f", config.Fullpath, "emits files and directories with full path")
var nofullpath bool
flag.BoolVar(&nofullpath, "nofullpath", config.Nofullpath, "emits files and directories without full path")
nocolorEnv := os.Getenv("NO_COLOR")
flag.Parse()
globalConfigDir := os.Getenv("HOME") + "/.config"
XDGConfigHome := os.Getenv("XDG_CONFIG_HOME")
if XDGConfigHome != "" {
globalConfigDir = XDGConfigHome
}
stevedoreGlobalConfigDir := globalConfigDir + "/stevedore/"
globalConfigFile := stevedoreGlobalConfigDir + "config.json"
_, err := loadGlobalConfigFile(globalConfigFile, &config)
if err != nil {
fmt.Printf("Error attempting to read global configuration file: %s, continuing...\n", err)
}
configFile := ".stevedore.json"
_, err = loadLocalConfigFile(configFile, &config)
if err != nil {
fmt.Printf("Error attempting to read configuration file: %s, continuing...\n", err)
}
markFlags := func(f *flag.Flag) {
switch {
case f.Name == "debug":
config.Debug = true
case f.Name == "color" || f.Name == "c":
config.Color = true
config.Nocolor = false
case f.Name == "nocolor" || f.Name == "n":
config.Nocolor = true
config.Color = false
case f.Name == "ignorefile" || f.Name == "i":
config.Ignorefile = ignorefile
case f.Name == "excluded":
config.Excluded = true
case f.Name == "included":
config.Included = true
case f.Name == "invertcolors":
config.Invertcolors = true
case f.Name == "fullpath" || f.Name == "f":
config.Fullpath = true
config.Nofullpath = false
case f.Name == "nofullpath":
config.Nofullpath = true
config.Fullpath = false
case f.Name == "verbose" || f.Name == "v":
config.Verbose = true
default:
fmt.Printf("No special handling of this command line flag %s.\n", f.Name)
}
}
flag.Visit(markFlags)
path := flag.Arg(0)
if path == "" {
path = "."
}
var ignoreLines string
if stdin {
scanner := bufio.NewScanner(os.Stdin)
var lines []string
for scanner.Scan() {
// read line from stdin using newline as separator
line := scanner.Text()
// append the line to a slice
lines = append(lines, line)
}
ignoreLines = strings.Join(lines, "\n")
if config.Debug {
fmt.Println("path: ", path)
fmt.Println("ignore string from STDIN")
fmt.Printf("ignorelines: \n%s\n\n", ignoreLines)
}
} else if config.Ignorefile == "" {
config.Ignorefile = path + "/.dockerignore"
if config.Debug {
fmt.Println("path: ", path)
fmt.Println("ignoreFile: ", config.Ignorefile)
}
}
if config.Included && config.Excluded {
config.Included = false
config.Excluded = false
} else if !config.Included && !config.Excluded {
config.Included = true
config.Excluded = true
}
if config.Debug {
fmt.Println("CLI flags:")
fmt.Println("\tcolor: ", color)
fmt.Println("\tnocolor: ", nocolor)
fmt.Println("\tignorefile: ", ignorefile)
fmt.Println("\tdebug: ", debug)
fmt.Println("\tverbose: ", verbose)
fmt.Println("\texcluded: ", excluded)
fmt.Println("\tincluded: ", included)
fmt.Println("\tfullpath: ", fullpath)
fmt.Println("\tnofullpath: ", nofullpath)
fmt.Println("\ttail: ", flag.Args())
}
if config.Debug {
fmt.Println("Environment Variables")
fmt.Println("ENV: ", nocolorEnv)
}
var ignoreObject = ignore.CompileIgnoreLines([]string{}...)
if stdin {
ignoreObject = ignore.CompileIgnoreLines(ignoreLines)
} else {
ignoreObject, err = ignore.CompileIgnoreFile(config.Ignorefile)
if err != nil {
fmt.Printf("unable to read %s file\n", config.Ignorefile)
return 1
}
}
if config.Color {
config.Nocolor = false
}
if config.Nocolor || nocolorEnv != "" || nocolorEnv == "1" {
config.Color = false
config.Invertcolors = false
}
if config.Invertcolors {
ignoredColor, includedColor = includedColor, ignoredColor
}
if config.Nofullpath {
config.Fullpath = false
}
ownIgnoreFile := ".stevedoreignore"
ownIgnoreObject := ignore.CompileIgnoreLines([]string{}...)
if _, err := os.Stat(ownIgnoreFile); !errors.Is(err, fs.ErrNotExist) {
if config.Debug {
fmt.Println("stevedore ignorefile found")
}
ownIgnoreObject, err = ignore.CompileIgnoreFile(ownIgnoreFile)
if err != nil {
fmt.Printf("unable to read %s file, ignoring - %v\n", ownIgnoreFile, err)
ownIgnoreObject = ignore.CompileIgnoreLines([]string{}...)
}
} else if config.Debug {
fmt.Println("No stevedore ignorefile found")
}
err = filepath.Walk(path, func(path string, info fs.FileInfo, err error) error {
if err != nil {
fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", path, err)
return err
}
if ownIgnoreObject.MatchesPath(path) && info.IsDir() {
if config.Verbose {
fmt.Printf("%s have been ignored by stevedore, no traversal\n", info.Name())
}
return filepath.SkipDir
} else if ownIgnoreObject.MatchesPath(path) {
if config.Verbose {
fmt.Printf("%s have been ignored by stevedore\n", info.Name())
}
return nil
}
var entry string
if config.Fullpath {
entry = path
} else {
entry = info.Name()
}
if path == "." {
if config.Verbose {
fmt.Printf("%s is ignored, but traversed by stevedore by default\n", entry)
}
return nil
}
var aliasedPath = path
if info.IsDir() {
aliasedPath += "/"
}
if ignoreObject.MatchesPath(path) || ignoreObject.MatchesPath(aliasedPath) {
if config.Excluded {
if config.Color {
colorize.Set(ignoredColor)
}
if config.Verbose {
fmt.Printf("path %s ignored and is not included in Docker image\n", entry)
} else {
fmt.Printf("%s\n", entry)
}
colorize.Unset()
}
} else {
if config.Included {
if config.Color {
colorize.Set(includedColor)
}
if config.Verbose {
fmt.Printf("path %s not ignored and is included in Docker image\n", entry)
} else {
fmt.Printf("%s\n", entry)
}
colorize.Unset()
}
}
if config.Debug {
fmt.Printf("visited file or dir: %q\n", path)
}
return nil
})
if err != nil {
fmt.Printf("error walking the path %q: %v\n", path, err)
return 2
}
return 0
}
func loadGlobalConfigFile(configFile string, config *Config) (rv bool, err error) {
if _, err := os.Stat(configFile); !errors.Is(err, fs.ErrNotExist) {
jsonData, err := os.ReadFile(configFile)
if err != nil {
return false, err
}
err = json.Unmarshal(jsonData, &config)
if err != nil {
return false, err
}
if config.Debug {
fmt.Println("Config file:")
fmt.Println("\tcolor: ", config.Color)
fmt.Println("\tnocolor: ", config.Nocolor)
fmt.Println("\tignorefile: ", config.Ignorefile)
fmt.Println("\tdebug: ", config.Debug)
fmt.Println("\tverbose: ", config.Verbose)
fmt.Println("\texcluded: ", config.Excluded)
fmt.Println("\tincluded: ", config.Included)
fmt.Println("\tfullpath: ", config.Fullpath)
fmt.Println("\tnofullpath: ", config.Nofullpath)
}
} else {
return false, fmt.Errorf("Config file %s not found", configFile)
}
return true, nil
}
func loadLocalConfigFile(configFile string, config *Config) (rv bool, err error) {
if _, err := os.Stat(configFile); !errors.Is(err, fs.ErrNotExist) {
jsonData, err := os.ReadFile(configFile)
if err != nil {
return false, err
}
err = json.Unmarshal(jsonData, &config)
if err != nil {
return false, err
}
if config.Debug {
fmt.Println("Config file:")
fmt.Println("\tcolor: ", config.Color)
fmt.Println("\tnocolor: ", config.Nocolor)
fmt.Println("\tignorefile: ", config.Ignorefile)
fmt.Println("\tdebug: ", config.Debug)
fmt.Println("\tverbose: ", config.Verbose)
fmt.Println("\texcluded: ", config.Excluded)
fmt.Println("\tincluded: ", config.Included)
fmt.Println("\tfullpath: ", config.Fullpath)
fmt.Println("\tnofullpath: ", config.Nofullpath)
}
} else {
return false, fmt.Errorf("Config file %s not found", configFile)
}
return true, nil
}