-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwalker.go
More file actions
62 lines (51 loc) · 1.12 KB
/
walker.go
File metadata and controls
62 lines (51 loc) · 1.12 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
// walks directory, applies exclusion rules, returns list of files.
package main
import (
"os"
"path/filepath"
"slices"
"strings"
)
func shouldSkipDir(name string, cfg *Config) bool {
return slices.Contains(cfg.ExcludeDirs, name)
}
func shouldSkipExt(path string, cfg *Config) bool {
ext := strings.ToLower(filepath.Ext(path))
for _, e := range cfg.ExcludeFileExts {
if ext == strings.ToLower(e) {
return true
}
}
return false
}
func shouldSkipFile(name string, cfg *Config) bool {
for _, pattern := range cfg.ExcludeFileNames {
if strings.EqualFold(name, pattern) {
return true
}
}
return false
}
func CollectFiles(root string, cfg *Config) ([]string, error) {
var files []string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil // ignore errors but continue
}
if info.IsDir() {
if shouldSkipDir(info.Name(), cfg) {
return filepath.SkipDir
}
return nil
}
if shouldSkipExt(path, cfg) {
return nil
}
if shouldSkipFile(path, cfg) {
return nil
}
files = append(files, path)
return nil
})
return files, err
}