Skip to content

Commit

Permalink
preflight: add yaml output format (#940)
Browse files Browse the repository at this point in the history
* preflight: add yaml output format

ref #905
  • Loading branch information
Nathan Sullivan authored Jan 4, 2023
1 parent 8ba5d6f commit 87c153c
Showing 1 changed file with 43 additions and 19 deletions.
62 changes: 43 additions & 19 deletions pkg/preflight/stdout_results.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import (

"github.com/pkg/errors"
analyzerunner "github.com/replicatedhq/troubleshoot/pkg/analyze"
"gopkg.in/yaml.v2"
)

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)
} else if format == "yaml" {
return showStdoutResultsYAML(preflightName, analyzeResults)
}

return errors.Errorf("unknown output format: %q", format)
Expand All @@ -37,27 +40,29 @@ func showStdoutResultsHuman(preflightName string, analyzeResults []*analyzerunne
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"`
Strict bool `json:"strict,omitempty"`
}
type Output struct {
Pass []ResultOutput `json:"pass,omitempty"`
Warn []ResultOutput `json:"warn,omitempty"`
Fail []ResultOutput `json:"fail,omitempty"`
}
type stdoutResultOutput struct {
Title string `json:"title" yaml:"title"`
Message string `json:"message" yaml:"message"`
URI string `json:"uri,omitempty" yaml:"uri,omitempty"`
Strict bool `json:"strict,omitempty" yaml:"strict,omitempty"`
}

type stdoutOutput struct {
Pass []stdoutResultOutput `json:"pass,omitempty" yaml:"pass,omitempty"`
Warn []stdoutResultOutput `json:"warn,omitempty" yaml:"warn,omitempty"`
Fail []stdoutResultOutput `json:"fail,omitempty" yaml:"fail,omitempty"`
}

output := Output{
Pass: []ResultOutput{},
Warn: []ResultOutput{},
Fail: []ResultOutput{},
// Used by both JSON and YAML outputs
func showStdoutResultsStructured(preflightName string, analyzeResults []*analyzerunner.AnalyzeResult) *stdoutOutput {
output := stdoutOutput{
Pass: []stdoutResultOutput{},
Warn: []stdoutResultOutput{},
Fail: []stdoutResultOutput{},
}

for _, analyzeResult := range analyzeResults {
resultOutput := ResultOutput{
resultOutput := stdoutResultOutput{
Title: analyzeResult.Title,
Message: analyzeResult.Message,
URI: analyzeResult.URI,
Expand All @@ -76,9 +81,28 @@ func showStdoutResultsJSON(preflightName string, analyzeResults []*analyzerunner
}
}

b, err := json.MarshalIndent(output, "", " ")
return &output
}

func showStdoutResultsJSON(preflightName string, analyzeResults []*analyzerunner.AnalyzeResult) error {
output := showStdoutResultsStructured(preflightName, analyzeResults)

b, err := json.MarshalIndent(*output, "", " ")
if err != nil {
return errors.Wrap(err, "failed to marshal results as json")
}

fmt.Printf("%s\n", b)

return nil
}

func showStdoutResultsYAML(preflightName string, analyzeResults []*analyzerunner.AnalyzeResult) error {
output := showStdoutResultsStructured(preflightName, analyzeResults)

b, err := yaml.Marshal(*output)
if err != nil {
return errors.Wrap(err, "failed to marshal results")
return errors.Wrap(err, "failed to marshal results as yaml")
}

fmt.Printf("%s\n", b)
Expand Down

0 comments on commit 87c153c

Please sign in to comment.