generated from xmidt-org/.go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matcher.go
44 lines (35 loc) · 903 Bytes
/
matcher.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
package thoth
import (
"os"
"github.com/gobwas/glob"
)
// Matcher is a simple strategy for matching values, such as names
// and paths.
type Matcher interface {
Match(string) bool
}
// Matchers is an aggregate Matcher. A value will
// match if any of the sequence of Matchers returns true.
type Matchers []Matcher
func (ms Matchers) Match(v string) bool {
for _, m := range ms {
if m.Match(v) {
return true
}
}
return false
}
// ParsePatterns parses a sequence of globs for matching values. The returned
// Matcher will match values if at least one of the globs matched. If patterns
// is empty, then the returned Matcher won't match anything.
func ParsePatterns(patterns ...string) (Matcher, error) {
var ms Matchers
for _, p := range patterns {
g, err := glob.Compile(p, os.PathSeparator)
if err != nil {
return nil, err
}
ms = append(ms, g)
}
return ms, nil
}