diff --git a/main.go b/main.go index fa0d289..14df454 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "flag" "fmt" + "github.com/yannh/kubeconform/pkg/output" "io" "io/ioutil" "log" @@ -74,13 +75,24 @@ func (i *arrayFiles) Set(value string) error { func realMain() int { var files, dirs arrayFiles - var skipKinds, k8sVersion string + var skipKinds, k8sVersion, outputFormat string flag.Var(&files, "file", "file to validate (can be specified multiple times)") flag.Var(&dirs, "dir", "directory to validate (can be specified multiple times)") flag.StringVar(&k8sVersion, "k8sversion", "1.18.0", "version of Kubernetes to test against") flag.StringVar(&skipKinds, "skipKinds", "", "comma-separated list of kinds to ignore") + flag.StringVar(&outputFormat, "output", "text", "output format - text, json") flag.Parse() + var o output.Output + switch { + case outputFormat == "text": + o = output.NewTextOutput() + case outputFormat == "json": + o = output.NewJSONOutput() + default: + log.Fatalf("-output must be text or json") + } + filter := func(signature resource.Signature) bool { kinds := strings.Split(skipKinds, ",") for _, kind := range kinds { @@ -119,27 +131,14 @@ func realMain() int { res := validateFile(f, []*registry.KubernetesRegistry{r}, k8sVersion, filter) f.Close() + + for _, resourceValidation := range res { - if resourceValidation.skipped { - log.Printf("skipping resource\n") - continue - } - - if resourceValidation.err != nil { - if _, ok := resourceValidation.err.(validator.InvalidResourceError); ok { - log.Printf("invalid resource: %s\n", resourceValidation.err) - } else { - log.Printf("failed validating resource in file %s: %s\n", filename, resourceValidation.err) - } - continue - } - - if resourceValidation.err == nil && !resourceValidation.skipped{ - log.Printf("file %s is valid\n", filename) - } + o.Write(filename, resourceValidation.err, resourceValidation.skipped) } } } + o.Flush() return 0 } diff --git a/pkg/output/json.go b/pkg/output/json.go new file mode 100644 index 0000000..dabacd3 --- /dev/null +++ b/pkg/output/json.go @@ -0,0 +1,45 @@ +package output + +import ( + "encoding/json" + "fmt" +) + +type result struct { + Filename string `json:"filename"` + Status string `json:"status"` + Msg string `json:"msg"` +} + +type JSONOutput struct { + results []result +} + +func NewJSONOutput() Output{ + return &JSONOutput{ + results: []result{}, + } +} + +func (o *JSONOutput) Write(filename string,err error, skipped bool) { + status := "VALID" + msg := "" + if err != nil { + status = "ERROR" + msg = err.Error() + } + if skipped { + status = "SKIPPED" + } + + o.results = append(o.results, result{Filename: filename, Status: status, Msg: msg}) +} + +func (o *JSONOutput) Flush() { + res, err := json.MarshalIndent(o.results,"", " ") + if err != nil { + fmt.Printf("error print results: %s", err) + return + } + fmt.Printf("%s\n", res) +} diff --git a/pkg/output/main.go b/pkg/output/main.go new file mode 100644 index 0000000..492dce9 --- /dev/null +++ b/pkg/output/main.go @@ -0,0 +1,7 @@ +package output + +type Output interface { + Write (filename string, err error, skipped bool) + Flush () +} + diff --git a/pkg/output/text.go b/pkg/output/text.go new file mode 100644 index 0000000..c80b88d --- /dev/null +++ b/pkg/output/text.go @@ -0,0 +1,37 @@ +package output + +import ( + "github.com/yannh/kubeconform/pkg/validator" + "log" +) + +type TextOutput struct { +} + +func NewTextOutput() Output { + return &TextOutput{} +} + +func (o *TextOutput) Write(filename string,err error, skipped bool) { + if skipped { + log.Printf("skipping resource\n") + return + } + + if err != nil { + if _, ok := err.(validator.InvalidResourceError); ok { + log.Printf("invalid resource: %s\n", err) + } else { + log.Printf("failed validating resource in file %s: %s\n", filename, err) + } + return + } + + if !skipped{ + log.Printf("file %s is valid\n", filename) + } +} + +func (o *TextOutput) Flush() { +} +