16
16
package compile
17
17
18
18
import (
19
+ "bytes"
19
20
"context"
20
21
"os"
21
22
22
23
"github.com/arduino/arduino-cli/cli/feedback"
24
+ "github.com/arduino/arduino-cli/cli/output"
23
25
"github.com/arduino/arduino-cli/configuration"
24
26
25
27
"github.com/arduino/arduino-cli/cli/errorcodes"
@@ -124,7 +126,7 @@ func run(cmd *cobra.Command, args []string) {
124
126
// the config file and the env vars.
125
127
exportBinaries = configuration .Settings .GetBool ("sketch.always_export_binaries" )
126
128
127
- _ , err = compile . Compile ( context . Background (), & rpc.CompileReq {
129
+ compileReq := & rpc.CompileReq {
128
130
Instance : inst ,
129
131
Fqbn : fqbn ,
130
132
SketchPath : sketchPath .String (),
@@ -142,15 +144,22 @@ func run(cmd *cobra.Command, args []string) {
142
144
OptimizeForDebug : optimizeForDebug ,
143
145
Clean : clean ,
144
146
ExportBinaries : exportBinaries ,
145
- }, os .Stdout , os .Stderr , configuration .Settings .GetString ("logging.level" ) == "debug" )
146
-
147
+ }
148
+ compileOut := new (bytes.Buffer )
149
+ compileErr := new (bytes.Buffer )
150
+ verboseCompile := configuration .Settings .GetString ("logging.level" ) == "debug"
151
+ if output .OutputFormat == "json" {
152
+ _ , err = compile .Compile (context .Background (), compileReq , compileOut , compileErr , verboseCompile )
153
+ } else {
154
+ _ , err = compile .Compile (context .Background (), compileReq , os .Stdout , os .Stderr , verboseCompile )
155
+ }
147
156
if err != nil {
148
157
feedback .Errorf ("Error during build: %v" , err )
149
158
os .Exit (errorcodes .ErrGeneric )
150
159
}
151
160
152
161
if uploadAfterCompile {
153
- _ , err := upload . Upload ( context . Background (), & rpc.UploadReq {
162
+ uploadReq := & rpc.UploadReq {
154
163
Instance : inst ,
155
164
Fqbn : fqbn ,
156
165
SketchPath : sketchPath .String (),
@@ -159,13 +168,26 @@ func run(cmd *cobra.Command, args []string) {
159
168
Verify : verify ,
160
169
ImportDir : buildPath ,
161
170
Programmer : programmer ,
162
- }, os .Stdout , os .Stderr )
163
-
171
+ }
172
+ var err error
173
+ if output .OutputFormat == "json" {
174
+ // TODO: do not print upload output in json mode
175
+ uploadOut := new (bytes.Buffer )
176
+ uploadErr := new (bytes.Buffer )
177
+ _ , err = upload .Upload (context .Background (), uploadReq , uploadOut , uploadErr )
178
+ } else {
179
+ _ , err = upload .Upload (context .Background (), uploadReq , os .Stdout , os .Stderr )
180
+ }
164
181
if err != nil {
165
182
feedback .Errorf ("Error during Upload: %v" , err )
166
183
os .Exit (errorcodes .ErrGeneric )
167
184
}
168
185
}
186
+
187
+ feedback .PrintResult (& compileResult {
188
+ CompileOut : compileOut .String (),
189
+ CompileErr : compileErr .String (),
190
+ })
169
191
}
170
192
171
193
// initSketchPath returns the current working directory
@@ -182,3 +204,17 @@ func initSketchPath(sketchPath *paths.Path) *paths.Path {
182
204
logrus .Infof ("Reading sketch from dir: %s" , wd )
183
205
return wd
184
206
}
207
+
208
+ type compileResult struct {
209
+ CompileOut string
210
+ CompileErr string
211
+ }
212
+
213
+ func (r * compileResult ) Data () interface {} {
214
+ return r
215
+ }
216
+
217
+ func (r * compileResult ) String () string {
218
+ // The output is already printed via os.Stdout/os.Stdin
219
+ return ""
220
+ }
0 commit comments