-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.go
48 lines (42 loc) · 1.04 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
package main
import (
"github.com/sabhiram/go-gitignore"
)
// FileFilter is an interface for file matching
type FileFilter interface {
// LoadFile loads patterns from file
LoadFile(ignoreFile string) error
// Match tests pathname
Match(pathname string) bool
}
// fileFilterImpl implements FileFilter interface
type fileFilterImpl struct {
matchers []*ignore.GitIgnore
}
// Match tests pathname
func (filter *fileFilterImpl) Match(pathname string) bool {
for _, m := range filter.matchers {
if m.MatchesPath(pathname) {
return true
}
}
return false
}
// LoadFile loads patterns from file
func (filter *fileFilterImpl) LoadFile(f string) error {
i, err := ignore.CompileIgnoreFile(f)
if err == nil {
filter.matchers = append(filter.matchers, i)
}
return err
}
// NewFileFilter returns FileFilter built from patterns passed in arguments
func NewFileFilter(patterns ...string) FileFilter {
var m []*ignore.GitIgnore
if len(patterns) > 0 {
m = append(m, ignore.CompileIgnoreLines(patterns...))
}
return &fileFilterImpl{
matchers: m,
}
}