-
Notifications
You must be signed in to change notification settings - Fork 817
/
filter.go
40 lines (37 loc) · 1.08 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
package config
import (
"regexp"
"strings"
)
type filter struct {
Kind string // TODO: as we currently only support one GVK version, we use the kind only. But if we support multiple GVK, then GV need to be added
Namespace string
Name string
}
// ParseKinds parses the kinds if a single string contains comma separated kinds
// {"1,2,3","4","5"} => {"1","2","3","4","5"}
func parseKinds(list string) []filter {
resources := []filter{}
var resource filter
re := regexp.MustCompile(`\[([^\[\]]*)\]`)
submatchall := re.FindAllString(list, -1)
for _, element := range submatchall {
element = strings.Trim(element, "[")
element = strings.Trim(element, "]")
elements := strings.Split(element, ",")
if len(elements) == 0 {
continue
}
if len(elements) == 3 {
resource = filter{Kind: elements[0], Namespace: elements[1], Name: elements[2]}
}
if len(elements) == 2 {
resource = filter{Kind: elements[0], Namespace: elements[1]}
}
if len(elements) == 1 {
resource = filter{Kind: elements[0]}
}
resources = append(resources, resource)
}
return resources
}