-
Notifications
You must be signed in to change notification settings - Fork 4
/
filters.go
189 lines (153 loc) · 4.31 KB
/
filters.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
package leaf
import (
"fmt"
"path/filepath"
"strings"
)
// Filter can be used to Filter out watch results.
type Filter struct {
Include bool // whether to include pattern
Pattern string
}
// NewFilter creates a filter from the pattern string. The
// pattern either starts with '+' or '-' to include or
// exclude the directory from results.
func NewFilter(pattern string) (Filter, error) {
f := Filter{}
var err error
cleanedPattern := strings.Trim(pattern, " ")
if len(cleanedPattern) < 2 {
return f, fmt.Errorf(
"effective pattern '%s' invalid", cleanedPattern)
}
toInclude := cleanedPattern[0]
if toInclude == '+' {
f.Include = true
} else if toInclude == '-' {
f.Include = false
} else {
return f, fmt.Errorf(
"should have first character as '+' or '-'")
}
onlyPath := strings.Trim(cleanedPattern[1:], " ")
f.Pattern, err = filepath.Abs(onlyPath)
if err != nil {
return f, fmt.Errorf(
"error making path absolute: %v", err)
}
return f, nil
}
// A FilterCollection contains a bunch of includes and excludes.
type FilterCollection struct {
Includes []string
Excludes []string
match FilterMatchFunc
handle FilterHandleFunc
}
// NewFilterCollection creates a filter collection from a bunch
// of filter patterns.
func NewFilterCollection(filters []Filter, mf FilterMatchFunc, hf FilterHandleFunc) *FilterCollection {
collection := &FilterCollection{
Includes: []string{},
Excludes: []string{},
match: mf,
handle: hf,
}
if len(filters) == 0 {
return collection
}
for _, f := range filters {
if f.Include {
collection.Includes = append(collection.Includes, f.Pattern)
} else {
collection.Excludes = append(collection.Excludes, f.Pattern)
}
}
return collection
}
// NewFCFromPatterns creates a filter collection from a list of
// string format filters, like `+ /path/to/some/dir`.
func NewFCFromPatterns(patterns []string, mf FilterMatchFunc, hf FilterHandleFunc) (*FilterCollection, error) {
filters := []Filter{}
for _, p := range patterns {
f, err := NewFilter(p)
if err != nil {
return nil, err
}
filters = append(filters, f)
}
return NewFilterCollection(filters, mf, hf), nil
}
// FilterMatchFunc compares the pattern with the path of
// the file changed and returns true if the path resembles
// the given pattern.
type FilterMatchFunc func(pattern, path string) bool
// StandardFilterMatcher matches the pattern with the path
// and returns true if the path either starts with
// (in absolute terms) or matches like the path regex.
func StandardFilterMatcher(pattern, path string) bool {
matched, err := filepath.Match(pattern, path)
if err != nil {
return false
}
if matched {
return true
}
isDir, err := isDir(pattern)
if err != nil || !isDir {
return false
}
if len(path) < len(pattern) {
return false
}
if path[:len(pattern)] == pattern {
return true
}
return false
}
// HasInclude tells if the collection matches the path with
// one of its includes.
func (fc *FilterCollection) HasInclude(path string) bool {
cleanedPath := filepath.Clean(path)
for _, pattern := range fc.Includes {
if fc.match(pattern, cleanedPath) {
return true
}
}
return false
}
// HasExclude tells if the collection matches the path with
// one of its excludes.
func (fc *FilterCollection) HasExclude(path string) bool {
cleanedPath := filepath.Clean(path)
for _, pattern := range fc.Excludes {
if fc.match(pattern, cleanedPath) {
return true
}
}
return false
}
// ShouldHandlePath returns the result of the path handler
// for the filter collection.
func (fc *FilterCollection) ShouldHandlePath(path string) bool {
handlerFunc := fc.handle
return handlerFunc(fc, path)
}
// FilterHandleFunc is a function that checks if for the filter
// collection, should the path be handled or not, i.e., should
// the notifier tick for change in path or not.
type FilterHandleFunc func(fc *FilterCollection, path string) bool
// StandardFilterHandler returns true if the path should be included
// and returns false if path should not be included in result.
func StandardFilterHandler(fc *FilterCollection, path string) bool {
handle := false
// If there are no includes, path should be handled unless
// it is in the excludes.
if len(fc.Includes) == 0 || fc.HasInclude(path) {
handle = true
}
if fc.HasExclude(path) {
handle = false
}
return handle
}