|
1 | 1 | package files |
2 | 2 |
|
3 | | -import "path" |
| 3 | +import "github.com/gobwas/glob" |
4 | 4 |
|
5 | 5 | // Filters represents what files to include, and which to exclude from copying operations. |
6 | 6 | type Filters struct { |
7 | 7 | Include []string `json:"include"` |
8 | 8 | Exclude []string `json:"exclude"` |
9 | 9 | } |
10 | 10 |
|
11 | | -func (f Filters) Matches(pth string) bool { |
| 11 | +func (f Filters) Matcher() Matcher { |
| 12 | + m := Matcher{ |
| 13 | + Include: make([]glob.Glob, 0, len(f.Include)), |
| 14 | + Exclude: make([]glob.Glob, 0, len(f.Exclude)), |
| 15 | + } |
| 16 | + separators := []rune{'/'} |
| 17 | + for _, p := range f.Include { |
| 18 | + g := glob.MustCompile(p, separators...) |
| 19 | + m.Include = append(m.Include, g) |
| 20 | + } |
| 21 | + for _, p := range f.Exclude { |
| 22 | + g := glob.MustCompile(p, separators...) |
| 23 | + m.Exclude = append(m.Exclude, g) |
| 24 | + } |
| 25 | + return m |
| 26 | +} |
| 27 | + |
| 28 | +type Matcher struct { |
| 29 | + Include []glob.Glob |
| 30 | + Exclude []glob.Glob |
| 31 | +} |
| 32 | + |
| 33 | +func (m Matcher) Matches(pth string) bool { |
12 | 34 | result := false |
13 | | - for _, pattern := range f.Include { |
14 | | - matched, _ := path.Match(pattern, pth) |
15 | | - if matched { |
| 35 | + for _, pattern := range m.Include { |
| 36 | + if pattern.Match(pth) { |
16 | 37 | result = true |
17 | 38 | } |
18 | 39 | } |
19 | 40 | if !result { |
20 | 41 | return false |
21 | 42 | } |
22 | | - for _, pattern := range f.Exclude { |
23 | | - matched, _ := path.Match(pattern, pth) |
24 | | - if matched { |
| 43 | + for _, pattern := range m.Exclude { |
| 44 | + if pattern.Match(pth) { |
25 | 45 | return false |
26 | 46 | } |
27 | 47 | } |
|
0 commit comments