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,22 @@ 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+ }
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+ }
147156 if err != nil {
148157 feedback .Errorf ("Error during build: %v" , err )
149158 os .Exit (errorcodes .ErrGeneric )
150159 }
151160
152161 if uploadAfterCompile {
153- _ , err := upload . Upload ( context . Background (), & rpc.UploadReq {
162+ uploadReq := & rpc.UploadReq {
154163 Instance : inst ,
155164 Fqbn : fqbn ,
156165 SketchPath : sketchPath .String (),
@@ -159,13 +168,26 @@ func run(cmd *cobra.Command, args []string) {
159168 Verify : verify ,
160169 ImportDir : buildPath ,
161170 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+ }
164181 if err != nil {
165182 feedback .Errorf ("Error during Upload: %v" , err )
166183 os .Exit (errorcodes .ErrGeneric )
167184 }
168185 }
186+
187+ feedback .PrintResult (& compileResult {
188+ CompileOut : compileOut .String (),
189+ CompileErr : compileErr .String (),
190+ })
169191}
170192
171193// initSketchPath returns the current working directory
@@ -182,3 +204,17 @@ func initSketchPath(sketchPath *paths.Path) *paths.Path {
182204 logrus .Infof ("Reading sketch from dir: %s" , wd )
183205 return wd
184206}
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