-
Notifications
You must be signed in to change notification settings - Fork 189
/
filter.go
197 lines (173 loc) · 4.58 KB
/
filter.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
package finder
import (
"os"
"path"
"regexp"
"strings"
"time"
)
// FileFilter for filter file path.
type FileFilter interface {
FilterFile(filePath, filename string) bool
}
// FileFilterFunc for filter file path.
type FileFilterFunc func(filePath, filename string) bool
// FilterFile Filter for filter file path.
func (fn FileFilterFunc) FilterFile(filePath, filename string) bool {
return fn(filePath, filename)
}
// DirFilter for filter dir path.
type DirFilter interface {
FilterDir(dirPath, dirName string) bool
}
// DirFilterFunc for filter file path.
type DirFilterFunc func(dirPath, dirName string) bool
// FilterDir Filter for filter file path.
func (fn DirFilterFunc) FilterDir(dirPath, dirName string) bool {
return fn(dirPath, dirName)
}
// // BodyFilter for filter file contents.
// type BodyFilter interface {
// FilterBody(contents, filePath string) bool
// }
//
// // BodyFilterFunc for filter file contents.
// type BodyFilterFunc func(contents, filePath string) bool
//
// // Filter for filter file path.
// func (fn BodyFilterFunc) FilterBody(contents, filePath string) bool {
// return fn(contents, filePath)
// }
// FilterFunc for filter file path.
type FilterFunc func(filePath, filename string) bool
// Filter for filter file path.
func (fn FilterFunc) Filter(filePath, filename string) bool {
return fn(filePath, filename)
}
//
// ------------------ built in file path filters ------------------
//
// ExtFilterFunc filter filepath by given file ext.
//
// Usage:
// f := EmptyFinder()
// f.AddFilter(ExtFilterFunc([]string{".go", ".md"}, true))
// f.AddFilter(ExtFilterFunc([]string{".log", ".tmp"}, false))
func ExtFilterFunc(exts []string, include bool) FileFilterFunc {
return func(filePath, _ string) bool {
fExt := path.Ext(filePath)
for _, ext := range exts {
if fExt == ext {
return include
}
}
return !include
}
}
// SuffixFilterFunc filter filepath by given file ext.
//
// Usage:
// f := EmptyFinder()
// f.AddFilter(SuffixFilterFunc([]string{"util.go", ".md"}, true))
// f.AddFilter(SuffixFilterFunc([]string{"_test.go", ".log"}, false))
func SuffixFilterFunc(suffixes []string, include bool) FileFilterFunc {
return func(filePath, _ string) bool {
for _, sfx := range suffixes {
if strings.HasSuffix(filePath, sfx) {
return include
}
}
return !include
}
}
// PathNameFilterFunc filter filepath by given path names.
func PathNameFilterFunc(names []string, include bool) FileFilterFunc {
return func(filePath, _ string) bool {
for _, name := range names {
if strings.Contains(filePath, name) {
return include
}
}
return !include
}
}
// DotFileFilterFunc filter dot filename. eg: ".gitignore"
func DotFileFilterFunc(include bool) FileFilterFunc {
return func(filePath, filename string) bool {
// filename := path.Base(filePath)
if filename[0] == '.' {
return include
}
return !include
}
}
// ModTimeFilterFunc filter file by modify time.
func ModTimeFilterFunc(limitSec int, op rune, include bool) FileFilterFunc {
return func(filePath, filename string) bool {
fi, err := os.Stat(filePath)
if err != nil {
return !include
}
now := time.Now().Second()
if op == '>' {
if now-fi.ModTime().Second() > limitSec {
return include
}
return !include
}
// '<'
if now-fi.ModTime().Second() < limitSec {
return include
}
return !include
}
}
// GlobFilterFunc filter filepath by given patterns.
//
// Usage:
// f := EmptyFiler()
// f.AddFilter(GlobFilterFunc([]string{"*_test.go"}, true))
func GlobFilterFunc(patterns []string, include bool) FileFilterFunc {
return func(_, filename string) bool {
for _, pattern := range patterns {
if ok, _ := path.Match(pattern, filename); ok {
return include
}
}
return !include
}
}
// RegexFilterFunc filter filepath by given regex pattern
//
// Usage:
// f := EmptyFiler()
// f.AddFilter(RegexFilterFunc(`[A-Z]\w+`, true))
func RegexFilterFunc(pattern string, include bool) FileFilterFunc {
reg := regexp.MustCompile(pattern)
return func(_, filename string) bool {
return reg.MatchString(filename)
}
}
//
// ----------------- built in dir path filters -----------------
//
// DotDirFilterFunc filter dot dirname. eg: ".idea"
func DotDirFilterFunc(include bool) DirFilterFunc {
return func(_, dirname string) bool {
if dirname[0] == '.' {
return include
}
return !include
}
}
// DirNameFilterFunc filter filepath by given dir names.
func DirNameFilterFunc(names []string, include bool) DirFilterFunc {
return func(_, dirName string) bool {
for _, name := range names {
if dirName == name {
return include
}
}
return !include
}
}