Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5dd14c4

Browse files
author
Paolo Calao
authored
Add YAML output format (#1600)
1 parent 8dcea2d commit 5dd14c4

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

‎cli/cli.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func createCliCommandTree(cmd *cobra.Command) {
116116
cmd.RegisterFlagCompletionFunc("log-format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
117117
return validLogFormats, cobra.ShellCompDirectiveDefault
118118
})
119-
validOutputFormats := []string{"text", "json", "jsonmini"}
119+
validOutputFormats := []string{"text", "json", "jsonmini", "yaml"}
120120
cmd.PersistentFlags().StringVar(&outputFormat, "format", "text", tr("The output format for the logs, can be: %s", strings.Join(validOutputFormats, ", ")))
121121
cmd.RegisterFlagCompletionFunc("format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
122122
return validOutputFormats, cobra.ShellCompDirectiveDefault
@@ -148,6 +148,7 @@ func parseFormatString(arg string) (feedback.OutputFormat, bool) {
148148
"json": feedback.JSON,
149149
"jsonmini": feedback.JSONMini,
150150
"text": feedback.Text,
151+
"yaml": feedback.YAML,
151152
}[strings.ToLower(arg)]
152153

153154
return f, found

‎cli/feedback/feedback.go‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"io"
2323
"os"
2424

25+
"gopkg.in/yaml.v2"
26+
2527
"github.com/arduino/arduino-cli/i18n"
2628
"github.com/sirupsen/logrus"
2729
"google.golang.org/grpc/status"
@@ -37,6 +39,8 @@ const (
3739
JSON
3840
// JSONMini is identical to JSON but without whitespaces
3941
JSONMini
42+
// YAML means YAML format
43+
YAML
4044
)
4145

4246
// Result is anything more complex than a sentence that needs to be printed
@@ -109,9 +113,12 @@ func (fb *Feedback) Printf(format string, v ...interface{}) {
109113

110114
// Print behaves like fmt.Print but writes on the out writer and adds a newline.
111115
func (fb *Feedback) Print(v interface{}) {
112-
if fb.format == JSON || fb.format == JSONMini {
116+
switch fb.format {
117+
case JSON, JSONMini:
113118
fb.printJSON(v)
114-
} else {
119+
case YAML:
120+
fb.printYAML(v)
121+
default:
115122
fmt.Fprintln(fb.out, v)
116123
}
117124
}
@@ -156,13 +163,27 @@ func (fb *Feedback) printJSON(v interface{}) {
156163
}
157164
}
158165

166+
// printYAML is a convenient wrapper to provide feedback by printing the
167+
// desired output in YAML format. It adds a newline to the output.
168+
func (fb *Feedback) printYAML(v interface{}) {
169+
d, err := yaml.Marshal(v)
170+
if err != nil {
171+
fb.Errorf(tr("Error during YAML encoding of the output: %v"), err)
172+
return
173+
}
174+
fmt.Fprintf(fb.out, "%v\n", string(d))
175+
}
176+
159177
// PrintResult is a convenient wrapper to provide feedback for complex data,
160178
// where the contents can't be just serialized to JSON but requires more
161179
// structure.
162180
func (fb *Feedback) PrintResult(res Result) {
163-
if fb.format == JSON || fb.format == JSONMini {
181+
switch fb.format {
182+
case JSON, JSONMini:
164183
fb.printJSON(res.Data())
165-
} else {
184+
case YAML:
185+
fb.printYAML(res.Data())
186+
default:
166187
fb.Print(fmt.Sprintf("%s", res))
167188
}
168189
}

‎cli/version/version.go‎

Lines changed: 1 addition & 1 deletion
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 f := feedback.GetFormat(); (f == feedback.JSON || f == feedback.JSONMini) && latestVersion != nil {
64+
if f := feedback.GetFormat(); (f == feedback.JSON || f == feedback.JSONMini||f==feedback.YAML) && latestVersion != nil {
6565
// Set this only we managed to get the latest version
6666
versionInfo.LatestVersion = latestVersion.String()
6767
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /