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) {
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")
}

View file

@ -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)
}
}
}
}

View file

@ -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)
}

View file

@ -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)
}
}