From 9b129d021ae8dcddf0a97ff4e90f2b15919a32a8 Mon Sep 17 00:00:00 2001 From: Yann Hamon Date: Mon, 1 Jun 2020 17:15:52 +0200 Subject: [PATCH] loggers take an io.writer --- main.go | 6 ++++-- main_test.go | 18 +++++++++--------- pkg/output/json.go | 9 ++++++--- pkg/output/text.go | 15 +++++++++------ 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/main.go b/main.go index 2f19dc4..15d63a3 100644 --- a/main.go +++ b/main.go @@ -127,11 +127,13 @@ func (ap *arrayParam) Set(value string) error { } func getLogger(outputFormat string, printSummary, verbose bool) (output.Output, error) { + w := os.Stdout + switch { case outputFormat == "text": - return output.Text(printSummary, verbose), nil + return output.Text(w, printSummary, verbose), nil case outputFormat == "json": - return output.JSON(printSummary, verbose), nil + return output.JSON(w, printSummary, verbose), nil default: return nil, fmt.Errorf("-output must be text or json") } diff --git a/main_test.go b/main_test.go index c5898d3..91c0ee5 100644 --- a/main_test.go +++ b/main_test.go @@ -7,28 +7,28 @@ import ( func TestSkipKindMaps(t *testing.T) { for _, testCase := range []struct { - name string + name string csvSkipKinds string - expect map[string]bool - } { + expect map[string]bool + }{ { "nothing to skip", "", - map[string]bool {}, + map[string]bool{}, }, { "a single kind to skip", "somekind", - map[string]bool { + map[string]bool{ "somekind": true, }, }, { "multiple kinds to skip", "somekind,anotherkind,yetsomeotherkind", - map[string]bool { - "somekind": true, - "anotherkind": true, + map[string]bool{ + "somekind": true, + "anotherkind": true, "yetsomeotherkind": true, }, }, @@ -38,4 +38,4 @@ func TestSkipKindMaps(t *testing.T) { t.Errorf("%s - got %+v, expected %+v", testCase.name, got, testCase.expect) } } -} \ No newline at end of file +} diff --git a/pkg/output/json.go b/pkg/output/json.go index 1545431..a5ee196 100644 --- a/pkg/output/json.go +++ b/pkg/output/json.go @@ -3,6 +3,7 @@ package output import ( "encoding/json" "fmt" + "io" ) type result struct { @@ -14,14 +15,16 @@ type result struct { } type jsono struct { + w io.Writer withSummary bool verbose bool results []result nValid, nInvalid, nErrors, nSkipped int } -func JSON(withSummary bool, quiet bool) Output { +func JSON(w io.Writer, withSummary bool, quiet bool) Output { return &jsono{ + w: w, withSummary: withSummary, verbose: quiet, results: []result{}, @@ -99,8 +102,8 @@ func (o *jsono) Flush() { } if err != nil { - fmt.Printf("error print results: %s", err) + fmt.Fprintf(o.w, "error print results: %s", err) return } - fmt.Printf("%s\n", res) + fmt.Fprintf(o.w, "%s\n", res) } diff --git a/pkg/output/text.go b/pkg/output/text.go index bf4a78d..6f1f7fc 100644 --- a/pkg/output/text.go +++ b/pkg/output/text.go @@ -2,18 +2,21 @@ package output import ( "fmt" + "io" "sync" ) type text struct { sync.Mutex + w io.Writer withSummary bool verbose bool nValid, nInvalid, nErrors, nSkipped int } -func Text(withSummary, verbose bool) Output { +func Text(w io.Writer, withSummary, verbose bool) Output { return &text{ + w: w, withSummary: withSummary, verbose: verbose, nValid: 0, @@ -30,18 +33,18 @@ func (o *text) Write(filename, kind, version string, err error, skipped bool) { switch status(err, skipped) { case VALID: if !o.verbose { - fmt.Printf("%s - %s is valid\n", filename, kind) + fmt.Fprintf(o.w, "%s - %s is valid\n", filename, kind) } o.nValid++ case INVALID: - fmt.Printf("%s - %s is invalid: %s\n", filename, kind, err) + fmt.Fprintf(o.w, "%s - %s is invalid: %s\n", filename, kind, err) o.nInvalid++ case ERROR: - fmt.Printf("%s - %s failed validation: %s\n", filename, kind, err) + fmt.Fprintf(o.w, "%s - %s failed validation: %s\n", filename, kind, err) o.nErrors++ case SKIPPED: if o.verbose { - fmt.Printf("%s - %s skipped\n", filename, kind) + fmt.Fprintf(o.w, "%s - %s skipped\n", filename, kind) } o.nSkipped++ } @@ -49,6 +52,6 @@ func (o *text) Write(filename, kind, version string, err error, skipped bool) { func (o *text) Flush() { if o.withSummary { - fmt.Printf("Run summary - Valid: %d, Invalid: %d, Errors: %d Skipped: %d\n", o.nValid, o.nInvalid, o.nErrors, o.nSkipped) + fmt.Fprintf(o.w, "Run summary - Valid: %d, Invalid: %d, Errors: %d Skipped: %d\n", o.nValid, o.nInvalid, o.nErrors, o.nSkipped) } }