forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatchers.go
294 lines (257 loc) · 6.65 KB
/
matchers.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
package finder
import (
"path"
"regexp"
"strings"
"time"
"github.com/gookit/goutil/fsutil"
"github.com/gookit/goutil/mathutil"
"github.com/gookit/goutil/strutil"
"github.com/gookit/goutil/timex"
)
// ------------------ built in filters ------------------
// MatchFile only allow file path.
var MatchFile = MatcherFunc(func(el Elem) bool {
return !el.IsDir()
})
// MatchDir only allow dir path.
var MatchDir = MatcherFunc(func(el Elem) bool {
return el.IsDir()
})
// StartWithDot match dot file/dir. eg: ".gitignore"
func StartWithDot() MatcherFunc {
return func(el Elem) bool {
name := el.Name()
return len(name) > 0 && name[0] == '.'
}
}
// MatchDotFile match dot filename. eg: ".idea"
func MatchDotFile() MatcherFunc {
return func(el Elem) bool {
return !el.IsDir() && el.Name()[0] == '.'
}
}
// MatchDotDir match dot dirname. eg: ".idea"
func MatchDotDir() MatcherFunc {
return func(el Elem) bool {
return el.IsDir() && el.Name()[0] == '.'
}
}
// MatchExt match filepath by given file ext.
//
// Usage:
//
// f := NewFinder('path/to/dir')
// f.Add(MatchExt(".go"))
// f.Not(MatchExt(".md"))
func MatchExt(exts ...string) MatcherFunc { return MatchExts(exts) }
// MatchExts filter filepath by given file ext.
func MatchExts(exts []string) MatcherFunc {
return func(el Elem) bool {
elExt := path.Ext(el.Name())
for _, ext := range exts {
if ext == elExt {
return true
}
}
return false
}
}
// MatchName match filepath by given names.
//
// Usage:
//
// f := NewFinder('path/to/dir')
// f.Not(MatchName("README.md", "*_test.go"))
func MatchName(names ...string) MatcherFunc { return MatchNames(names) }
// MatchNames match filepath by given names or patterns.
//
// Usage:
//
// f.Not(MatchNames([]string{"README.md", "*_test.go"}))
func MatchNames(names []string) MatcherFunc {
return func(el Elem) bool {
elName := el.Name()
for _, name := range names {
if name == elName || fsutil.PathMatch(name, elName) {
return true
}
}
return false
}
}
// MatchPrefix match filepath by check given prefixes.
//
// Usage:
//
// f := NewFinder('path/to/dir')
// f.Add(finder.MatchPrefix("app_", "README"))
func MatchPrefix(prefixes ...string) MatcherFunc { return MatchPrefixes(prefixes) }
// MatchPrefixes match filepath by check given prefixes.
func MatchPrefixes(prefixes []string) MatcherFunc {
return func(el Elem) bool {
for _, pfx := range prefixes {
if strings.HasPrefix(el.Name(), pfx) {
return true
}
}
return false
}
}
// MatchSuffix match filepath by check path has suffixes.
//
// Usage:
//
// f := NewFinder('path/to/dir')
// f.Add(finder.MatchSuffix("util.go", "en.md"))
// f.Not(finder.MatchSuffix("_test.go", ".log"))
func MatchSuffix(suffixes ...string) MatcherFunc { return MatchSuffixes(suffixes) }
// MatchSuffixes match filepath by check path has suffixes.
func MatchSuffixes(suffixes []string) MatcherFunc {
return func(el Elem) bool {
for _, sfx := range suffixes {
if strings.HasSuffix(el.Path(), sfx) {
return true
}
}
return false
}
}
// MatchPath match file/dir by given sub paths.
//
// Usage:
//
// f := NewFinder('path/to/dir')
// f.Add(MatchPath("need/path"))
func MatchPath(subPaths ...string) MatcherFunc { return MatchPaths(subPaths) }
// MatchPaths match file/dir by given sub paths.
func MatchPaths(subPaths []string) MatcherFunc {
return func(el Elem) bool {
for _, subPath := range subPaths {
if strings.Contains(el.Path(), subPath) {
return true
}
}
return false
}
}
// GlobMatch file/dir name by given patterns.
//
// Usage:
//
// f := NewFinder('path/to/dir')
// f.AddFilter(GlobMatch("*_test.go"))
func GlobMatch(patterns ...string) MatcherFunc { return GlobMatches(patterns) }
// GlobMatches file/dir name by given patterns.
func GlobMatches(patterns []string) MatcherFunc {
return func(el Elem) bool {
for _, pattern := range patterns {
if ok, _ := path.Match(pattern, el.Name()); ok {
return true
}
}
return false
}
}
// RegexMatch match name by given regex pattern
//
// Usage:
//
// f := NewFinder('path/to/dir')
// f.AddFilter(RegexMatch(`[A-Z]\w+`))
func RegexMatch(pattern string) MatcherFunc {
reg := regexp.MustCompile(pattern)
return func(el Elem) bool {
return reg.MatchString(el.Name())
}
}
// NameLike exclude filepath by given name match.
func NameLike(patterns ...string) MatcherFunc { return NameLikes(patterns) }
// NameLikes filter filepath by given name match.
func NameLikes(patterns []string) MatcherFunc {
return func(el Elem) bool {
for _, pattern := range patterns {
if strutil.LikeMatch(pattern, el.Name()) {
return true
}
}
return false
}
}
//
// ----------------- built in file info filters -----------------
//
// MatchMtime match file by modify time.
//
// Note: if time is zero, it will be ignored.
//
// Usage:
//
// f := NewFinder('path/to/dir')
// // -600 seconds to now(last 10 minutes)
// f.AddFile(MatchMtime(timex.NowAddSec(-600), timex.ZeroTime))
// // before 600 seconds(before 10 minutes)
// f.AddFile(MatchMtime(timex.ZeroTime, timex.NowAddSec(-600)))
func MatchMtime(start, end time.Time) MatcherFunc {
return MatchModTime(start, end)
}
// MatchModTime filter file by modify time.
func MatchModTime(start, end time.Time) MatcherFunc {
return func(el Elem) bool {
if el.IsDir() {
return false
}
fi, err := el.Info()
if err != nil {
return false
}
return timex.InRange(fi.ModTime(), start, end)
}
}
var timeNumReg = regexp.MustCompile(`(-?\d+)`)
// HumanModTime filter file by modify time string.
//
// Usage:
//
// f := finder.NewFinder()
// f.Include(HumanModTime(">10m")) // before 10 minutes
// f.Include(HumanModTime("<10m")) // latest 10 minutes, to Now
func HumanModTime(expr string) MatcherFunc {
opt := &timex.ParseRangeOpt{AutoSort: true}
// convert > to <, < to >
expr = strutil.Replaces(expr, map[string]string{">": "<", "<": ">"})
expr = timeNumReg.ReplaceAllStringFunc(expr, func(s string) string {
if s[0] == '-' {
return s
}
return "-" + s
})
start, end, err := timex.ParseRange(expr, opt)
if err != nil {
panic(err)
}
return MatchModTime(start, end)
}
// FileSize match file by file size. unit: byte
func FileSize(min, max uint64) MatcherFunc { return SizeRange(min, max) }
// SizeRange match file by file size. unit: byte
func SizeRange(min, max uint64) MatcherFunc {
return func(el Elem) bool {
if el.IsDir() {
return false
}
fi, err := el.Info()
if err != nil {
return false
}
return mathutil.InUintRange(uint64(fi.Size()), min, max)
}
}
// HumanSize match file by file size string. eg: ">1k", "<2m", "1g~3g"
func HumanSize(expr string) MatcherFunc {
min, max, err := strutil.ParseSizeRange(expr, nil)
if err != nil {
panic(err)
}
return SizeRange(min, max)
}