-
Notifications
You must be signed in to change notification settings - Fork 0
/
matcher.go
50 lines (40 loc) · 998 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
45
46
47
48
49
50
package docker
import (
"regexp"
"strings"
log "github.com/sirupsen/logrus"
)
// Matcher allows to create any kind of matcher for container outputs
type Matcher func(l string) bool
// NewSubstringMatcher represents partial matcher
func NewSubstringMatcher(s string) Matcher {
return func(l string) bool {
ok := strings.Contains(l, s)
log.WithFields(log.Fields{
"kind": "substring",
"pattern": s,
"line": l,
"result": ok,
}).Trace("matching string")
return ok
}
}
// NewExactMatcher represents exact matcher i.e. the output should be
// exactly matched (except space chars around the word)
func NewExactMatcher(s string) Matcher {
return func(l string) bool {
ok := strings.TrimSpace(l) == s
log.WithFields(log.Fields{
"kind": "exact",
"pattern": s,
"line": l,
"result": ok,
}).Trace("matching string")
return ok
}
}
func NewRegexpMatcher(r *regexp.Regexp) Matcher {
return func(l string) bool {
return r.MatchString(l)
}
}