Skip to content

Commit b3c2ef0

Browse files
author
Michelle Noorali
committed
feat(*): add validation framework
1 parent ff872c6 commit b3c2ef0

File tree

3 files changed

+160
-10
lines changed

3 files changed

+160
-10
lines changed

lint.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func Lint(path string) {
8+
fmt.Println("coming soon")
9+
}

main.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,21 @@ import (
66
"os"
77
)
88

9+
const version = "0.0.1"
10+
911
func main() {
1012
app := cli.NewApp()
1113
app.Name = ("linter")
1214
app.Usage = "lint a helm chart"
13-
app.Action = func(c *cli.Context) {
14-
println("Happy Linting!")
15-
}
16-
15+
app.Version = version
1716
app.Run(os.Args)
1817

1918
app.Commands = []cli.Command{
2019
{
2120
Name: "lint",
2221
Usage: "applies linting to the chart path passed in",
2322
Action: func(c *cli.Context) {
24-
path := c.Args().First()
25-
lint(path)
23+
Lint(c.Args().First())
2624
},
2725
},
2826
{
@@ -59,10 +57,6 @@ func main() {
5957

6058
}
6159

62-
func lint(path string) {
63-
fmt.Println("coming soon")
64-
}
65-
6660
func addRules(path string) {
6761
fmt.Println("coming soon")
6862
}

validation/validation.go

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package validation
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
)
7+
8+
// ChartValidation represents a specific instance of validation against a specific directory.
9+
type ChartValidation struct {
10+
Path string
11+
Validations []*Validation
12+
ErrorCount int
13+
WarningCount int
14+
}
15+
16+
const (
17+
warningLevel = 1
18+
errorLevel = 2
19+
)
20+
21+
// Validation represents a single validation of a ChartValidation.
22+
type Validation struct {
23+
children []*Validation
24+
path string
25+
validator Validator
26+
Message string
27+
level int
28+
}
29+
30+
// Validator is a declared function that returns the result of a Validation.
31+
type Validator func(path string, v *Validation) (result bool)
32+
33+
func (cv *ChartValidation) addValidator(v *Validation) {
34+
cv.Validations = append(cv.Validations, v)
35+
}
36+
37+
func (v *Validation) addValidator(child *Validation) {
38+
v.children = append(v.children, child)
39+
}
40+
41+
// AddError adds error level validation to a ChartValidation.
42+
func (cv *ChartValidation) AddError(message string, fn Validator) *Validation {
43+
v := new(Validation)
44+
v.Message = message
45+
v.validator = fn
46+
v.level = errorLevel
47+
v.path = cv.Path
48+
49+
cv.addValidator(v)
50+
51+
return v
52+
}
53+
54+
// AddWarning adds a warning level validation to a ChartValidation
55+
func (cv *ChartValidation) AddWarning(message string, fn Validator) *Validation {
56+
v := new(Validation)
57+
v.Message = message
58+
v.validator = fn
59+
v.level = warningLevel
60+
v.path = cv.Path
61+
62+
cv.addValidator(v)
63+
64+
return v
65+
}
66+
67+
// AddError adds an error level validation to a Validation.
68+
func (v *Validation) AddError(message string, fn Validator) *Validation {
69+
child := new(Validation)
70+
child.Message = message
71+
child.validator = fn
72+
child.level = errorLevel
73+
child.path = v.path
74+
75+
v.addValidator(child)
76+
77+
return child
78+
}
79+
80+
// AddWarning adds a warning level validation to a Validation.
81+
func (v *Validation) AddWarning(message string, fn Validator) *Validation {
82+
child := new(Validation)
83+
child.Message = message
84+
child.validator = fn
85+
child.level = warningLevel
86+
child.path = v.path
87+
88+
v.addValidator(child)
89+
90+
return child
91+
}
92+
93+
// ChartName returns the name of the chart directory.
94+
func (cv *ChartValidation) ChartName() string {
95+
return filepath.Base(cv.Path)
96+
}
97+
98+
func (v *Validation) valid() bool {
99+
return v.validator(v.path, v)
100+
}
101+
102+
func (v *Validation) walk(talker func(*Validation) bool) {
103+
validResult := talker(v)
104+
105+
if validResult {
106+
for _, child := range v.children {
107+
child.walk(talker)
108+
}
109+
}
110+
}
111+
112+
func (cv *ChartValidation) walk(talker func(v *Validation) bool) {
113+
for _, v := range cv.Validations {
114+
v.walk(talker)
115+
}
116+
}
117+
118+
// Valid returns true if every validation passes.
119+
func (cv *ChartValidation) Valid() bool {
120+
var valid bool = true
121+
122+
fmt.Printf("\nVerifying %s chart is a valid chart...\n", cv.ChartName())
123+
cv.walk(func(v *Validation) bool {
124+
v.path = cv.Path
125+
vv := v.valid()
126+
if !vv {
127+
switch v.level {
128+
case 2:
129+
cv.ErrorCount = cv.ErrorCount + 1
130+
msg := v.Message + " : " + strconv.FormatBool(vv)
131+
fmt.Println(msg)
132+
case 1:
133+
cv.WarningCount = cv.WarningCount + 1
134+
msg := v.Message + " : " + strconv.FormatBool(vv)
135+
fmt.Println(msg)
136+
}
137+
} else {
138+
msg := v.Message + " : " + strconv.FormatBool(vv)
139+
fmt.Println(msg)
140+
}
141+
142+
valid = valid && vv
143+
return valid
144+
})
145+
146+
return valid
147+
}

0 commit comments

Comments
 (0)