Skip to content

Commit 2b77020

Browse files
committed
add buffer to FlashFirmware func to capture output for json printing
1 parent ecaaaac commit 2b77020

File tree

5 files changed

+49
-10
lines changed

5 files changed

+49
-10
lines changed

cli/firmware/flash.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,21 +204,44 @@ func run(cmd *cobra.Command, args []string) {
204204
}
205205
defer f.Close()
206206

207-
if err := f.FlashFirmware(firmwareFile); err != nil {
207+
// now flash the actual firmware
208+
flasherOut := new(bytes.Buffer)
209+
flasherErr := new(bytes.Buffer)
210+
if feedback.GetFormat() == feedback.JSON {
211+
err = f.FlashFirmware(firmwareFile, flasherOut)
212+
} else {
213+
err = f.FlashFirmware(firmwareFile, os.Stdout)
214+
}
215+
if err != nil {
208216
feedback.Errorf("Error during firmware flashing: %s", err)
209-
os.Exit(errorcodes.ErrGeneric)
217+
flasherErr.Write([]byte(fmt.Sprintf("Error during firmware flashing: %s", err)))
210218
}
211219

212220
// Print the results
213221
feedback.PrintResult(&flashResult{
214-
ProgrammerOut: programmerOut.String(),
215-
ProgrammerErr: programmerErr.String(),
222+
Programmer: (&ExecOutput{
223+
Stdout: programmerOut.String(),
224+
Stderr: programmerErr.String(),
225+
}),
226+
Flasher: (&ExecOutput{
227+
Stdout: flasherOut.String(),
228+
Stderr: flasherErr.String(),
229+
}),
216230
})
231+
// Exit if something went wrong but after printing
232+
if err != nil {
233+
os.Exit(errorcodes.ErrGeneric)
234+
}
217235
}
218236

219237
type flashResult struct {
220-
ProgrammerOut string
221-
ProgrammerErr string
238+
Programmer *ExecOutput `json:"programmer"`
239+
Flasher *ExecOutput `json:"flasher"`
240+
}
241+
242+
type ExecOutput struct {
243+
Stdout string `json:"stdout"`
244+
Stderr string `json:"stderr"`
222245
}
223246

224247
func (r *flashResult) Data() interface{} {

flasher/flasher.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package flasher
2121

2222
import (
2323
"fmt"
24+
"io"
2425
"time"
2526

2627
"github.com/arduino/go-paths-helper"
@@ -48,7 +49,7 @@ func (e FlasherError) Error() string {
4849
}
4950

5051
type Flasher interface {
51-
FlashFirmware(firmwareFile *paths.Path) error
52+
FlashFirmware(firmwareFile *paths.Path, flasherOut io.Writer) error
5253
FlashCertificates(certificatePaths *paths.PathList, URLs []string) error
5354
Close() error
5455

flasher/nina.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"encoding/binary"
2828
"encoding/pem"
2929
"fmt"
30+
"io"
3031
"strconv"
3132
"time"
3233

@@ -64,8 +65,10 @@ type NinaFlasher struct {
6465
}
6566

6667
// FlashFirmware in board connected to port using data from firmwareFile
67-
func (f *NinaFlasher) FlashFirmware(firmwareFile *paths.Path) error {
68+
func (f *NinaFlasher) FlashFirmware(firmwareFile *paths.Path, flasherOut io.Writer) error {
6869
logrus.Infof("Flashing firmware %s", firmwareFile)
70+
flasherOut.Write([]byte(fmt.Sprintf("Flashing firmware %s", firmwareFile)))
71+
flasherOut.Write([]byte(fmt.Sprintln()))
6972
if err := f.hello(); err != nil {
7073
logrus.Error(err)
7174
return err
@@ -91,6 +94,8 @@ func (f *NinaFlasher) FlashFirmware(firmwareFile *paths.Path) error {
9194
return err
9295
}
9396
logrus.Infof("Flashed all the things")
97+
flasherOut.Write([]byte("Flashed all the things"))
98+
flasherOut.Write([]byte(fmt.Sprintln()))
9499
return err //should be nil
95100
}
96101

flasher/sara.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package flasher
2121

2222
import (
2323
"fmt"
24+
"io"
2425
"strconv"
2526
"strings"
2627
"time"
@@ -45,8 +46,10 @@ type SaraFlasher struct {
4546
payloadSize int
4647
}
4748

48-
func (f *SaraFlasher) FlashFirmware(firmwareFile *paths.Path) error {
49+
func (f *SaraFlasher) FlashFirmware(firmwareFile *paths.Path, flasherOut io.Writer) error {
4950
logrus.Infof("Flashing firmware %s", firmwareFile)
51+
flasherOut.Write([]byte(fmt.Sprintf("Flashing firmware %s", firmwareFile)))
52+
flasherOut.Write([]byte(fmt.Sprintln()))
5053
data, err := firmwareFile.ReadFile()
5154
if err != nil {
5255
logrus.Error(err)
@@ -102,6 +105,8 @@ func (f *SaraFlasher) FlashFirmware(firmwareFile *paths.Path) error {
102105
logrus.Error(err)
103106
}
104107
logrus.Infof("Flashed all the things")
108+
flasherOut.Write([]byte("Flashed all the things"))
109+
flasherOut.Write([]byte(fmt.Sprintln()))
105110
return err //should be nil
106111
}
107112

flasher/winc.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"encoding/binary"
2828
"errors"
2929
"fmt"
30+
"io"
3031
"strconv"
3132
"time"
3233

@@ -59,8 +60,10 @@ type WincFlasher struct {
5960
payloadSize int
6061
}
6162

62-
func (f *WincFlasher) FlashFirmware(firmwareFile *paths.Path) error {
63+
func (f *WincFlasher) FlashFirmware(firmwareFile *paths.Path, flasherOut io.Writer) error {
6364
logrus.Infof("Flashing firmware %s", firmwareFile)
65+
flasherOut.Write([]byte(fmt.Sprintf("Flashing firmware %s", firmwareFile)))
66+
flasherOut.Write([]byte(fmt.Sprintln()))
6467
data, err := firmwareFile.ReadFile()
6568
if err != nil {
6669
logrus.Error(err)
@@ -72,6 +75,8 @@ func (f *WincFlasher) FlashFirmware(firmwareFile *paths.Path) error {
7275
return err
7376
}
7477
logrus.Infof("Flashed all the things")
78+
flasherOut.Write([]byte("Flashed all the things"))
79+
flasherOut.Write([]byte(fmt.Sprintln()))
7580
return err //should be nil
7681
}
7782

0 commit comments

Comments
 (0)