/
utils.go
75 lines (65 loc) · 1.55 KB
/
utils.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
package minion
import (
"fmt"
"regexp"
"strings"
)
func (s *Service) IsGroupAllowed(groupName string) bool {
isAllowed := false
for _, regex := range s.AllowedGroupIDsExpr {
if regex.MatchString(groupName) {
isAllowed = true
break
}
}
for _, regex := range s.IgnoredGroupIDsExpr {
if regex.MatchString(groupName) {
isAllowed = false
break
}
}
return isAllowed
}
func (s *Service) IsTopicAllowed(topicName string) bool {
isAllowed := false
for _, regex := range s.AllowedTopicsExpr {
if regex.MatchString(topicName) {
isAllowed = true
break
}
}
for _, regex := range s.IgnoredTopicsExpr {
if regex.MatchString(topicName) {
isAllowed = false
break
}
}
return isAllowed
}
func compileRegex(expr string) (*regexp.Regexp, error) {
if strings.HasPrefix(expr, "/") && strings.HasSuffix(expr, "/") {
substr := expr[1 : len(expr)-1]
regex, err := regexp.Compile(substr)
if err != nil {
return nil, err
}
return regex, nil
}
// If this is no regex input (which is marked by the slashes around it) then we escape it so that it's a literal
regex, err := regexp.Compile("^" + expr + "$")
if err != nil {
return nil, err
}
return regex, nil
}
func compileRegexes(expr []string) ([]*regexp.Regexp, error) {
compiledExpressions := make([]*regexp.Regexp, len(expr))
for i, exprStr := range expr {
expr, err := compileRegex(exprStr)
if err != nil {
return nil, fmt.Errorf("failed to compile expression string '%v': %w", exprStr, err)
}
compiledExpressions[i] = expr
}
return compiledExpressions, nil
}