Skip to content

Commit b53ea60

Browse files
committed
Add YAML to feedback formats
1 parent d458040 commit b53ea60

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

cli/feedback/feedback.go

+19-2
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
@@ -156,13 +160,26 @@ func (fb *Feedback) printJSON(v interface{}) {
156160
}
157161
}
158162

163+
// printYAML is a convenient wrapper to provide feedback by printing the
164+
// desired output in YAML format. It adds a newline to the output.
165+
func (fb *Feedback) printYAML(v interface{}) {
166+
d, err := yaml.Marshal(v)
167+
if err != nil {
168+
fb.Errorf(tr("Error during YAML encoding of the output: %v"), err)
169+
}
170+
fmt.Fprintf(fb.out, "%v\n", string(d))
171+
}
172+
159173
// PrintResult is a convenient wrapper to provide feedback for complex data,
160174
// where the contents can't be just serialized to JSON but requires more
161175
// structure.
162176
func (fb *Feedback) PrintResult(res Result) {
163-
if fb.format == JSON || fb.format == JSONMini {
177+
switch fb.format {
178+
case JSON, JSONMini:
164179
fb.printJSON(res.Data())
165-
} else {
180+
case YAML:
181+
fb.printYAML(res.Data())
182+
default:
166183
fb.Print(fmt.Sprintf("%s", res))
167184
}
168185
}

0 commit comments

Comments
 (0)