This repository has been archived by the owner on Feb 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
expr.go
114 lines (98 loc) · 2.29 KB
/
expr.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
package filter
import (
"fmt"
"regexp"
"strings"
log "github.com/Sirupsen/logrus"
)
const (
// EQ is exported
EQ = iota
// NOTEQ is exported
NOTEQ
)
// OPERATORS is exported
var OPERATORS = []string{"==", "!="}
type expr struct {
key string
operator int
value string
isSoft bool
}
func parseExprs(env []string) ([]expr, error) {
exprs := []expr{}
for _, e := range env {
found := false
for i, op := range OPERATORS {
if strings.Contains(e, op) {
// split with the op
parts := strings.SplitN(e, op, 2)
// validate key
// allow alpha-numeric
matched, err := regexp.MatchString(`^(?i)[a-z_][a-z0-9\-_.]+$`, parts[0])
if err != nil {
return nil, err
}
if matched == false {
return nil, fmt.Errorf("Key '%s' is invalid", parts[0])
}
if len(parts) == 2 {
// validate value
// allow leading = in case of using ==
// allow * for globbing
// allow regexp
matched, err := regexp.MatchString(`^(?i)[=!\/]?(~)?[a-z0-9:\-_\s\.\*/\(\)\?\+\[\]\\\^\$\|]+$`, parts[1])
if err != nil {
return nil, err
}
if matched == false {
return nil, fmt.Errorf("Value '%s' is invalid", parts[1])
}
exprs = append(exprs, expr{key: strings.ToLower(parts[0]), operator: i, value: strings.TrimLeft(parts[1], "~"), isSoft: isSoft(parts[1])})
} else {
exprs = append(exprs, expr{key: strings.ToLower(parts[0]), operator: i})
}
found = true
break // found an op, move to next entry
}
}
if !found {
return nil, fmt.Errorf("One of operator ==, != is expected")
}
}
return exprs, nil
}
func (e *expr) Match(whats ...string) bool {
var (
pattern string
match bool
err error
)
if e.value[0] == '/' && e.value[len(e.value)-1] == '/' {
// regexp
pattern = e.value[1 : len(e.value)-1]
} else {
// simple match, create the regex for globbing (ex: ub*t* -> ^ub.*t.*$) and match.
pattern = "^" + strings.Replace(e.value, "*", ".*", -1) + "$"
}
for _, what := range whats {
if match, err = regexp.MatchString(pattern, what); match {
break
} else if err != nil {
log.Error(err)
}
}
switch e.operator {
case EQ:
return match
case NOTEQ:
return !match
}
return false
}
func isSoft(value string) bool {
if value[0] == '~' {
return true
}
return false
}