@@ -9,12 +9,16 @@ import (
9
9
)
10
10
11
11
// Filter returns all elements of the list that match at least one expression
12
+ // and are not ignored
12
13
func Filter (list manifest.List , exprs Matchers ) manifest.List {
13
14
out := make (manifest.List , 0 , len (list ))
14
15
for _ , m := range list {
15
16
if ! exprs .MatchString (m .KindName ()) {
16
17
continue
17
18
}
19
+ if exprs .IgnoreString (m .KindName ()) {
20
+ continue
21
+ }
18
22
out = append (out , m )
19
23
}
20
24
return out
@@ -26,7 +30,13 @@ type Matcher interface {
26
30
MatchString (string ) bool
27
31
}
28
32
33
+ // Ignorer is like matcher, but for explicitely ignoring resources
34
+ type Ignorer interface {
35
+ IgnoreString (string ) bool
36
+ }
37
+
29
38
// Matchers is a collection of multiple expressions.
39
+ // A matcher may also implement Ignorer to explicitely ignore fields
30
40
type Matchers []Matcher
31
41
32
42
// MatchString returns whether at least one expression (OR) matches the string
@@ -38,6 +48,18 @@ func (e Matchers) MatchString(s string) bool {
38
48
return b
39
49
}
40
50
51
+ func (e Matchers ) IgnoreString (s string ) bool {
52
+ b := false
53
+ for _ , exp := range e {
54
+ i , ok := exp .(Ignorer )
55
+ if ! ok {
56
+ continue
57
+ }
58
+ b = b || i .IgnoreString (s )
59
+ }
60
+ return b
61
+ }
62
+
41
63
// RegExps is a helper to construct Matchers from regular expressions
42
64
func RegExps (rs []* regexp.Regexp ) Matchers {
43
65
xprs := make (Matchers , 0 , len (rs ))
@@ -50,11 +72,20 @@ func RegExps(rs []*regexp.Regexp) Matchers {
50
72
func StrExps (strs ... string ) (Matchers , error ) {
51
73
exps := make (Matchers , 0 , len (strs ))
52
74
for _ , raw := range strs {
53
- s := fmt .Sprintf (`(?i)^%s$` , raw )
75
+ // trim exlamation mark, not supported by regex
76
+ s := fmt .Sprintf (`(?i)^%s$` , strings .TrimPrefix (raw , "!" ))
77
+
78
+ // create regexp matcher
79
+ var exp Matcher
54
80
exp , err := regexp .Compile (s )
55
81
if err != nil {
56
82
return nil , ErrBadExpr {err }
57
83
}
84
+
85
+ // if negative (!), invert regex behaviour
86
+ if strings .HasPrefix (raw , "!" ) {
87
+ exp = NegMatcher {exp : exp }
88
+ }
58
89
exps = append (exps , exp )
59
90
}
60
91
return exps , nil
@@ -76,3 +107,16 @@ type ErrBadExpr struct {
76
107
func (e ErrBadExpr ) Error () string {
77
108
return fmt .Sprintf ("%s.\n See https://tanka.dev/output-filtering/#regular-expressions for details on regular expressions." , strings .Title (e .inner .Error ()))
78
109
}
110
+
111
+ // NexMatcher is a matcher that inverts the original behaviour
112
+ type NegMatcher struct {
113
+ exp Matcher
114
+ }
115
+
116
+ func (n NegMatcher ) MatchString (s string ) bool {
117
+ return true
118
+ }
119
+
120
+ func (n NegMatcher ) IgnoreString (s string ) bool {
121
+ return n .exp .MatchString (s )
122
+ }
0 commit comments