Skip to content
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

Add YAML to feedback formats #1600

Merged
merged 2 commits into from
Dec 20, 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
3 changes: 2 additions & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
29 changes: 25 additions & 4 deletions cli/feedback/feedback.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -109,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)
}
}
Expand Down Expand Up @@ -156,13 +163,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))
}
}
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 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()
}
Expand Down