-
Notifications
You must be signed in to change notification settings - Fork 0
/
constraint.go
59 lines (45 loc) · 1.15 KB
/
constraint.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
package main
import (
"fmt"
"strings"
)
type Constraint interface {
Evaluate(filename string, b []byte) (bool, string)
}
type MultiConstraint struct {
Constraints []Constraint
}
func (mc *MultiConstraint) Evaluate(filename string, b []byte) (bool, string) {
for _, c := range mc.Constraints {
isGood, message := c.Evaluate(filename, b)
if !isGood {
return false, message
}
}
return true, ""
}
type MustNotBeEmpty struct{}
func (_ *MustNotBeEmpty) Evaluate(filename string, b []byte) (bool, string) {
if len(b) == 0 {
return false, fmt.Sprintf("empty: %s", filename)
}
return true, ""
}
type Contains struct {
Value string `yaml:"value"`
}
func (c *Contains) Evaluate(filename string, b []byte) (bool, string) {
if strings.Contains(string(b), c.Value) {
return true, ""
}
return false, fmt.Sprintf("(%s) does not contain text: %s", filename, c.Value)
}
type MustNotContain struct {
Value string `yaml:"value"`
}
func (mnc *MustNotContain) Evaluate(filename string, b []byte) (bool, string) {
if strings.Contains(string(b), mnc.Value) {
return false, fmt.Sprintf("(%s) contains text: %s", filename, mnc.Value)
}
return true, ""
}