From ea8ecafa3814953e00f04d067ccb14eec89b8469 Mon Sep 17 00:00:00 2001 From: Yann Hamon Date: Sat, 31 Oct 2020 14:29:13 +0100 Subject: [PATCH] refactor output --- cmd/kubeconform/main.go | 15 +-------------- pkg/output/json.go | 22 +++++++++++----------- pkg/output/json_test.go | 4 ++-- pkg/output/output.go | 35 +++++++++++++++++++++++++---------- pkg/output/text.go | 22 +++++++++++----------- pkg/output/text_test.go | 2 +- 6 files changed, 51 insertions(+), 49 deletions(-) diff --git a/cmd/kubeconform/main.go b/cmd/kubeconform/main.go index 782034d..d527cc2 100644 --- a/cmd/kubeconform/main.go +++ b/cmd/kubeconform/main.go @@ -135,19 +135,6 @@ func (ap *arrayParam) Set(value string) error { return nil } -func getLogger(outputFormat string, printSummary, isStdin, verbose bool) (output.Output, error) { - w := os.Stdout - - switch { - case outputFormat == "text": - return output.Text(w, printSummary, isStdin, verbose), nil - case outputFormat == "json": - return output.JSON(w, printSummary, isStdin, verbose), nil - default: - return nil, fmt.Errorf("-output must be text or json") - } -} - func skipKindsMap(skipKindsCSV string) map[string]bool { splitKinds := strings.Split(skipKindsCSV, ",") skipKinds := map[string]bool{} @@ -289,7 +276,7 @@ func realMain() int { }() var o output.Output - if o, err = getLogger(outputFormat, summary, isStdin, verbose); err != nil { + if o, err = output.New(outputFormat, summary, isStdin, verbose); err != nil { fmt.Fprintln(os.Stderr, err) return 1 } diff --git a/pkg/output/json.go b/pkg/output/json.go index cbf89fc..0654b14 100644 --- a/pkg/output/json.go +++ b/pkg/output/json.go @@ -24,7 +24,7 @@ type jsono struct { } // JSON will output the results of the validation as a JSON -func JSON(w io.Writer, withSummary bool, isStdin, verbose bool) Output { +func jsonOutput(w io.Writer, withSummary bool, isStdin, verbose bool) Output { return &jsono{ w: w, withSummary: withSummary, @@ -44,24 +44,24 @@ func (o *jsono) Write(filename, kind, name, version string, err error, skipped b s := status(kind, name, err, skipped) switch s { - case VALID: - st = "VALID" + case statusValid: + st = "statusValid" o.nValid++ - case INVALID: - st = "INVALID" + case statusInvalid: + st = "statusInvalid" msg = err.Error() o.nInvalid++ - case ERROR: - st = "ERROR" + case statusError: + st = "statusError" msg = err.Error() o.nErrors++ - case SKIPPED: - st = "SKIPPED" + case statusSkipped: + st = "statusSkipped" o.nSkipped++ - case EMPTY: + case statusEmpty: } - if o.verbose || (s != VALID && s != SKIPPED && s != EMPTY) { + if o.verbose || (s != statusValid && s != statusSkipped && s != statusEmpty) { o.results = append(o.results, result{Filename: filename, Kind: kind, Name: name, Version: version, Status: st, Msg: msg}) } diff --git a/pkg/output/json_test.go b/pkg/output/json_test.go index 4d0006f..f14c912 100644 --- a/pkg/output/json_test.go +++ b/pkg/output/json_test.go @@ -85,7 +85,7 @@ func TestJSONWrite(t *testing.T) { "kind": "Deployment", "name": "my-app", "version": "apps/v1", - "status": "VALID", + "status": "statusValid", "msg": "" } ], @@ -100,7 +100,7 @@ func TestJSONWrite(t *testing.T) { }, } { w := new(bytes.Buffer) - o := JSON(w, testCase.withSummary, false, testCase.verbose) + o := jsonOutput(w, testCase.withSummary, false, testCase.verbose) for _, res := range testCase.res { o.Write(res.fileName, res.kind, res.name, res.version, res.err, res.skipped) diff --git a/pkg/output/output.go b/pkg/output/output.go index d5c0a4f..5e0e58e 100644 --- a/pkg/output/output.go +++ b/pkg/output/output.go @@ -1,16 +1,18 @@ package output import ( + "fmt" "github.com/yannh/kubeconform/pkg/validator" + "os" ) const ( _ = iota - VALID - INVALID - ERROR - SKIPPED - EMPTY + statusValid + statusInvalid + statusError + statusSkipped + statusEmpty ) type Output interface { @@ -18,21 +20,34 @@ type Output interface { Flush() error } +func New(outputFormat string, printSummary, isStdin, verbose bool) (Output, error) { + w := os.Stdout + + switch { + case outputFormat == "text": + return textOutput(w, printSummary, isStdin, verbose), nil + case outputFormat == "json": + return jsonOutput(w, printSummary, isStdin, verbose), nil + default: + return nil, fmt.Errorf("`outputFormat` must be 'text' or 'json'") + } +} + func status(kind, name string, err error, skipped bool) int { if name == "" && kind == "" && err == nil && skipped == false { - return EMPTY + return statusEmpty } if skipped { - return SKIPPED + return statusSkipped } if err != nil { if _, ok := err.(validator.InvalidResourceError); ok { - return INVALID + return statusInvalid } - return ERROR + return statusError } - return VALID + return statusValid } diff --git a/pkg/output/text.go b/pkg/output/text.go index b232821..cdf462b 100644 --- a/pkg/output/text.go +++ b/pkg/output/text.go @@ -6,7 +6,7 @@ import ( "sync" ) -type text struct { +type texto struct { sync.Mutex w io.Writer withSummary bool @@ -16,9 +16,9 @@ type text struct { nValid, nInvalid, nErrors, nSkipped int } -// Text will output the results of the validation as a text -func Text(w io.Writer, withSummary, isStdin, verbose bool) Output { - return &text{ +// Text will output the results of the validation as a texto +func textOutput(w io.Writer, withSummary, isStdin, verbose bool) Output { + return &texto{ w: w, withSummary: withSummary, isStdin: isStdin, @@ -31,7 +31,7 @@ func Text(w io.Writer, withSummary, isStdin, verbose bool) Output { } } -func (o *text) Write(filename, kind, name, version string, reserr error, skipped bool) error { +func (o *texto) Write(filename, kind, name, version string, reserr error, skipped bool) error { o.Lock() defer o.Unlock() @@ -39,33 +39,33 @@ func (o *text) Write(filename, kind, name, version string, reserr error, skipped o.files[filename] = true switch status(kind, name, reserr, skipped) { - case VALID: + case statusValid: if o.verbose { _, err = fmt.Fprintf(o.w, "%s - %s %s is valid\n", filename, kind, name) } o.nValid++ - case INVALID: + case statusInvalid: _, err = fmt.Fprintf(o.w, "%s - %s %s is invalid: %s\n", filename, kind, name, reserr) o.nInvalid++ - case ERROR: + case statusError: if kind != "" && name != "" { _, err = fmt.Fprintf(o.w, "%s - %s %s failed validation: %s\n", filename, kind, name, reserr) } else { _, err = fmt.Fprintf(o.w, "%s - failed validation: %s\n", filename, reserr) } o.nErrors++ - case SKIPPED: + case statusSkipped: if o.verbose { _, err = fmt.Fprintf(o.w, "%s - %s %s skipped\n", filename, name, kind) } o.nSkipped++ - case EMPTY: // sent to ensure we count the filename as parsed + case statusEmpty: // sent to ensure we count the filename as parsed } return err } -func (o *text) Flush() error { +func (o *texto) Flush() error { var err error if o.withSummary { nFiles := len(o.files) diff --git a/pkg/output/text_test.go b/pkg/output/text_test.go index 14f35ce..ba9d2ea 100644 --- a/pkg/output/text_test.go +++ b/pkg/output/text_test.go @@ -72,7 +72,7 @@ Summary: 1 resource found in 1 file - Valid: 1, Invalid: 0, Errors: 0 Skipped: 0 }, } { w := new(bytes.Buffer) - o := Text(w, testCase.withSummary, false, testCase.verbose) + o := textOutput(w, testCase.withSummary, false, testCase.verbose) for _, res := range testCase.res { o.Write(res.fileName, res.kind, res.name, res.version, res.err, res.skipped)