-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilter.go
95 lines (86 loc) · 1.74 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
package main
import (
"regexp"
"strings"
"time"
)
func Filter[T any](in <-chan T, filterFunctions ...func(T) bool) <-chan T {
out := make(chan T, cap(in))
go func() {
defer close(out)
ElementLoop:
for element := range in {
for _, want := range filterFunctions {
if !want(element) {
continue ElementLoop
}
}
out <- element
}
}()
return out
}
func CreatedBefore[T Dater](t time.Time) func(T) bool {
return func(resource T) bool {
if resource.CreatedAt().Before(t) {
return true
}
return false
}
}
func IDIsNot[T Identifier](ids ...string) func(T) bool {
return func(resource T) bool {
for i := range ids {
if resource.ID() == ids[i] {
return false
}
}
return true
}
}
func NameIsNot[T Namer](names ...string) func(T) bool {
return func(resource T) bool {
for i := range names {
if resource.Name() == names[i] {
return false
}
}
return true
}
}
func NameDoesNotContain[T Namer](substrings ...string) func(T) bool {
return func(resource T) bool {
for i := range substrings {
if strings.Contains(resource.Name(), substrings[i]) {
return false
}
}
return true
}
}
func NameMatchesOneOfThesePatterns[T Namer](regexps ...string) func(T) bool {
patterns := make([]*regexp.Regexp, len(regexps))
for i := range regexps {
patterns[i] = regexp.MustCompile(regexps[i])
}
return func(resource T) bool {
for i := range patterns {
if patterns[i].MatchString(resource.Name()) {
return true
}
}
return false
}
}
func TagsDoNotContain(tag string) func(Resource) bool {
return func(resource Resource) bool {
if tagger, ok := resource.(Tagger); ok {
for _, have := range tagger.Tags() {
if have == tag {
return false
}
}
}
return true
}
}