-
Notifications
You must be signed in to change notification settings - Fork 0
/
path-patterns.go
255 lines (225 loc) · 5.1 KB
/
path-patterns.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
package rulespatterns
import (
"errors"
"os"
slashpath "path"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/nogproject/nog/backend/internal/nogfsostad/discoveryd/rules"
)
var ErrMalformedPattern = errors.New("malformed pattern")
var ErrInvalidAction = errors.New("invalid action")
var ErrMalformedDepthPath = errors.New("malformed depth path")
var ErrTooDeep = errors.New("too many subdirs in depth path")
type Config struct {
Patterns []string
EnabledPaths []string
}
type action int
const (
actEnter action = 1 + iota
actRepo
actSuperRepo
actIgnore
)
type pattern struct {
action action
glob string
}
// `rulespatterns.Finder` applies a list of patterns to identify repo
// candidates. Create with `NewFinder()`. Example `Patterns`:
//
// superrepo .
// superrepo foo
// repo foo/overview
// enter foo/data
// repo foo/data/*
// ignore foo/*
// ignore *
//
// `EnabledPaths` is a list of `<depth> <path>` pairs that are prepended before
// the patterns as follows:
//
// 0 foo -> "superrepo foo"
// 1 foo -> "superrepo foo", "superrepo foo/*"
// 2 foo -> "superrepo foo", "superrepo foo/*", "superrepo foo/*/*"
//
type Finder struct {
patterns []pattern
}
func NewFinder(cfg Config) (*Finder, error) {
patterns := make([]pattern, 0)
for _, depthPath := range cfg.EnabledPaths {
toks := strings.SplitN(depthPath, " ", 2)
if len(toks) != 2 {
return nil, ErrMalformedDepthPath
}
depth, err := strconv.ParseUint(toks[0], 10, 32)
if err != nil {
return nil, ErrMalformedDepthPath
}
// The limit here must be greater or equal to the limit in
// `fsorepos/pbevents/pbevents.go`.
if depth > 3 {
return nil, ErrTooDeep
}
glob := escapeGlob(toks[1])
for i := 0; i <= int(depth); i++ {
patterns = append(patterns, pattern{
action: actSuperRepo,
glob: glob,
})
glob += "/*"
}
}
for i, pat := range cfg.Patterns {
toks := strings.SplitN(pat, " ", 2)
if len(toks) != 2 {
return nil, ErrMalformedPattern
}
actString := toks[0]
glob := toks[1]
// Unless the first pattern explicitly handles the root, add a
// rule to enter it.
if i == 0 && glob != "." {
patterns = append(patterns, pattern{
action: actEnter,
glob: ".",
})
}
var a action
switch actString {
case "enter":
a = actEnter
case "repo":
a = actRepo
case "superrepo":
a = actSuperRepo
case "ignore":
a = actIgnore
default:
return nil, ErrInvalidAction
}
patterns = append(patterns, pattern{
action: a,
glob: glob,
})
}
return &Finder{patterns}, nil
}
// The special glob chars that need be escaped are `*?[\`, see
// <https://godoc.org/path#Match>.
var rgxGlobSpecial = regexp.MustCompile(`[*?\[\\]`)
func escapeGlob(path string) string {
return rgxGlobSpecial.ReplaceAllString(path, `\$0`)
}
func (f *Finder) Find(
root string, known map[string]bool, fns rules.FindHandlerFuncs,
) error {
handleCandidate := func(p string) error {
if fns.CandidateFn != nil {
if err := fns.CandidateFn(p); err != nil {
return err
}
}
return filepath.SkipDir
}
handleCandidateEnter := func(p string) error {
if fns.CandidateFn != nil {
if err := fns.CandidateFn(p); err != nil {
return err
}
}
return nil
}
handleIgnore := func(p string) error {
if fns.IgnoreFn != nil {
if err := fns.IgnoreFn(p); err != nil {
return err
}
}
return filepath.SkipDir
}
handleKnown := func(p string) error {
if fns.KnownFn != nil {
if err := fns.KnownFn(p); err != nil {
return err
}
}
return filepath.SkipDir
}
handleKnownEnter := func(p string) error {
if fns.KnownFn != nil {
if err := fns.KnownFn(p); err != nil {
return err
}
}
return nil
}
handleAction := func(act action, relpath string) error {
switch act {
case actEnter:
return nil
case actRepo:
if known[relpath] {
return handleKnown(relpath)
}
return handleCandidate(relpath)
case actSuperRepo:
if known[relpath] {
return handleKnownEnter(relpath)
}
return handleCandidateEnter(relpath)
case actIgnore:
if known[relpath] {
return handleKnown(relpath)
}
return handleIgnore(relpath)
default:
panic("invalid action")
}
}
root = ensureTrailingSlash(root)
walkFn := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
return nil
}
relpath := strings.TrimPrefix(path, root)
if relpath == "" {
relpath = "."
}
// Ignore hidden by Unix convention.
if relpath != "." {
_, basename := filepath.Split(relpath)
if basename[0] == '.' {
return handleIgnore(relpath)
}
}
for _, pat := range f.patterns {
matched, err := slashpath.Match(pat.glob, relpath)
if err != nil {
continue // Silently ignore invalid patterns.
}
if matched {
return handleAction(pat.action, relpath)
}
}
// The default is to ignore.
return handleIgnore(relpath)
}
return filepath.Walk(root, walkFn)
}
func ensureTrailingSlash(s string) string {
if s == "" {
return "/"
}
if s[len(s)-1] == '/' {
return s
}
return s + "/"
}