-
Notifications
You must be signed in to change notification settings - Fork 38
/
basic.go
73 lines (67 loc) · 1.56 KB
/
basic.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
package matcher
import (
"os"
"path"
"regexp"
"strings"
)
//Basic represents prefix, suffix or regexp matcher
type Basic struct {
Prefix string `json:",omitempty"`
Suffix string `json:",omitempty"`
Filter string `json:",omitempty"`
Exclusion string `json:",omitempty"`
Directory *bool `json:",omitempty"`
compiledFilter *regexp.Regexp
comiledExclusion *regexp.Regexp
}
//Match matcher parent and info with matcher rules
func (r *Basic) Match(parent string, info os.FileInfo) bool {
if r.Directory != nil {
expectDir := *r.Directory
if expectDir != info.IsDir() {
return false
}
}
if r.Filter != "" && r.compiledFilter == nil {
r.compiledFilter, _ = regexp.Compile(r.Filter)
}
location := path.Join(parent, info.Name())
if r.compiledFilter != nil {
if !r.compiledFilter.MatchString(location) {
return false
}
}
if r.Prefix != "" {
if !strings.HasPrefix(location, r.Prefix) {
return false
}
}
if r.Suffix != "" {
if !strings.HasSuffix(location, r.Suffix) {
return false
}
}
if r.Exclusion != "" && r.comiledExclusion == nil {
r.comiledExclusion, _ = regexp.Compile(r.Exclusion)
}
if r.comiledExclusion != nil {
if r.comiledExclusion.MatchString(location) {
return false
}
}
return true
}
//NewBasic creates basic matcher
func NewBasic(prefix, suffix, filter string, dir *bool) (matcher *Basic, err error) {
matcher = &Basic{
Prefix: prefix,
Suffix: suffix,
Filter: filter,
Directory: dir,
}
if filter != "" {
matcher.compiledFilter, err = regexp.Compile(filter)
}
return matcher, err
}