/
filter.go
155 lines (122 loc) · 2.96 KB
/
filter.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package filter
import (
"fmt"
"strings"
"github.com/choria-io/go-choria/filter/facts"
"github.com/choria-io/go-choria/protocol"
)
// Filter is a function that represents a specific filter in string form
type Filter func(f *protocol.Filter) error
// AgentFilter processes a series of strings as agent filters
func AgentFilter(f ...string) Filter {
return func(pf *protocol.Filter) (err error) {
for _, filter := range f {
if filter == "" {
continue
}
pf.AddAgentFilter(filter)
}
return
}
}
// ClassFilter processes a series of strings as agent filters
func ClassFilter(f ...string) Filter {
return func(pf *protocol.Filter) (err error) {
for _, filter := range f {
if filter == "" {
continue
}
pf.AddClassFilter(filter)
}
return
}
}
// IdentityFilter processes a series of strings as identity filters
func IdentityFilter(f ...string) Filter {
return func(pf *protocol.Filter) (err error) {
for _, filter := range f {
if filter == "" {
continue
}
pf.AddIdentityFilter(filter)
}
return
}
}
// CompoundFilter processes a series of strings as compound filters
func CompoundFilter(f ...string) Filter {
return func(pf *protocol.Filter) (err error) {
for _, filter := range f {
if filter == "" {
continue
}
pf.AddCompoundFilter(filter)
}
return
}
}
// FactFilter processes a series of strings as fact filters
func FactFilter(f ...string) Filter {
return func(pf *protocol.Filter) (err error) {
for _, filter := range f {
if filter == "" {
continue
}
ff, err := ParseFactFilterString(filter)
if err != nil {
return err
}
err = pf.AddFactFilter(ff.Fact, ff.Operator, ff.Value)
if err != nil {
return err
}
}
return
}
}
// CombinedFilter processes a series of strings as combined fact and class filters
func CombinedFilter(f ...string) Filter {
return func(pf *protocol.Filter) (err error) {
for _, filter := range f {
parts := strings.Split(filter, " ")
for _, part := range parts {
if part == "" {
continue
}
ff, err := ParseFactFilterString(part)
if err != nil {
pf.AddClassFilter(part)
continue
}
pf.AddFactFilter(ff.Fact, ff.Operator, ff.Value)
}
}
return
}
}
// ParseFactFilterString parses a fact filter string as typically typed on the CLI
func ParseFactFilterString(f string) (pf *protocol.FactFilter, err error) {
pf = &protocol.FactFilter{}
ff, err := facts.ParseFactFilterString(f)
if err != nil {
return nil, err
}
pf.Fact = ff[0]
pf.Operator = ff[1]
pf.Value = ff[2]
if pf.Fact == "" || pf.Operator == "" || pf.Value == "" {
return nil, fmt.Errorf("could not parse fact %s it does not appear to be in a valid format", f)
}
return
}
// NewFilter creates a new filter based on the supplied string representations
func NewFilter(fs ...Filter) (f *protocol.Filter, err error) {
f = protocol.NewFilter()
for _, filter := range fs {
err = filter(f)
if err != nil {
return
}
}
return
}