@@ -22,6 +22,8 @@ import (
22
22
"io"
23
23
"os"
24
24
25
+ "gopkg.in/yaml.v2"
26
+
25
27
"github.com/arduino/arduino-cli/i18n"
26
28
"github.com/sirupsen/logrus"
27
29
"google.golang.org/grpc/status"
@@ -37,6 +39,8 @@ const (
37
39
JSON
38
40
// JSONMini is identical to JSON but without whitespaces
39
41
JSONMini
42
+ // YAML means YAML format
43
+ YAML
40
44
)
41
45
42
46
// Result is anything more complex than a sentence that needs to be printed
@@ -156,13 +160,26 @@ func (fb *Feedback) printJSON(v interface{}) {
156
160
}
157
161
}
158
162
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
+
159
173
// PrintResult is a convenient wrapper to provide feedback for complex data,
160
174
// where the contents can't be just serialized to JSON but requires more
161
175
// structure.
162
176
func (fb * Feedback ) PrintResult (res Result ) {
163
- if fb .format == JSON || fb .format == JSONMini {
177
+ switch fb .format {
178
+ case JSON , JSONMini :
164
179
fb .printJSON (res .Data ())
165
- } else {
180
+ case YAML :
181
+ fb .printYAML (res .Data ())
182
+ default :
166
183
fb .Print (fmt .Sprintf ("%s" , res ))
167
184
}
168
185
}
0 commit comments