Skip to content

Commit

Permalink
Add minified json output format (#1586)
Browse files Browse the repository at this point in the history
  • Loading branch information
silvanocerza committed Dec 7, 2021
1 parent 2067504 commit 9b6f769
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
18 changes: 10 additions & 8 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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."))
Expand All @@ -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
}
Expand Down
15 changes: 12 additions & 3 deletions cli/feedback/feedback.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand Down
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 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()
}
Expand Down

0 comments on commit 9b6f769

Please sign in to comment.