1616package compile
1717
1818import (
19+ "bytes"
1920 "context"
2021 "os"
2122
2223 "github.com/arduino/arduino-cli/cli/feedback"
24+ "github.com/arduino/arduino-cli/cli/output"
2325 "github.com/arduino/arduino-cli/configuration"
2426
2527 "github.com/arduino/arduino-cli/cli/errorcodes"
@@ -124,7 +126,7 @@ func run(cmd *cobra.Command, args []string) {
124126 // the config file and the env vars.
125127 exportBinaries = configuration .Settings .GetBool ("sketch.always_export_binaries" )
126128
127- _ , err = compile . Compile ( context . Background (), & rpc.CompileReq {
129+ compileReq := & rpc.CompileReq {
128130 Instance : inst ,
129131 Fqbn : fqbn ,
130132 SketchPath : sketchPath .String (),
@@ -142,15 +144,19 @@ func run(cmd *cobra.Command, args []string) {
142144 OptimizeForDebug : optimizeForDebug ,
143145 Clean : clean ,
144146 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 )
150156 }
151157
152- if uploadAfterCompile {
153- _ , err := upload . Upload ( context . Background (), & rpc.UploadReq {
158+ if err == nil && uploadAfterCompile {
159+ uploadReq := & rpc.UploadReq {
154160 Instance : inst ,
155161 Fqbn : fqbn ,
156162 SketchPath : sketchPath .String (),
@@ -159,13 +165,32 @@ func run(cmd *cobra.Command, args []string) {
159165 Verify : verify ,
160166 ImportDir : buildPath ,
161167 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+ }
164178 if err != nil {
165179 feedback .Errorf ("Error during Upload: %v" , err )
166180 os .Exit (errorcodes .ErrGeneric )
167181 }
168182 }
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+ }
169194}
170195
171196// initSketchPath returns the current working directory
@@ -182,3 +207,19 @@ func initSketchPath(sketchPath *paths.Path) *paths.Path {
182207 logrus .Infof ("Reading sketch from dir: %s" , wd )
183208 return wd
184209}
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