forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
623 lines (513 loc) · 17.5 KB
/
config.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
package finder
import (
"fmt"
"strings"
"github.com/gookit/goutil/errorx"
"github.com/gookit/goutil/strutil"
)
// commonly dot file and dirs
var (
CommonlyDotDirs = []string{".git", ".idea", ".vscode", ".svn", ".hg"}
CommonlyDotFiles = []string{".gitignore", ".dockerignore", ".npmignore", ".DS_Store", ".env"}
)
// FindFlag type for find result.
type FindFlag uint8
// flags for find result.
const (
FlagFile FindFlag = iota + 1 // only find files(default)
FlagDir
FlagBoth = FlagFile | FlagDir
)
// ToFlag convert string to FindFlag
func ToFlag(s string) FindFlag {
switch strings.ToLower(s) {
case "dirs", "dir", "d":
return FlagDir
case "both", "b":
return FlagBoth
default:
return FlagFile
}
}
// Config for finder
type Config struct {
init bool
depth int
// ScanDirs scan dir paths for find.
ScanDirs []string `json:"scan_dirs"`
// FindFlags for find result. default is FlagFile
FindFlags FindFlag `json:"find_flags"`
// MaxDepth for find result. default is 0 - not limit
MaxDepth int `json:"max_depth"`
// UseAbsPath use abs path for find result. default is false
UseAbsPath bool `json:"use_abs_path"`
// CacheResult cache result for find result. default is false
CacheResult bool `json:"cache_result"`
// ExcludeDotDir exclude dot dir. default is true
ExcludeDotDir bool `json:"exclude_dot_dir"`
// ExcludeDotFile exclude dot dir. default is false
ExcludeDotFile bool `json:"exclude_dot_file"`
// Matchers generic include matchers for file/dir elems
Matchers []Matcher
// ExMatchers generic exclude matchers for file/dir elems
ExMatchers []Matcher
// DirMatchers include matchers for dir elems
DirMatchers []Matcher
// DirExMatchers exclude matchers for dir elems
DirExMatchers []Matcher
// FileMatchers include matchers for file elems
FileMatchers []Matcher
// FileExMatchers exclude matchers for file elems
FileExMatchers []Matcher
// commonly settings for build matchers
// IncludeDirs include dir name list. eg: {"model"}
IncludeDirs []string `json:"include_dirs"`
// IncludeExts include file ext name list. eg: {".go", ".md"}
IncludeExts []string `json:"include_exts"`
// IncludeFiles include file name list. eg: {"go.mod"}
IncludeFiles []string `json:"include_files"`
// IncludePaths include file/dir path list. eg: {"path/to"}
IncludePaths []string `json:"include_paths"`
// IncludeNames include file/dir name list. eg: {"test", "some.go"}
IncludeNames []string `json:"include_names"`
// ExcludeDirs exclude dir name list. eg: {"test"}
ExcludeDirs []string `json:"exclude_dirs"`
// ExcludeExts exclude file ext name list. eg: {".go", ".md"}
ExcludeExts []string `json:"exclude_exts"`
// ExcludeFiles exclude file name list. eg: {"go.mod"}
ExcludeFiles []string `json:"exclude_files"`
// ExcludePaths exclude file/dir path list. eg: {"path/to"}
ExcludePaths []string `json:"exclude_paths"`
// ExcludeNames exclude file/dir name list. eg: {"test", "some.go"}
ExcludeNames []string `json:"exclude_names"`
}
// NewConfig create a new Config
func NewConfig(dirs ...string) *Config {
return &Config{
ScanDirs: dirs,
FindFlags: FlagFile,
// with default setting.
ExcludeDotDir: true,
}
}
// NewEmptyConfig create a new Config
func NewEmptyConfig() *Config {
return &Config{FindFlags: FlagFile}
}
// LoadRules load rules and parse to config
//
// Rule Format:
//
// NAME:pattern1,pattern2
//
// Examples:
//
// ext:.go,.yaml
// name:*_test.go,go.mod
func (c *Config) LoadRules(addOrNot bool, rules []string) error {
es := errorx.Errors{}
for i, rule := range rules {
if !strings.Contains(rule, ":") {
es = append(es, fmt.Errorf("invalid rule#%d: %s", i, rule))
break
}
name, pattern := strutil.TrimCut(rule, ":")
if name == "" || pattern == "" {
es = append(es, fmt.Errorf("invalid rule#%d: %s", i, rule))
break
}
patterns := strutil.Split(pattern, ",")
switch name {
case "ext", "exts":
if addOrNot {
c.IncludeExts = append(c.IncludeExts, patterns...)
} else {
c.ExcludeExts = append(c.ExcludeExts, patterns...)
}
case "name", "names":
if addOrNot {
c.IncludeNames = append(c.IncludeNames, patterns...)
} else {
c.ExcludeNames = append(c.ExcludeNames, patterns...)
}
case "file", "files":
if addOrNot {
c.IncludeFiles = append(c.IncludeFiles, patterns...)
} else {
c.ExcludeFiles = append(c.ExcludeFiles, patterns...)
}
case "path", "paths":
if addOrNot {
c.IncludePaths = append(c.IncludePaths, patterns...)
} else {
c.ExcludePaths = append(c.ExcludePaths, patterns...)
}
case "dir", "dirs":
if addOrNot {
c.IncludeDirs = append(c.IncludeDirs, patterns...)
} else {
c.ExcludeDirs = append(c.ExcludeDirs, patterns...)
}
case "size": // size:>=1M,<=10M
if addOrNot {
for _, expr := range patterns {
c.FileMatchers = append(c.FileMatchers, HumanSize(expr))
}
} else {
for _, expr := range patterns {
c.FileExMatchers = append(c.FileExMatchers, HumanSize(expr))
}
}
case "time", "mtime": // mtime:>=1d,<=10d
if addOrNot {
for _, expr := range patterns {
c.FileMatchers = append(c.FileMatchers, HumanModTime(expr))
}
} else {
for _, expr := range patterns {
c.FileExMatchers = append(c.FileExMatchers, HumanModTime(expr))
}
}
}
}
return es.ErrorOrNil()
}
// NewFinder create a new Finder by config
func (c *Config) NewFinder() *Finder {
return NewWithConfig(c.Init())
}
// Init build matchers by config and append to Matchers.
func (c *Config) Init() *Config {
if c.init {
return c
}
// generic matchers
if len(c.IncludeNames) > 0 {
c.Matchers = append(c.Matchers, MatchNames(c.IncludeNames))
}
if len(c.IncludePaths) > 0 {
c.Matchers = append(c.Matchers, MatchPaths(c.IncludePaths))
}
if len(c.ExcludePaths) > 0 {
c.ExMatchers = append(c.ExMatchers, MatchPaths(c.ExcludePaths))
}
if len(c.ExcludeNames) > 0 {
c.ExMatchers = append(c.ExMatchers, MatchNames(c.ExcludeNames))
}
// dir matchers
if len(c.IncludeDirs) > 0 {
c.DirMatchers = append(c.DirMatchers, MatchNames(c.IncludeDirs))
}
if len(c.ExcludeDirs) > 0 {
c.DirExMatchers = append(c.DirExMatchers, MatchNames(c.ExcludeDirs))
}
// file matchers
if len(c.IncludeExts) > 0 {
c.FileMatchers = append(c.FileMatchers, MatchExts(c.IncludeExts))
}
if len(c.IncludeFiles) > 0 {
c.FileMatchers = append(c.FileMatchers, MatchNames(c.IncludeFiles))
}
if len(c.ExcludeExts) > 0 {
c.FileExMatchers = append(c.FileExMatchers, MatchExts(c.ExcludeExts))
}
if len(c.ExcludeFiles) > 0 {
c.FileExMatchers = append(c.FileExMatchers, MatchNames(c.ExcludeFiles))
}
return c
}
//
// --------- config finder by rules ---------
//
// IncludeRule include rules for finder
func (f *Finder) IncludeRule(rules ...string) *Finder {
f.err = f.c.LoadRules(true, rules)
return f
}
// IncludeRules include rules for finder
func (f *Finder) IncludeRules(rules []string) *Finder {
f.err = f.c.LoadRules(true, rules)
return f
}
// ExcludeRule exclude rules for finder
func (f *Finder) ExcludeRule(rules ...string) *Finder {
f.err = f.c.LoadRules(false, rules)
return f
}
// ExcludeRules exclude rules for finder
func (f *Finder) ExcludeRules(rules []string) *Finder {
f.err = f.c.LoadRules(false, rules)
return f
}
// WithRules on the finder
//
// Rule Format:
//
// NAME:pattern1,pattern2
//
// Examples:
//
// ext:.go,.yaml
// name:*_test.go,go.mod
func (f *Finder) WithRules(addOrNot bool, rules []string) *Finder {
f.err = f.c.LoadRules(addOrNot, rules)
return f
}
//
// --------- config for finder ---------
//
// WithConfig on the finder
func (f *Finder) WithConfig(c *Config) *Finder {
f.c = c
return f
}
// ConfigFn the finder. alias of WithConfigFn()
func (f *Finder) ConfigFn(fns ...func(c *Config)) *Finder { return f.WithConfigFn(fns...) }
// WithConfigFn the finder
func (f *Finder) WithConfigFn(fns ...func(c *Config)) *Finder {
if f.c == nil {
f.c = &Config{}
}
for _, fn := range fns {
fn(f.c)
}
return f
}
// AddScanDirs add source dir for find
func (f *Finder) AddScanDirs(dirPaths []string) *Finder {
f.c.ScanDirs = append(f.c.ScanDirs, dirPaths...)
return f
}
// AddScanDir add source dir for find. alias of AddScanDirs()
func (f *Finder) AddScanDir(dirPaths ...string) *Finder { return f.AddScanDirs(dirPaths) }
// AddScan add source dir for find. alias of AddScanDirs()
func (f *Finder) AddScan(dirPaths ...string) *Finder { return f.AddScanDirs(dirPaths) }
// ScanDir add source dir for find. alias of AddScanDirs()
func (f *Finder) ScanDir(dirPaths ...string) *Finder { return f.AddScanDirs(dirPaths) }
// CacheResult cache result for find result.
func (f *Finder) CacheResult(enable ...bool) *Finder {
if len(enable) > 0 {
f.c.CacheResult = enable[0]
} else {
f.c.CacheResult = true
}
return f
}
// WithFlags set find flags.
func (f *Finder) WithFlags(flags FindFlag) *Finder {
f.c.FindFlags = flags
return f
}
// WithStrFlag set find flags by string.
func (f *Finder) WithStrFlag(s string) *Finder {
f.c.FindFlags = ToFlag(s)
return f
}
// OnlyFindDir only find dir.
func (f *Finder) OnlyFindDir() *Finder { return f.WithFlags(FlagDir) }
// FileAndDir both find file and dir.
func (f *Finder) FileAndDir() *Finder { return f.WithFlags(FlagDir | FlagFile) }
// UseAbsPath use absolute path for find result. alias of WithUseAbsPath()
func (f *Finder) UseAbsPath(enable ...bool) *Finder { return f.WithUseAbsPath(enable...) }
// WithUseAbsPath use absolute path for find result.
func (f *Finder) WithUseAbsPath(enable ...bool) *Finder {
if len(enable) > 0 {
f.c.UseAbsPath = enable[0]
} else {
f.c.UseAbsPath = true
}
return f
}
// WithMaxDepth set max depth for find.
func (f *Finder) WithMaxDepth(i int) *Finder {
f.c.MaxDepth = i
return f
}
// IncludeDir include dir names.
func (f *Finder) IncludeDir(dirs ...string) *Finder {
f.c.IncludeDirs = append(f.c.IncludeDirs, dirs...)
return f
}
// WithDirName include dir names. alias of IncludeDir()
func (f *Finder) WithDirName(dirs ...string) *Finder { return f.IncludeDir(dirs...) }
// IncludeFile include file names.
func (f *Finder) IncludeFile(files ...string) *Finder {
f.c.IncludeFiles = append(f.c.IncludeFiles, files...)
return f
}
// WithFileName include file names. alias of IncludeFile()
func (f *Finder) WithFileName(files ...string) *Finder { return f.IncludeFile(files...) }
// IncludeName include file or dir names.
func (f *Finder) IncludeName(names ...string) *Finder {
f.c.IncludeNames = append(f.c.IncludeNames, names...)
return f
}
// WithNames include file or dir names. alias of IncludeName()
func (f *Finder) WithNames(names []string) *Finder { return f.IncludeName(names...) }
// IncludeExt include file exts.
func (f *Finder) IncludeExt(exts ...string) *Finder {
f.c.IncludeExts = append(f.c.IncludeExts, exts...)
return f
}
// WithExts include file exts. alias of IncludeExt()
func (f *Finder) WithExts(exts []string) *Finder { return f.IncludeExt(exts...) }
// WithFileExt include file exts. alias of IncludeExt()
func (f *Finder) WithFileExt(exts ...string) *Finder { return f.IncludeExt(exts...) }
// IncludePath include file or dir paths.
func (f *Finder) IncludePath(paths ...string) *Finder {
f.c.IncludePaths = append(f.c.IncludePaths, paths...)
return f
}
// WithPaths include file or dir paths. alias of IncludePath()
func (f *Finder) WithPaths(paths []string) *Finder { return f.IncludePath(paths...) }
// WithSubPath include file or dir paths. alias of IncludePath()
func (f *Finder) WithSubPath(paths ...string) *Finder { return f.IncludePath(paths...) }
// ExcludeDir exclude dir names.
func (f *Finder) ExcludeDir(dirs ...string) *Finder {
f.c.ExcludeDirs = append(f.c.ExcludeDirs, dirs...)
return f
}
// WithoutDir exclude dir names. alias of ExcludeDir()
func (f *Finder) WithoutDir(dirs ...string) *Finder { return f.ExcludeDir(dirs...) }
// WithoutNames exclude file or dir names.
func (f *Finder) WithoutNames(names []string) *Finder {
f.c.ExcludeNames = append(f.c.ExcludeNames, names...)
return f
}
// ExcludeName exclude file names. alias of WithoutNames()
func (f *Finder) ExcludeName(names ...string) *Finder { return f.WithoutNames(names) }
// ExcludeFile exclude file names.
func (f *Finder) ExcludeFile(files ...string) *Finder {
f.c.ExcludeFiles = append(f.c.ExcludeFiles, files...)
return f
}
// WithoutFile exclude file names. alias of ExcludeFile()
func (f *Finder) WithoutFile(files ...string) *Finder { return f.ExcludeFile(files...) }
// ExcludeExt exclude file exts.
//
// eg: ExcludeExt(".go", ".java")
func (f *Finder) ExcludeExt(exts ...string) *Finder {
f.c.ExcludeExts = append(f.c.ExcludeExts, exts...)
return f
}
// WithoutExt exclude file exts. alias of ExcludeExt()
func (f *Finder) WithoutExt(exts ...string) *Finder { return f.ExcludeExt(exts...) }
// WithoutExts exclude file exts. alias of ExcludeExt()
func (f *Finder) WithoutExts(exts []string) *Finder { return f.ExcludeExt(exts...) }
// ExcludePath exclude file paths.
func (f *Finder) ExcludePath(paths ...string) *Finder {
f.c.ExcludePaths = append(f.c.ExcludePaths, paths...)
return f
}
// WithoutPath exclude file paths. alias of ExcludePath()
func (f *Finder) WithoutPath(paths ...string) *Finder { return f.ExcludePath(paths...) }
// WithoutPaths exclude file paths. alias of ExcludePath()
func (f *Finder) WithoutPaths(paths []string) *Finder { return f.ExcludePath(paths...) }
// ExcludeDotDir exclude dot dir names. eg: ".idea"
func (f *Finder) ExcludeDotDir(exclude ...bool) *Finder {
if len(exclude) > 0 {
f.c.ExcludeDotDir = exclude[0]
} else {
f.c.ExcludeDotDir = true
}
return f
}
// WithoutDotDir exclude dot dir names. alias of ExcludeDotDir().
func (f *Finder) WithoutDotDir(exclude ...bool) *Finder {
return f.ExcludeDotDir(exclude...)
}
// NoDotDir exclude dot dir names. alias of ExcludeDotDir().
func (f *Finder) NoDotDir(exclude ...bool) *Finder {
return f.ExcludeDotDir(exclude...)
}
// ExcludeDotFile exclude dot dir names. eg: ".gitignore"
func (f *Finder) ExcludeDotFile(exclude ...bool) *Finder {
if len(exclude) > 0 {
f.c.ExcludeDotFile = exclude[0]
} else {
f.c.ExcludeDotFile = true
}
return f
}
// WithoutDotFile exclude dot dir names. alias of ExcludeDotFile().
func (f *Finder) WithoutDotFile(exclude ...bool) *Finder {
return f.ExcludeDotFile(exclude...)
}
// NoDotFile exclude dot dir names. alias of ExcludeDotFile().
func (f *Finder) NoDotFile(exclude ...bool) *Finder {
return f.ExcludeDotFile(exclude...)
}
//
// --------- add matchers to finder ---------
//
// Includes add include match matchers
func (f *Finder) Includes(fls []Matcher) *Finder {
f.c.Matchers = append(f.c.Matchers, fls...)
return f
}
// Collect add include match matchers. alias of Includes()
func (f *Finder) Collect(fls ...Matcher) *Finder { return f.Includes(fls) }
// Include add include match matchers. alias of Includes()
func (f *Finder) Include(fls ...Matcher) *Finder { return f.Includes(fls) }
// With add include match matchers. alias of Includes()
func (f *Finder) With(fls ...Matcher) *Finder { return f.Includes(fls) }
// Adds include match matchers. alias of Includes()
func (f *Finder) Adds(fls []Matcher) *Finder { return f.Includes(fls) }
// Add include match matchers. alias of Includes()
func (f *Finder) Add(fls ...Matcher) *Finder { return f.Includes(fls) }
// Excludes add exclude match matchers
func (f *Finder) Excludes(fls []Matcher) *Finder {
f.c.ExMatchers = append(f.c.ExMatchers, fls...)
return f
}
// Exclude add exclude match matchers. alias of Excludes()
func (f *Finder) Exclude(fls ...Matcher) *Finder { return f.Excludes(fls) }
// Without add exclude match matchers. alias of Excludes()
func (f *Finder) Without(fls ...Matcher) *Finder { return f.Excludes(fls) }
// Nots add exclude match matchers. alias of Excludes()
func (f *Finder) Nots(fls []Matcher) *Finder { return f.Excludes(fls) }
// Not add exclude match matchers. alias of Excludes()
func (f *Finder) Not(fls ...Matcher) *Finder { return f.Excludes(fls) }
// WithMatchers add include matchers
func (f *Finder) WithMatchers(fls []Matcher) *Finder {
f.c.Matchers = append(f.c.Matchers, fls...)
return f
}
// WithFilter add include matchers
func (f *Finder) WithFilter(fls ...Matcher) *Finder { return f.WithMatchers(fls) }
// MatchFiles add include file matchers
func (f *Finder) MatchFiles(fls []Matcher) *Finder {
f.c.FileMatchers = append(f.c.FileMatchers, fls...)
return f
}
// MatchFile add include file matchers
func (f *Finder) MatchFile(fls ...Matcher) *Finder { return f.MatchFiles(fls) }
// AddFiles add include file matchers
func (f *Finder) AddFiles(fls []Matcher) *Finder { return f.MatchFiles(fls) }
// AddFile add include file matchers
func (f *Finder) AddFile(fls ...Matcher) *Finder { return f.MatchFiles(fls) }
// NotFiles add exclude file matchers
func (f *Finder) NotFiles(fls []Matcher) *Finder {
f.c.FileExMatchers = append(f.c.FileExMatchers, fls...)
return f
}
// NotFile add exclude file matchers
func (f *Finder) NotFile(fls ...Matcher) *Finder { return f.NotFiles(fls) }
// MatchDirs add exclude dir matchers
func (f *Finder) MatchDirs(fls []Matcher) *Finder {
f.c.DirMatchers = append(f.c.DirMatchers, fls...)
return f
}
// MatchDir add exclude dir matchers
func (f *Finder) MatchDir(fls ...Matcher) *Finder { return f.MatchDirs(fls) }
// WithDirs add exclude dir matchers
func (f *Finder) WithDirs(fls []Matcher) *Finder { return f.MatchDirs(fls) }
// WithDir add exclude dir matchers
func (f *Finder) WithDir(fls ...Matcher) *Finder { return f.MatchDirs(fls) }
// NotDirs add exclude dir matchers
func (f *Finder) NotDirs(fls []Matcher) *Finder {
f.c.DirExMatchers = append(f.c.DirExMatchers, fls...)
return f
}
// NotDir add exclude dir matchers
func (f *Finder) NotDir(fls ...Matcher) *Finder { return f.NotDirs(fls) }