From 008474e0a3221c442ca01cedfedc3f03d97baf40 Mon Sep 17 00:00:00 2001 From: Silvano Cerza Date: Tue, 7 Dec 2021 16:22:57 +0100 Subject: [PATCH] Add minified json output format --- cli/cli.go | 18 ++++++++++-------- cli/feedback/feedback.go | 15 ++++++++++++--- cli/version/version.go | 2 +- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/cli/cli.go b/cli/cli.go index bc4e2d5ea5f..8a1aab99223 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -111,14 +111,15 @@ func createCliCommandTree(cmd *cobra.Command) { return validLogLevels, cobra.ShellCompDirectiveDefault }) cmd.PersistentFlags().String("log-file", "", tr("Path to the file where logs will be written.")) - validFormats := []string{"text", "json"} - cmd.PersistentFlags().String("log-format", "", tr("The output format for the logs, can be: %s", strings.Join(validFormats, ", "))) + validLogFormats := []string{"text", "json"} + cmd.PersistentFlags().String("log-format", "", tr("The output format for the logs, can be: %s", strings.Join(validLogFormats, ", "))) cmd.RegisterFlagCompletionFunc("log-format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return validFormats, cobra.ShellCompDirectiveDefault + return validLogFormats, cobra.ShellCompDirectiveDefault }) - cmd.PersistentFlags().StringVar(&outputFormat, "format", "text", tr("The output format for the logs, can be: %s", strings.Join(validFormats, ", "))) + validOutputFormats := []string{"text", "json", "jsonmini"} + 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 validFormats, cobra.ShellCompDirectiveDefault + return validOutputFormats, cobra.ShellCompDirectiveDefault }) cmd.PersistentFlags().StringVar(&configFile, "config-file", "", tr("The custom config file (if not specified the default will be used).")) cmd.PersistentFlags().StringSlice("additional-urls", []string{}, tr("Comma-separated list of additional URLs for the Boards Manager.")) @@ -144,9 +145,10 @@ func toLogLevel(s string) (t logrus.Level, found bool) { func parseFormatString(arg string) (feedback.OutputFormat, bool) { f, found := map[string]feedback.OutputFormat{ - "json": feedback.JSON, - "text": feedback.Text, - }[arg] + "json": feedback.JSON, + "jsonmini": feedback.JSONMini, + "text": feedback.Text, + }[strings.ToLower(arg)] return f, found } diff --git a/cli/feedback/feedback.go b/cli/feedback/feedback.go index ebc226a3a3d..bac8f30a974 100644 --- a/cli/feedback/feedback.go +++ b/cli/feedback/feedback.go @@ -35,6 +35,8 @@ const ( Text OutputFormat = iota // JSON means JSON format JSON + // JSONMini is identical to JSON but without whitespaces + JSONMini ) // Result is anything more complex than a sentence that needs to be printed @@ -107,7 +109,7 @@ 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 { + if fb.format == JSON || fb.format == JSONMini { fb.printJSON(v) } else { fmt.Fprintln(fb.out, v) @@ -140,7 +142,14 @@ func (fb *Feedback) Error(v ...interface{}) { // printJSON is a convenient wrapper to provide feedback by printing the // desired output in a pretty JSON format. It adds a newline to the output. func (fb *Feedback) printJSON(v interface{}) { - if d, err := json.MarshalIndent(v, "", " "); err != nil { + var d []byte + var err error + if fb.format == JSON { + d, err = json.MarshalIndent(v, "", " ") + } else if fb.format == JSONMini { + d, err = json.Marshal(v) + } + if err != nil { fb.Errorf(tr("Error during JSON encoding of the output: %v"), err) } else { fmt.Fprintf(fb.out, "%v\n", string(d)) @@ -151,7 +160,7 @@ func (fb *Feedback) printJSON(v interface{}) { // where the contents can't be just serialized to JSON but requires more // structure. func (fb *Feedback) PrintResult(res Result) { - if fb.format == JSON { + if fb.format == JSON || fb.format == JSONMini { fb.printJSON(res.Data()) } else { fb.Print(fmt.Sprintf("%s", res)) diff --git a/cli/version/version.go b/cli/version/version.go index 4114bfff235..8af8d73dc2a 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 feedback.GetFormat() == feedback.JSON && latestVersion != nil { + if f := feedback.GetFormat(); (f == feedback.JSON || f == feedback.JSONMini) && latestVersion != nil { // Set this only we managed to get the latest version versionInfo.LatestVersion = latestVersion.String() }