loggers take an io.writer

This commit is contained in:
Yann Hamon 2020-06-01 17:15:52 +02:00
parent 9f6965d10f
commit 9b129d021a
4 changed files with 28 additions and 20 deletions

View file

@ -127,11 +127,13 @@ func (ap *arrayParam) Set(value string) error {
} }
func getLogger(outputFormat string, printSummary, verbose bool) (output.Output, error) { func getLogger(outputFormat string, printSummary, verbose bool) (output.Output, error) {
w := os.Stdout
switch { switch {
case outputFormat == "text": case outputFormat == "text":
return output.Text(printSummary, verbose), nil return output.Text(w, printSummary, verbose), nil
case outputFormat == "json": case outputFormat == "json":
return output.JSON(printSummary, verbose), nil return output.JSON(w, printSummary, verbose), nil
default: default:
return nil, fmt.Errorf("-output must be text or json") return nil, fmt.Errorf("-output must be text or json")
} }

View file

@ -7,28 +7,28 @@ import (
func TestSkipKindMaps(t *testing.T) { func TestSkipKindMaps(t *testing.T) {
for _, testCase := range []struct { for _, testCase := range []struct {
name string name string
csvSkipKinds string csvSkipKinds string
expect map[string]bool expect map[string]bool
} { }{
{ {
"nothing to skip", "nothing to skip",
"", "",
map[string]bool {}, map[string]bool{},
}, },
{ {
"a single kind to skip", "a single kind to skip",
"somekind", "somekind",
map[string]bool { map[string]bool{
"somekind": true, "somekind": true,
}, },
}, },
{ {
"multiple kinds to skip", "multiple kinds to skip",
"somekind,anotherkind,yetsomeotherkind", "somekind,anotherkind,yetsomeotherkind",
map[string]bool { map[string]bool{
"somekind": true, "somekind": true,
"anotherkind": true, "anotherkind": true,
"yetsomeotherkind": true, "yetsomeotherkind": true,
}, },
}, },
@ -38,4 +38,4 @@ func TestSkipKindMaps(t *testing.T) {
t.Errorf("%s - got %+v, expected %+v", testCase.name, got, testCase.expect) t.Errorf("%s - got %+v, expected %+v", testCase.name, got, testCase.expect)
} }
} }
} }

View file

@ -3,6 +3,7 @@ package output
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
) )
type result struct { type result struct {
@ -14,14 +15,16 @@ type result struct {
} }
type jsono struct { type jsono struct {
w io.Writer
withSummary bool withSummary bool
verbose bool verbose bool
results []result results []result
nValid, nInvalid, nErrors, nSkipped int nValid, nInvalid, nErrors, nSkipped int
} }
func JSON(withSummary bool, quiet bool) Output { func JSON(w io.Writer, withSummary bool, quiet bool) Output {
return &jsono{ return &jsono{
w: w,
withSummary: withSummary, withSummary: withSummary,
verbose: quiet, verbose: quiet,
results: []result{}, results: []result{},
@ -99,8 +102,8 @@ func (o *jsono) Flush() {
} }
if err != nil { if err != nil {
fmt.Printf("error print results: %s", err) fmt.Fprintf(o.w, "error print results: %s", err)
return return
} }
fmt.Printf("%s\n", res) fmt.Fprintf(o.w, "%s\n", res)
} }

View file

@ -2,18 +2,21 @@ package output
import ( import (
"fmt" "fmt"
"io"
"sync" "sync"
) )
type text struct { type text struct {
sync.Mutex sync.Mutex
w io.Writer
withSummary bool withSummary bool
verbose bool verbose bool
nValid, nInvalid, nErrors, nSkipped int nValid, nInvalid, nErrors, nSkipped int
} }
func Text(withSummary, verbose bool) Output { func Text(w io.Writer, withSummary, verbose bool) Output {
return &text{ return &text{
w: w,
withSummary: withSummary, withSummary: withSummary,
verbose: verbose, verbose: verbose,
nValid: 0, nValid: 0,
@ -30,18 +33,18 @@ func (o *text) Write(filename, kind, version string, err error, skipped bool) {
switch status(err, skipped) { switch status(err, skipped) {
case VALID: case VALID:
if !o.verbose { 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++ o.nValid++
case INVALID: 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++ o.nInvalid++
case ERROR: 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++ o.nErrors++
case SKIPPED: case SKIPPED:
if o.verbose { if o.verbose {
fmt.Printf("%s - %s skipped\n", filename, kind) fmt.Fprintf(o.w, "%s - %s skipped\n", filename, kind)
} }
o.nSkipped++ o.nSkipped++
} }
@ -49,6 +52,6 @@ func (o *text) Write(filename, kind, version string, err error, skipped bool) {
func (o *text) Flush() { func (o *text) Flush() {
if o.withSummary { 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)
} }
} }