Skip to content

Add minified json output format #1586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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