-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
126 lines (100 loc) · 2.42 KB
/
main.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
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"context"
"log"
"os"
"github.com/brendanjryan/ccheck/pkg"
"github.com/fatih/color"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Name = "ccheck"
app.Usage = "ccheck <files>"
app.Author = "Brendan Ryan"
app.Description = "A command line utility for validating structured config files"
as := args{}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "p",
Value: "policies",
Usage: "directory which policy definitions live in",
Destination: &as.policyDir,
},
cli.StringFlag{
Name: "n",
Value: "main",
Usage: "namespace of rules",
Destination: &as.namespace,
},
cli.BoolFlag{
Name: "s",
Usage: "whether or not strict mode is enabled",
Destination: &as.strict,
},
}
app.Action = func(c *cli.Context) error {
as.configs = c.Args()
err := confCheck(as)
if err != nil {
log.Fatal(cli.NewExitError("error: "+err.Error(), 1))
}
return nil
}
err := app.Run(os.Args)
if err != nil {
log.Fatal("error creating CLI application: ", err)
}
return
}
func confCheck(as args) error {
ctx := context.Background()
cc := pkg.NewConfChecker(as.namespace, as.policyDir, as.configs)
cr, err := cc.Run(ctx)
if err != nil {
log.Println("error running checker: ", err)
return err
}
p := printer{}
for f, res := range cr {
if len(res.Warnings) == 0 && len(res.Failures) == 0 {
p.ok(f)
}
for _, w := range res.Warnings {
if as.strict {
p.err(f, w)
continue
}
p.warning(f, w)
}
for _, fa := range res.Failures {
// trap an error just so we exit with the right code
err = fa
p.err(f, fa)
}
}
return nil
}
// args represents all command line arguments supported by this script
type args struct {
// the directory which policy files live in
policyDir string
// the namespace rules live in:
// https://www.openpolicyagent.org/docs/latest/how-do-i-write-policies#packages
namespace string
// whether or not strict mode is enabled
strict bool
// a list of config files we will check
configs []string
}
// printer controlls printing the results of this script in a formatted manner.
type printer struct{}
func (p printer) err(file string, err error) {
color.Red("Failure: %s - %s", file, err)
}
func (p printer) warning(file string, err error) {
color.Yellow("Warning: %s - %s", file, err)
}
func (p printer) ok(file string) {
color.Green("Passed: %s", file)
}