Add json output

This commit is contained in:
Marc Campbell
2019-12-18 15:12:11 +00:00
parent 94f812c7bd
commit 7ac973647d
2 changed files with 57 additions and 5 deletions
+1 -1
View File
@@ -126,7 +126,7 @@ func runPreflights(v *viper.Viper, arg string) error {
return showInteractiveResults(preflight.Name, analyzeResults)
}
return showStdoutResults(preflight.Name, analyzeResults)
return showStdoutResults(v.GetString("format"), preflight.Name, analyzeResults)
}
func runCollectors(v *viper.Viper, preflight troubleshootv1beta1.Preflight) (map[string][]byte, error) {
+56 -4
View File
@@ -1,16 +1,24 @@
package cli
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
analyzerunner "github.com/replicatedhq/troubleshoot/pkg/analyze"
)
func showStdoutResults(preflightName string, analyzeResults []*analyzerunner.AnalyzeResult) error {
fmt.Printf("\n=== TEST %s\n", preflightName)
for _, analyzeResult := range analyzeResults {
fmt.Printf("=== RUN: %s\n", analyzeResult.Title)
func showStdoutResults(format string, preflightName string, analyzeResults []*analyzerunner.AnalyzeResult) error {
if format == "human" {
return showStdoutResultsHuman(preflightName, analyzeResults)
} else if format == "json" {
return showStdoutResultsJSON(preflightName, analyzeResults)
}
return errors.Errorf("unknown output format: %q", format)
}
func showStdoutResultsHuman(preflightName string, analyzeResults []*analyzerunner.AnalyzeResult) error {
var failed bool
for _, analyzeResult := range analyzeResults {
testResultfailed := outputResult(analyzeResult)
@@ -28,6 +36,50 @@ func showStdoutResults(preflightName string, analyzeResults []*analyzerunner.Ana
return nil
}
func showStdoutResultsJSON(preflightName string, analyzeResults []*analyzerunner.AnalyzeResult) error {
type ResultOutput struct {
Title string `json:"title"`
Message string `json:"message"`
URI string `json:"uri,omitempty"`
}
type Output struct {
Pass []ResultOutput `json:"pass,omitempty"`
Warn []ResultOutput `json:"warn,omitempty"`
Fail []ResultOutput `json:"fail,omitempty"`
}
output := Output{
Pass: []ResultOutput{},
Warn: []ResultOutput{},
Fail: []ResultOutput{},
}
for _, analyzeResult := range analyzeResults {
resultOutput := ResultOutput{
Title: analyzeResult.Title,
Message: analyzeResult.Message,
URI: analyzeResult.URI,
}
if analyzeResult.IsPass {
output.Pass = append(output.Pass, resultOutput)
} else if analyzeResult.IsWarn {
output.Warn = append(output.Warn, resultOutput)
} else if analyzeResult.IsFail {
output.Fail = append(output.Fail, resultOutput)
}
}
b, err := json.MarshalIndent(output, "", " ")
if err != nil {
return errors.Wrap(err, "failed to marshal results")
}
fmt.Printf("%s\n", b)
return nil
}
func outputResult(analyzeResult *analyzerunner.AnalyzeResult) bool {
if analyzeResult.IsPass {
fmt.Printf(" --- PASS %s\n", analyzeResult.Title)