output: extract iniitializer function

This commit is contained in:
Christoph Mertz 2020-10-22 10:21:40 +02:00
parent bf29e486d2
commit daab22bc8a
2 changed files with 14 additions and 14 deletions

View file

@ -136,19 +136,6 @@ func (ap *arrayParam) Set(value string) error {
return nil return nil
} }
func getLogger(outputFormat string, printSummary, verbose bool) (output.Output, error) {
w := os.Stdout
switch {
case outputFormat == "text":
return output.Text(w, printSummary, verbose), nil
case outputFormat == "json":
return output.JSON(w, printSummary, verbose), nil
default:
return nil, fmt.Errorf("-output must be text or json")
}
}
func skipKindsMap(skipKindsCSV string) map[string]bool { func skipKindsMap(skipKindsCSV string) map[string]bool {
splitKinds := strings.Split(skipKindsCSV, ",") splitKinds := strings.Split(skipKindsCSV, ",")
skipKinds := map[string]bool{} skipKinds := map[string]bool{}
@ -281,7 +268,7 @@ func realMain() int {
}() }()
var o output.Output var o output.Output
if o, err = getLogger(outputFormat, summary, verbose); err != nil { if o, err = output.New(outputFormat, summary, verbose); err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
return 1 return 1
} }

View file

@ -18,6 +18,19 @@ type Output interface {
Flush() error Flush() error
} }
func New(outputFormat string, printSummary, verbose bool) (Output, error) {
w := os.Stdout
switch {
case outputFormat == "text":
return Text(w, printSummary, verbose), nil
case outputFormat == "json":
return JSON(w, printSummary, verbose), nil
default:
return nil, fmt.Errorf("`outputFormat` must be 'text' or 'json'")
}
}
func status(kind, name string, err error, skipped bool) int { func status(kind, name string, err error, skipped bool) int {
if name == "" && kind == "" && err == nil && skipped == false { if name == "" && kind == "" && err == nil && skipped == false {
return EMPTY return EMPTY