From e9bee9a4135e7a45d63ac4f116689d3006d2417b Mon Sep 17 00:00:00 2001 From: Paolo Calao Date: Wed, 15 Dec 2021 12:04:38 +0100 Subject: [PATCH 1/2] Add YAML to feedback formats --- cli/feedback/feedback.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/cli/feedback/feedback.go b/cli/feedback/feedback.go index bac8f30a974..23f47a97557 100644 --- a/cli/feedback/feedback.go +++ b/cli/feedback/feedback.go @@ -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" @@ -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 @@ -156,13 +160,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)) } } From 42b734557270f037ecdfa5f9e8e215ede1fa2221 Mon Sep 17 00:00:00 2001 From: Paolo Calao Date: Mon, 20 Dec 2021 12:55:12 +0100 Subject: [PATCH 2/2] Enable YAML output support --- cli/cli.go | 3 ++- cli/feedback/feedback.go | 7 +++++-- cli/version/version.go | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/cli/cli.go b/cli/cli.go index 8a1aab99223..72556e2d88a 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -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 @@ -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 diff --git a/cli/feedback/feedback.go b/cli/feedback/feedback.go index 23f47a97557..1a29630b887 100644 --- a/cli/feedback/feedback.go +++ b/cli/feedback/feedback.go @@ -113,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) } } diff --git a/cli/version/version.go b/cli/version/version.go index 8af8d73dc2a..73a708c5450 100644 --- a/cli/version/version.go +++ b/cli/version/version.go @@ -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() }