From bb478da7e0da7244d7b3189d27a0d651d80858a6 Mon Sep 17 00:00:00 2001 From: Yann Hamon Date: Sat, 30 May 2020 20:03:27 +0200 Subject: [PATCH] enable printing a summary at the end of the run --- main.go | 8 ++++---- pkg/output/json.go | 44 +++++++++++++++++++++++++++++++++++++++++--- pkg/output/text.go | 22 +++++++++++++++------- 3 files changed, 60 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 6fb8be6..0275235 100644 --- a/main.go +++ b/main.go @@ -58,8 +58,6 @@ func validateFile(f io.Reader, regs []registry.Registry, k8sVersion string, skip return []validationResult{{err: nil}} } - - type arrayFiles []string func (i *arrayFiles) String() string { @@ -76,6 +74,8 @@ func (i *arrayFiles) Set(value string) error { func realMain() int { var files, dirs, schemas arrayFiles var skipKinds, k8sVersion, outputFormat string + var printSummary bool + flag.BoolVar(&printSummary, "printsummary", false, "print a summary at the end") 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.Var(&schemas, "schema", "file containing an additional Schema") @@ -87,9 +87,9 @@ func realMain() int { var o output.Output switch { case outputFormat == "text": - o = output.NewTextOutput() + o = output.NewTextOutput(printSummary) case outputFormat == "json": - o = output.NewJSONOutput() + o = output.NewJSONOutput(printSummary) default: log.Fatalf("-output must be text or json") } diff --git a/pkg/output/json.go b/pkg/output/json.go index dabacd3..5c74dbf 100644 --- a/pkg/output/json.go +++ b/pkg/output/json.go @@ -12,11 +12,13 @@ type result struct { } type JSONOutput struct { + withSummary bool results []result } -func NewJSONOutput() Output{ +func NewJSONOutput(withSummary bool) Output{ return &JSONOutput{ + withSummary: withSummary, results: []result{}, } } @@ -25,7 +27,7 @@ func (o *JSONOutput) Write(filename string,err error, skipped bool) { status := "VALID" msg := "" if err != nil { - status = "ERROR" + status = "INVALID" msg = err.Error() } if skipped { @@ -36,7 +38,43 @@ func (o *JSONOutput) Write(filename string,err error, skipped bool) { } func (o *JSONOutput) Flush() { - res, err := json.MarshalIndent(o.results,"", " ") + var err error + var res []byte + + if o.withSummary { + jsonObj := struct { + Resources []result `json:"resources"` + Summary struct { + Valid int `json:"valid"` + Invalid int `json:"invalid"` + Skipped int `json:"skipped"` + } `json:"summary"` + } { + Resources: o.results, + } + + for _, r := range o.results { + switch { + case r.Status == "VALID": + jsonObj.Summary.Valid++ + case r.Status == "INVALID": + jsonObj.Summary.Invalid++ + case r.Status == "SKIPPED": + jsonObj.Summary.Skipped++ + } + } + + res, err = json.MarshalIndent(jsonObj,"", " ") + } else { + jsonObj := struct { + Resources []result + } { + Resources: o.results, + } + + res, err = json.MarshalIndent(jsonObj,"", " ") + } + if err != nil { fmt.Printf("error print results: %s", err) return diff --git a/pkg/output/text.go b/pkg/output/text.go index c80b88d..bc21d59 100644 --- a/pkg/output/text.go +++ b/pkg/output/text.go @@ -1,37 +1,45 @@ package output import ( + "fmt" "github.com/yannh/kubeconform/pkg/validator" - "log" ) type TextOutput struct { + withSummary bool + nValid, nInvalid, nSkipped int } -func NewTextOutput() Output { - return &TextOutput{} +func NewTextOutput(withSummary bool) Output { + return &TextOutput{withSummary, 0,0,0} } func (o *TextOutput) Write(filename string,err error, skipped bool) { if skipped { - log.Printf("skipping resource\n") + fmt.Printf("skipping resource\n") + o.nSkipped++ return } if err != nil { + o.nInvalid++ if _, ok := err.(validator.InvalidResourceError); ok { - log.Printf("invalid resource: %s\n", err) + fmt.Printf("invalid resource: %s\n", err) } else { - log.Printf("failed validating resource in file %s: %s\n", filename, err) + fmt.Printf("failed validating resource in file %s: %s\n", filename, err) } return } if !skipped{ - log.Printf("file %s is valid\n", filename) + fmt.Printf("file %s is valid\n", filename) + o.nValid++ } } func (o *TextOutput) Flush() { + if o.withSummary { + fmt.Printf("Run summary - Valid: %d, Invalid: %d, Skipped: %d\n", o.nValid, o.nInvalid, o.nSkipped) + } }