forked from google/yamlfmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_collector.go
167 lines (151 loc) · 4.55 KB
/
path_collector.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
package yamlfmt
import (
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/bmatcuk/doublestar/v4"
"github.com/google/yamlfmt/internal/collections"
"github.com/google/yamlfmt/internal/logger"
ignore "github.com/sabhiram/go-gitignore"
)
type PathCollector interface {
CollectPaths() ([]string, error)
}
type FilepathCollector struct {
Include []string
Exclude []string
Extensions []string
}
func (c *FilepathCollector) CollectPaths() ([]string, error) {
logger.Debug(logger.DebugCodePaths, "using file path matching. include patterns: %s", c.Include)
pathsFound := []string{}
for _, inclPath := range c.Include {
info, err := os.Stat(inclPath)
if err != nil {
if !os.IsNotExist(err) {
return nil, err
}
continue
}
if !info.IsDir() {
pathsFound = append(pathsFound, inclPath)
continue
}
paths, err := c.walkDirectoryForYaml(inclPath)
if err != nil {
return nil, err
}
pathsFound = append(pathsFound, paths...)
}
logger.Debug(logger.DebugCodePaths, "found paths: %s", pathsFound)
pathsFoundSet := collections.SliceToSet(pathsFound)
pathsToFormat := collections.SliceToSet(pathsFound)
for _, exclPath := range c.Exclude {
info, err := os.Stat(exclPath)
if err != nil {
if !os.IsNotExist(err) {
return nil, err
}
continue
}
if info.IsDir() {
logger.Debug(logger.DebugCodePaths, "for exclude dir: %s", exclPath)
for foundPath := range pathsFoundSet {
if strings.HasPrefix(foundPath, exclPath) {
logger.Debug(logger.DebugCodePaths, "excluding %s", foundPath)
pathsToFormat.Remove(foundPath)
}
}
} else {
logger.Debug(logger.DebugCodePaths, "for exclude file: %s", exclPath)
removed := pathsToFormat.Remove(exclPath)
if removed {
logger.Debug(logger.DebugCodePaths, "found in paths, excluding")
}
}
}
pathsToFormatSlice := pathsToFormat.ToSlice()
logger.Debug(logger.DebugCodePaths, "paths to format: %s", pathsToFormat)
return pathsToFormatSlice, nil
}
func (c *FilepathCollector) walkDirectoryForYaml(dir string) ([]string, error) {
paths := []string{}
err := filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
if info.IsDir() {
return nil
}
extension := ""
if strings.Contains(info.Name(), ".") {
nameParts := strings.Split(info.Name(), ".")
extension = nameParts[len(nameParts)-1]
}
if collections.SliceContains(c.Extensions, extension) {
paths = append(paths, path)
}
return nil
})
return paths, err
}
type DoublestarCollector struct {
Include []string
Exclude []string
}
func (c *DoublestarCollector) CollectPaths() ([]string, error) {
logger.Debug(logger.DebugCodePaths, "using doublestar path matching. include patterns: %s", c.Include)
includedPaths := []string{}
for _, pattern := range c.Include {
logger.Debug(logger.DebugCodePaths, "trying pattern: %s", pattern)
globMatches, err := doublestar.FilepathGlob(pattern)
if err != nil {
return nil, err
}
logger.Debug(logger.DebugCodePaths, "pattern %s matches: %s", pattern, globMatches)
includedPaths = append(includedPaths, globMatches...)
}
pathsToFormatSet := collections.Set[string]{}
for _, path := range includedPaths {
if len(c.Exclude) == 0 {
pathsToFormatSet.Add(path)
continue
}
excluded := false
logger.Debug(logger.DebugCodePaths, "calculating excludes for %s", path)
for _, pattern := range c.Exclude {
match, err := doublestar.PathMatch(filepath.Clean(pattern), path)
if err != nil {
return nil, err
}
if match {
logger.Debug(logger.DebugCodePaths, "pattern %s matched, excluding", pattern)
excluded = true
break
}
logger.Debug(logger.DebugCodePaths, "pattern %s did not match path", pattern)
}
if !excluded {
logger.Debug(logger.DebugCodePaths, "path %s included", path)
pathsToFormatSet.Add(path)
}
}
pathsToFormat := pathsToFormatSet.ToSlice()
logger.Debug(logger.DebugCodePaths, "paths to format: %s", pathsToFormat)
return pathsToFormat, nil
}
func ExcludeWithGitignore(gitignorePath string, paths []string) ([]string, error) {
logger.Debug(logger.DebugCodePaths, "excluding paths with gitignore: %s", gitignorePath)
ignorer, err := ignore.CompileIgnoreFile(gitignorePath)
if err != nil {
return nil, err
}
pathsToFormat := []string{}
for _, path := range paths {
if ok, pattern := ignorer.MatchesPathHow(path); !ok {
pathsToFormat = append(pathsToFormat, path)
} else {
logger.Debug(logger.DebugCodePaths, "pattern %s matches %s, excluding", pattern.Line, path)
}
}
logger.Debug(logger.DebugCodePaths, "paths to format: %s", pathsToFormat)
return pathsToFormat, nil
}