Skip to content

Commit

Permalink
Add YAML output format (#1600)
Browse files Browse the repository at this point in the history
  • Loading branch information
Paolo Calao committed Dec 20, 2021
1 parent 8dcea2d commit 5dd14c4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
3 changes: 2 additions & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func createCliCommandTree(cmd *cobra.Command) {
cmd.RegisterFlagCompletionFunc("log-format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return validLogFormats, cobra.ShellCompDirectiveDefault
})
validOutputFormats := []string{"text", "json", "jsonmini"}
validOutputFormats := []string{"text", "json", "jsonmini", "yaml"}
cmd.PersistentFlags().StringVar(&outputFormat, "format", "text", tr("The output format for the logs, can be: %s", strings.Join(validOutputFormats, ", ")))
cmd.RegisterFlagCompletionFunc("format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return validOutputFormats, cobra.ShellCompDirectiveDefault
Expand Down Expand Up @@ -148,6 +148,7 @@ func parseFormatString(arg string) (feedback.OutputFormat, bool) {
"json": feedback.JSON,
"jsonmini": feedback.JSONMini,
"text": feedback.Text,
"yaml": feedback.YAML,
}[strings.ToLower(arg)]

return f, found
Expand Down
29 changes: 25 additions & 4 deletions cli/feedback/feedback.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"io"
"os"

"gopkg.in/yaml.v2"

"github.com/arduino/arduino-cli/i18n"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/status"
Expand All @@ -37,6 +39,8 @@ const (
JSON
// JSONMini is identical to JSON but without whitespaces
JSONMini
// YAML means YAML format
YAML
)

// Result is anything more complex than a sentence that needs to be printed
Expand Down Expand Up @@ -109,9 +113,12 @@ func (fb *Feedback) Printf(format string, v ...interface{}) {

// Print behaves like fmt.Print but writes on the out writer and adds a newline.
func (fb *Feedback) Print(v interface{}) {
if fb.format == JSON || fb.format == JSONMini {
switch fb.format {
case JSON, JSONMini:
fb.printJSON(v)
} else {
case YAML:
fb.printYAML(v)
default:
fmt.Fprintln(fb.out, v)
}
}
Expand Down Expand Up @@ -156,13 +163,27 @@ func (fb *Feedback) printJSON(v interface{}) {
}
}

// printYAML is a convenient wrapper to provide feedback by printing the
// desired output in YAML format. It adds a newline to the output.
func (fb *Feedback) printYAML(v interface{}) {
d, err := yaml.Marshal(v)
if err != nil {
fb.Errorf(tr("Error during YAML encoding of the output: %v"), err)
return
}
fmt.Fprintf(fb.out, "%v\n", string(d))
}

// PrintResult is a convenient wrapper to provide feedback for complex data,
// where the contents can't be just serialized to JSON but requires more
// structure.
func (fb *Feedback) PrintResult(res Result) {
if fb.format == JSON || fb.format == JSONMini {
switch fb.format {
case JSON, JSONMini:
fb.printJSON(res.Data())
} else {
case YAML:
fb.printYAML(res.Data())
default:
fb.Print(fmt.Sprintf("%s", res))
}
}
2 changes: 1 addition & 1 deletion cli/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func runVersionCommand(cmd *cobra.Command, args []string) {
latestVersion := updater.ForceCheckForUpdate(currentVersion)

versionInfo := globals.VersionInfo
if f := feedback.GetFormat(); (f == feedback.JSON || f == feedback.JSONMini) && latestVersion != nil {
if f := feedback.GetFormat(); (f == feedback.JSON || f == feedback.JSONMini || f == feedback.YAML) && latestVersion != nil {
// Set this only we managed to get the latest version
versionInfo.LatestVersion = latestVersion.String()
}
Expand Down

0 comments on commit 5dd14c4

Please sign in to comment.