forked from rebuy-de/aws-nuke
/
filter.go
89 lines (71 loc) · 1.55 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
package config
import (
"fmt"
"regexp"
"strings"
"github.com/mb0/glob"
)
type FilterType string
const (
FilterTypeEmpty FilterType = ""
FilterTypeExact = "exact"
FilterTypeGlob = "glob"
FilterTypeRegex = "regex"
FilterTypeContains = "contains"
)
type Filters map[string][]Filter
func (f Filters) Merge(f2 Filters) {
for resourceType, filter := range f2 {
f[resourceType] = append(f[resourceType], filter...)
}
}
type Filter struct {
Property string
Type FilterType
Value string
Invert string
}
func (f Filter) Match(o string) (bool, error) {
switch f.Type {
case FilterTypeEmpty:
fallthrough
case FilterTypeExact:
return f.Value == o, nil
case FilterTypeContains:
return strings.Contains(o, string(f.Value)), nil
case FilterTypeGlob:
return glob.Match(string(f.Value), o)
case FilterTypeRegex:
re, err := regexp.Compile(string(f.Value))
if err != nil {
return false, err
}
return re.MatchString(o), nil
default:
return false, fmt.Errorf("unknown type %s", f.Type)
}
}
func (f *Filter) UnmarshalYAML(unmarshal func(interface{}) error) error {
var value string
if unmarshal(&value) == nil {
f.Type = FilterTypeExact
f.Value = value
return nil
}
m := map[string]string{}
err := unmarshal(m)
if err != nil {
return err
}
f.Type = FilterType(m["type"])
f.Value = m["value"]
f.Property = m["property"]
f.Invert = m["invert"]
return nil
}
func NewExactFilter(value string) Filter {
return Filter{
Type: FilterTypeExact,
Value: value,
}
}