Skip to content

Commit 09bedb5

Browse files
committed
add buffer to FlashFirmware func to capture output for json printing
1 parent 56397f3 commit 09bedb5

File tree

6 files changed

+51
-10
lines changed

6 files changed

+51
-10
lines changed

cli/certificates/flash.go

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package certificates
2121

2222
import (
2323
"bytes"
24+
"fmt"
2425
"os"
2526
"path/filepath"
2627
"strings"

cli/firmware/flash.go

+30-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package firmware
2121

2222
import (
2323
"bytes"
24+
"fmt"
2425
"os"
2526
"path/filepath"
2627
"strings"
@@ -201,21 +202,44 @@ func run(cmd *cobra.Command, args []string) {
201202
}
202203
defer f.Close()
203204

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

209218
// Print the results
210219
feedback.PrintResult(&flashResult{
211-
ProgrammerOut: programmerOut.String(),
212-
ProgrammerErr: programmerErr.String(),
220+
Programmer: (&ExecOutput{
221+
Stdout: programmerOut.String(),
222+
Stderr: programmerErr.String(),
223+
}),
224+
Flasher: (&ExecOutput{
225+
Stdout: flasherOut.String(),
226+
Stderr: flasherErr.String(),
227+
}),
213228
})
229+
// Exit if something went wrong but after printing
230+
if err != nil {
231+
os.Exit(errorcodes.ErrGeneric)
232+
}
214233
}
215234

216235
type flashResult struct {
217-
ProgrammerOut string
218-
ProgrammerErr string
236+
Programmer *ExecOutput `json:"programmer"`
237+
Flasher *ExecOutput `json:"flasher"`
238+
}
239+
240+
type ExecOutput struct {
241+
Stdout string `json:"stdout"`
242+
Stderr string `json:"stderr"`
219243
}
220244

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

flasher/flasher.go

+2-1
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

+6-1
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

+6-1
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

+6-1
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)