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,19 @@ 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
- if err != nil {
148
- feedback .Errorf ("Error during build: %v" , err )
149
- os .Exit (errorcodes .ErrGeneric )
147
+ }
148
+ compileOut := new (bytes.Buffer )
149
+ compileErr := new (bytes.Buffer )
150
+ verboseCompile := configuration .Settings .GetString ("logging.level" ) == "debug"
151
+ var compileRes * rpc.CompileResp
152
+ if output .OutputFormat == "json" {
153
+ compileRes , err = compile .Compile (context .Background (), compileReq , compileOut , compileErr , verboseCompile )
154
+ } else {
155
+ compileRes , err = compile .Compile (context .Background (), compileReq , os .Stdout , os .Stderr , verboseCompile )
150
156
}
151
157
152
- if uploadAfterCompile {
153
- _ , err := upload . Upload ( context . Background (), & rpc.UploadReq {
158
+ if err == nil && uploadAfterCompile {
159
+ uploadReq := & rpc.UploadReq {
154
160
Instance : inst ,
155
161
Fqbn : fqbn ,
156
162
SketchPath : sketchPath .String (),
@@ -159,13 +165,32 @@ func run(cmd *cobra.Command, args []string) {
159
165
Verify : verify ,
160
166
ImportDir : buildPath ,
161
167
Programmer : programmer ,
162
- }, os .Stdout , os .Stderr )
163
-
168
+ }
169
+ var err error
170
+ if output .OutputFormat == "json" {
171
+ // TODO: do not print upload output in json mode
172
+ uploadOut := new (bytes.Buffer )
173
+ uploadErr := new (bytes.Buffer )
174
+ _ , err = upload .Upload (context .Background (), uploadReq , uploadOut , uploadErr )
175
+ } else {
176
+ _ , err = upload .Upload (context .Background (), uploadReq , os .Stdout , os .Stderr )
177
+ }
164
178
if err != nil {
165
179
feedback .Errorf ("Error during Upload: %v" , err )
166
180
os .Exit (errorcodes .ErrGeneric )
167
181
}
168
182
}
183
+
184
+ feedback .PrintResult (& compileResult {
185
+ CompileOut : compileOut .String (),
186
+ CompileErr : compileErr .String (),
187
+ BuilderResult : compileRes ,
188
+ Success : err == nil ,
189
+ })
190
+ if err != nil && output .OutputFormat != "json" {
191
+ feedback .Errorf ("Error during build: %v" , err )
192
+ os .Exit (errorcodes .ErrGeneric )
193
+ }
169
194
}
170
195
171
196
// initSketchPath returns the current working directory
@@ -182,3 +207,19 @@ func initSketchPath(sketchPath *paths.Path) *paths.Path {
182
207
logrus .Infof ("Reading sketch from dir: %s" , wd )
183
208
return wd
184
209
}
210
+
211
+ type compileResult struct {
212
+ CompileOut string `json:"compiler_out"`
213
+ CompileErr string `json:"compiler_err"`
214
+ BuilderResult * rpc.CompileResp `json:"builder_result"`
215
+ Success bool `json:"success"`
216
+ }
217
+
218
+ func (r * compileResult ) Data () interface {} {
219
+ return r
220
+ }
221
+
222
+ func (r * compileResult ) String () string {
223
+ // The output is already printed via os.Stdout/os.Stdin
224
+ return ""
225
+ }
0 commit comments