-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpatternfilter.go
More file actions
43 lines (37 loc) · 918 Bytes
/
patternfilter.go
File metadata and controls
43 lines (37 loc) · 918 Bytes
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
package nettrigger
import (
"errors"
"fmt"
"github.com/gobwas/glob"
)
type patternFilter struct {
subject string
pattern glob.Glob
}
func (f patternFilter) Filter(env Environment) bool {
return f.pattern.Match(env.Expand(f.subject))
}
// PatternBuilder constructs pattern filters from filter specifications.
func PatternBuilder(spec FilterSpec) (Filter, error) {
switch spec.Type {
case "pattern", "pat":
default:
return nil, nil
}
switch len(spec.Args) {
case 0, 1:
return nil, errors.New("pattern filter requires a subject and a pattern")
case 2:
subject, pattern := spec.Args[0], spec.Args[1]
g, err := glob.Compile(pattern)
if err != nil {
return nil, fmt.Errorf("invalid pattern \"%s\": %v", pattern, err)
}
return patternFilter{
subject: subject,
pattern: g,
}.Filter, nil
default:
return nil, errors.New("pattern filter has %d arguments when two are needed")
}
}