11package cli
22
33import (
4+ "encoding/json"
45 "fmt"
56
7+ "github.com/pkg/errors"
68 analyzerunner "github.com/replicatedhq/troubleshoot/pkg/analyze"
79)
810
9- func showStdoutResults (preflightName string , analyzeResults []* analyzerunner.AnalyzeResult ) error {
10- fmt .Printf ("\n === TEST %s\n " , preflightName )
11- for _ , analyzeResult := range analyzeResults {
12- fmt .Printf ("=== RUN: %s\n " , analyzeResult .Title )
11+ func showStdoutResults (format string , preflightName string , analyzeResults []* analyzerunner.AnalyzeResult ) error {
12+ if format == "human" {
13+ return showStdoutResultsHuman (preflightName , analyzeResults )
14+ } else if format == "json" {
15+ return showStdoutResultsJSON (preflightName , analyzeResults )
1316 }
17+
18+ return errors .Errorf ("unknown output format: %q" , format )
19+ }
20+
21+ func showStdoutResultsHuman (preflightName string , analyzeResults []* analyzerunner.AnalyzeResult ) error {
1422 var failed bool
1523 for _ , analyzeResult := range analyzeResults {
1624 testResultfailed := outputResult (analyzeResult )
@@ -28,6 +36,50 @@ func showStdoutResults(preflightName string, analyzeResults []*analyzerunner.Ana
2836 return nil
2937}
3038
39+ func showStdoutResultsJSON (preflightName string , analyzeResults []* analyzerunner.AnalyzeResult ) error {
40+ type ResultOutput struct {
41+ Title string `json:"title"`
42+ Message string `json:"message"`
43+ URI string `json:"uri,omitempty"`
44+ }
45+ type Output struct {
46+ Pass []ResultOutput `json:"pass,omitempty"`
47+ Warn []ResultOutput `json:"warn,omitempty"`
48+ Fail []ResultOutput `json:"fail,omitempty"`
49+ }
50+
51+ output := Output {
52+ Pass : []ResultOutput {},
53+ Warn : []ResultOutput {},
54+ Fail : []ResultOutput {},
55+ }
56+
57+ for _ , analyzeResult := range analyzeResults {
58+ resultOutput := ResultOutput {
59+ Title : analyzeResult .Title ,
60+ Message : analyzeResult .Message ,
61+ URI : analyzeResult .URI ,
62+ }
63+
64+ if analyzeResult .IsPass {
65+ output .Pass = append (output .Pass , resultOutput )
66+ } else if analyzeResult .IsWarn {
67+ output .Warn = append (output .Warn , resultOutput )
68+ } else if analyzeResult .IsFail {
69+ output .Fail = append (output .Fail , resultOutput )
70+ }
71+ }
72+
73+ b , err := json .MarshalIndent (output , "" , " " )
74+ if err != nil {
75+ return errors .Wrap (err , "failed to marshal results" )
76+ }
77+
78+ fmt .Printf ("%s\n " , b )
79+
80+ return nil
81+ }
82+
3183func outputResult (analyzeResult * analyzerunner.AnalyzeResult ) bool {
3284 if analyzeResult .IsPass {
3385 fmt .Printf (" --- PASS %s\n " , analyzeResult .Title )
0 commit comments