Skip to content

Commit 9b6f769

Browse files
authored
Add minified json output format (#1586)
1 parent 2067504 commit 9b6f769

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

Diff for: cli/cli.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,15 @@ func createCliCommandTree(cmd *cobra.Command) {
111111
return validLogLevels, cobra.ShellCompDirectiveDefault
112112
})
113113
cmd.PersistentFlags().String("log-file", "", tr("Path to the file where logs will be written."))
114-
validFormats := []string{"text", "json"}
115-
cmd.PersistentFlags().String("log-format", "", tr("The output format for the logs, can be: %s", strings.Join(validFormats, ", ")))
114+
validLogFormats := []string{"text", "json"}
115+
cmd.PersistentFlags().String("log-format", "", tr("The output format for the logs, can be: %s", strings.Join(validLogFormats, ", ")))
116116
cmd.RegisterFlagCompletionFunc("log-format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
117-
return validFormats, cobra.ShellCompDirectiveDefault
117+
return validLogFormats, cobra.ShellCompDirectiveDefault
118118
})
119-
cmd.PersistentFlags().StringVar(&outputFormat, "format", "text", tr("The output format for the logs, can be: %s", strings.Join(validFormats, ", ")))
119+
validOutputFormats := []string{"text", "json", "jsonmini"}
120+
cmd.PersistentFlags().StringVar(&outputFormat, "format", "text", tr("The output format for the logs, can be: %s", strings.Join(validOutputFormats, ", ")))
120121
cmd.RegisterFlagCompletionFunc("format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
121-
return validFormats, cobra.ShellCompDirectiveDefault
122+
return validOutputFormats, cobra.ShellCompDirectiveDefault
122123
})
123124
cmd.PersistentFlags().StringVar(&configFile, "config-file", "", tr("The custom config file (if not specified the default will be used)."))
124125
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) {
144145

145146
func parseFormatString(arg string) (feedback.OutputFormat, bool) {
146147
f, found := map[string]feedback.OutputFormat{
147-
"json": feedback.JSON,
148-
"text": feedback.Text,
149-
}[arg]
148+
"json": feedback.JSON,
149+
"jsonmini": feedback.JSONMini,
150+
"text": feedback.Text,
151+
}[strings.ToLower(arg)]
150152

151153
return f, found
152154
}

Diff for: cli/feedback/feedback.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ const (
3535
Text OutputFormat = iota
3636
// JSON means JSON format
3737
JSON
38+
// JSONMini is identical to JSON but without whitespaces
39+
JSONMini
3840
)
3941

4042
// 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{}) {
107109

108110
// Print behaves like fmt.Print but writes on the out writer and adds a newline.
109111
func (fb *Feedback) Print(v interface{}) {
110-
if fb.format == JSON {
112+
if fb.format == JSON || fb.format == JSONMini {
111113
fb.printJSON(v)
112114
} else {
113115
fmt.Fprintln(fb.out, v)
@@ -140,7 +142,14 @@ func (fb *Feedback) Error(v ...interface{}) {
140142
// printJSON is a convenient wrapper to provide feedback by printing the
141143
// desired output in a pretty JSON format. It adds a newline to the output.
142144
func (fb *Feedback) printJSON(v interface{}) {
143-
if d, err := json.MarshalIndent(v, "", " "); err != nil {
145+
var d []byte
146+
var err error
147+
if fb.format == JSON {
148+
d, err = json.MarshalIndent(v, "", " ")
149+
} else if fb.format == JSONMini {
150+
d, err = json.Marshal(v)
151+
}
152+
if err != nil {
144153
fb.Errorf(tr("Error during JSON encoding of the output: %v"), err)
145154
} else {
146155
fmt.Fprintf(fb.out, "%v\n", string(d))
@@ -151,7 +160,7 @@ func (fb *Feedback) printJSON(v interface{}) {
151160
// where the contents can't be just serialized to JSON but requires more
152161
// structure.
153162
func (fb *Feedback) PrintResult(res Result) {
154-
if fb.format == JSON {
163+
if fb.format == JSON || fb.format == JSONMini {
155164
fb.printJSON(res.Data())
156165
} else {
157166
fb.Print(fmt.Sprintf("%s", res))

Diff for: cli/version/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func runVersionCommand(cmd *cobra.Command, args []string) {
6161
latestVersion := updater.ForceCheckForUpdate(currentVersion)
6262

6363
versionInfo := globals.VersionInfo
64-
if feedback.GetFormat() == feedback.JSON && latestVersion != nil {
64+
if f := feedback.GetFormat(); (f == feedback.JSON || f == feedback.JSONMini) && latestVersion != nil {
6565
// Set this only we managed to get the latest version
6666
versionInfo.LatestVersion = latestVersion.String()
6767
}

0 commit comments

Comments
 (0)