@@ -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
@@ -109,9 +113,12 @@ func (fb *Feedback) Printf(format string, v ...interface{}) {
109
113
110
114
// Print behaves like fmt.Print but writes on the out writer and adds a newline.
111
115
func (fb * Feedback ) Print (v interface {}) {
112
- if fb .format == JSON || fb .format == JSONMini {
116
+ switch fb .format {
117
+ case JSON , JSONMini :
113
118
fb .printJSON (v )
114
- } else {
119
+ case YAML :
120
+ fb .printYAML (v )
121
+ default :
115
122
fmt .Fprintln (fb .out , v )
116
123
}
117
124
}
@@ -156,13 +163,27 @@ func (fb *Feedback) printJSON(v interface{}) {
156
163
}
157
164
}
158
165
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
+
159
177
// PrintResult is a convenient wrapper to provide feedback for complex data,
160
178
// where the contents can't be just serialized to JSON but requires more
161
179
// structure.
162
180
func (fb * Feedback ) PrintResult (res Result ) {
163
- if fb .format == JSON || fb .format == JSONMini {
181
+ switch fb .format {
182
+ case JSON , JSONMini :
164
183
fb .printJSON (res .Data ())
165
- } else {
184
+ case YAML :
185
+ fb .printYAML (res .Data ())
186
+ default :
166
187
fb .Print (fmt .Sprintf ("%s" , res ))
167
188
}
168
189
}
0 commit comments