-
Notifications
You must be signed in to change notification settings - Fork 199
/
multi.go
72 lines (57 loc) · 1.59 KB
/
multi.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
package path_matcher
import (
"fmt"
"sort"
"strings"
"github.com/werf/werf/pkg/util"
)
func NewMultiPathMatcher(matchers ...PathMatcher) PathMatcher {
if len(matchers) == 0 {
matchers = append(matchers, NewTruePathMatcher())
}
return MultiPathMatcher{matchers: matchers}
}
type MultiPathMatcher struct {
matchers []PathMatcher
}
func (m MultiPathMatcher) IsDirOrSubmodulePathMatched(path string) bool {
return m.IsPathMatched(path) || m.ShouldGoThrough(path)
}
func (m MultiPathMatcher) IsPathMatched(path string) bool {
for _, matcher := range m.matchers {
if !matcher.IsPathMatched(path) {
return false
}
}
return true
}
// ShouldGoThrough returns true if the ShouldGoThrough method of at least one matcher returns true and the path partially or completely matched by others (IsDirOrSubmodulePathMatched returns true)
func (m MultiPathMatcher) ShouldGoThrough(path string) bool {
var shouldGoThrough bool
for _, matcher := range m.matchers {
if matcher.ShouldGoThrough(path) {
shouldGoThrough = true
} else if !matcher.IsPathMatched(path) {
return false
}
}
return shouldGoThrough
}
func (m MultiPathMatcher) ID() string {
var ids []string
for _, matcher := range m.matchers {
ids = append(ids, matcher.ID())
}
sort.Strings(ids)
var args []string
args = append(args, "multi")
args = append(args, ids...)
return util.Sha256Hash(strings.Join(args, ":::"))
}
func (m MultiPathMatcher) String() string {
var result []string
for _, matcher := range m.matchers {
result = append(result, matcher.String())
}
return fmt.Sprintf("{ %s }", strings.Join(result, " && "))
}