@@ -6,13 +6,16 @@ import (
66
77 "github.com/pkg/errors"
88 analyzerunner "github.com/replicatedhq/troubleshoot/pkg/analyze"
9+ "gopkg.in/yaml.v2"
910)
1011
1112func showStdoutResults (format string , preflightName string , analyzeResults []* analyzerunner.AnalyzeResult ) error {
1213 if format == "human" {
1314 return showStdoutResultsHuman (preflightName , analyzeResults )
1415 } else if format == "json" {
1516 return showStdoutResultsJSON (preflightName , analyzeResults )
17+ } else if format == "yaml" {
18+ return showStdoutResultsYAML (preflightName , analyzeResults )
1619 }
1720
1821 return errors .Errorf ("unknown output format: %q" , format )
@@ -37,27 +40,29 @@ func showStdoutResultsHuman(preflightName string, analyzeResults []*analyzerunne
3740 return nil
3841}
3942
40- func showStdoutResultsJSON ( preflightName string , analyzeResults [] * analyzerunner. AnalyzeResult ) error {
41- type ResultOutput struct {
42- Title string `json:"title "`
43- Message string `json:"message "`
44- URI string `json:"uri ,omitempty"`
45- Strict bool `json:"strict,omitempty"`
46- }
47- type Output struct {
48- Pass []ResultOutput `json:"pass,omitempty"`
49- Warn []ResultOutput `json:"warn,omitempty"`
50- Fail []ResultOutput `json:"fail,omitempty"`
51- }
43+ type stdoutResultOutput struct {
44+ Title string `json:"title" yaml:"title"`
45+ Message string `json:"message" yaml:"message "`
46+ URI string `json:"uri,omitempty" yaml:"uri,omitempty "`
47+ Strict bool `json:"strict,omitempty" yaml:"strict ,omitempty"`
48+ }
49+
50+ type stdoutOutput struct {
51+ Pass []stdoutResultOutput `json:"pass,omitempty" yaml :"pass,omitempty"`
52+ Warn []stdoutResultOutput `json:"warn,omitempty" yaml :"warn,omitempty"`
53+ Fail []stdoutResultOutput `json:"fail,omitempty" yaml :"fail,omitempty"`
54+ }
5255
53- output := Output {
54- Pass : []ResultOutput {},
55- Warn : []ResultOutput {},
56- Fail : []ResultOutput {},
56+ // Used by both JSON and YAML outputs
57+ func showStdoutResultsStructured (preflightName string , analyzeResults []* analyzerunner.AnalyzeResult ) * stdoutOutput {
58+ output := stdoutOutput {
59+ Pass : []stdoutResultOutput {},
60+ Warn : []stdoutResultOutput {},
61+ Fail : []stdoutResultOutput {},
5762 }
5863
5964 for _ , analyzeResult := range analyzeResults {
60- resultOutput := ResultOutput {
65+ resultOutput := stdoutResultOutput {
6166 Title : analyzeResult .Title ,
6267 Message : analyzeResult .Message ,
6368 URI : analyzeResult .URI ,
@@ -76,9 +81,28 @@ func showStdoutResultsJSON(preflightName string, analyzeResults []*analyzerunner
7681 }
7782 }
7883
79- b , err := json .MarshalIndent (output , "" , " " )
84+ return & output
85+ }
86+
87+ func showStdoutResultsJSON (preflightName string , analyzeResults []* analyzerunner.AnalyzeResult ) error {
88+ output := showStdoutResultsStructured (preflightName , analyzeResults )
89+
90+ b , err := json .MarshalIndent (* output , "" , " " )
91+ if err != nil {
92+ return errors .Wrap (err , "failed to marshal results as json" )
93+ }
94+
95+ fmt .Printf ("%s\n " , b )
96+
97+ return nil
98+ }
99+
100+ func showStdoutResultsYAML (preflightName string , analyzeResults []* analyzerunner.AnalyzeResult ) error {
101+ output := showStdoutResultsStructured (preflightName , analyzeResults )
102+
103+ b , err := yaml .Marshal (* output )
80104 if err != nil {
81- return errors .Wrap (err , "failed to marshal results" )
105+ return errors .Wrap (err , "failed to marshal results as yaml " )
82106 }
83107
84108 fmt .Printf ("%s\n " , b )
0 commit comments