Skip to content

Commit e5f422a

Browse files
authored
add --format json output enhacement to flash and certificates commands (#56)
* enable programmer output to be printed in json * minor optimizations * add buffer to FlashFirmware func to capture output for json printing * apply same enhancements to FlashCertificates func * remove some code duplication * apply suggestion from code review
1 parent 43dfacf commit e5f422a

File tree

6 files changed

+122
-27
lines changed

6 files changed

+122
-27
lines changed

cli/certificates/flash.go

+28-5
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,10 @@ func run(cmd *cobra.Command, args []string) {
143143
}
144144

145145
// Flash loader Sketch
146-
flashOut := new(bytes.Buffer)
147-
flashErr := new(bytes.Buffer)
148-
// var err error
146+
programmerOut := new(bytes.Buffer)
147+
programmerErr := new(bytes.Buffer)
149148
if feedback.GetFormat() == feedback.JSON {
150-
err = programmer.Flash(commandLine, flashOut, flashErr)
149+
err = programmer.Flash(commandLine, programmerOut, programmerErr)
151150
} else {
152151
err = programmer.Flash(commandLine, os.Stdout, os.Stderr)
153152
}
@@ -179,9 +178,33 @@ func run(cmd *cobra.Command, args []string) {
179178
}
180179
defer f.Close()
181180

181+
// now flash the certificate
182+
flasherOut := new(bytes.Buffer)
183+
flasherErr := new(bytes.Buffer)
182184
certFileList := paths.NewPathList(certificatePaths...)
183-
if err := f.FlashCertificates(&certFileList, certificateURLs); err != nil {
185+
if feedback.GetFormat() == feedback.JSON {
186+
err = f.FlashCertificates(&certFileList, certificateURLs, flasherOut)
187+
} else {
188+
err = f.FlashCertificates(&certFileList, certificateURLs, os.Stdout)
189+
}
190+
if err != nil {
184191
feedback.Errorf("Error during certificates flashing: %s", err)
192+
flasherErr.Write([]byte(fmt.Sprintf("Error during certificates flashing: %s", err)))
193+
}
194+
195+
// Print the results
196+
feedback.PrintResult(&flasher.FlashResult{
197+
Programmer: (&flasher.ExecOutput{
198+
Stdout: programmerOut.String(),
199+
Stderr: programmerErr.String(),
200+
}),
201+
Flasher: (&flasher.ExecOutput{
202+
Stdout: flasherOut.String(),
203+
Stderr: flasherErr.String(),
204+
}),
205+
})
206+
// Exit if something went wrong but after printing
207+
if err != nil {
185208
os.Exit(errorcodes.ErrGeneric)
186209
}
187210
}

cli/firmware/flash.go

+28-5
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,10 @@ func run(cmd *cobra.Command, args []string) {
170170
}
171171

172172
// Flash loader Sketch
173-
flashOut := new(bytes.Buffer)
174-
flashErr := new(bytes.Buffer)
175-
// var err error
173+
programmerOut := new(bytes.Buffer)
174+
programmerErr := new(bytes.Buffer)
176175
if feedback.GetFormat() == feedback.JSON {
177-
err = programmer.Flash(commandLine, flashOut, flashErr)
176+
err = programmer.Flash(commandLine, programmerOut, programmerErr)
178177
} else {
179178
err = programmer.Flash(commandLine, os.Stdout, os.Stderr)
180179
}
@@ -205,8 +204,32 @@ func run(cmd *cobra.Command, args []string) {
205204
}
206205
defer f.Close()
207206

208-
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 {
209216
feedback.Errorf("Error during firmware flashing: %s", err)
217+
flasherErr.Write([]byte(fmt.Sprintf("Error during firmware flashing: %s", err)))
218+
}
219+
220+
// Print the results
221+
feedback.PrintResult(&flasher.FlashResult{
222+
Programmer: (&flasher.ExecOutput{
223+
Stdout: programmerOut.String(),
224+
Stderr: programmerErr.String(),
225+
}),
226+
Flasher: (&flasher.ExecOutput{
227+
Stdout: flasherOut.String(),
228+
Stderr: flasherErr.String(),
229+
}),
230+
})
231+
// Exit if something went wrong but after printing
232+
if err != nil {
210233
os.Exit(errorcodes.ErrGeneric)
211234
}
212235
}

flasher/flasher.go

+22-2
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,8 +49,8 @@ func (e FlasherError) Error() string {
4849
}
4950

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

5556
hello() error
@@ -92,3 +93,22 @@ func openSerial(portAddress string) (serial.Port, error) {
9293

9394
return nil, lastError
9495
}
96+
97+
type FlashResult struct {
98+
Programmer *ExecOutput `json:"programmer"`
99+
Flasher *ExecOutput `json:"flasher"`
100+
}
101+
102+
type ExecOutput struct {
103+
Stdout string `json:"stdout"`
104+
Stderr string `json:"stderr"`
105+
}
106+
107+
func (r *FlashResult) Data() interface{} {
108+
return r
109+
}
110+
111+
func (r *FlashResult) String() string {
112+
// The output is already printed via os.Stdout/os.Stdin
113+
return ""
114+
}

flasher/nina.go

+16-6
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,9 @@ 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\n", firmwareFile)))
6971
if err := f.hello(); err != nil {
7072
logrus.Error(err)
7173
return err
@@ -86,19 +88,20 @@ func (f *NinaFlasher) FlashFirmware(firmwareFile *paths.Path) error {
8688
}
8789

8890
logrus.Debugf("Checking md5")
89-
err = f.md5sum(data)
90-
if err != nil {
91+
if err := f.md5sum(data); err != nil {
9192
logrus.Error(err)
9293
return err
9394
}
94-
logrus.Debugf("Flashed all the things")
95+
logrus.Infof("Flashed all the things")
96+
flasherOut.Write([]byte("Flashed all the things\n"))
9597
return nil
9698
}
9799

98-
func (f *NinaFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs []string) error {
100+
func (f *NinaFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs []string, flasherOut io.Writer) error {
99101
var certificatesData []byte
100102
for _, certPath := range *certificatePaths {
101103
logrus.Infof("Converting and flashing certificate %s", certPath)
104+
flasherOut.Write([]byte(fmt.Sprintf("Converting and flashing certificate %s\n", certPath)))
102105

103106
data, err := f.certificateFromFile(certPath)
104107
if err != nil {
@@ -109,6 +112,7 @@ func (f *NinaFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs [
109112

110113
for _, URL := range URLs {
111114
logrus.Infof("Converting and flashing certificate from %s", URL)
115+
flasherOut.Write([]byte(fmt.Sprintf("Converting and flashing certificate from %s\n", URL)))
112116
data, err := f.certificateFromURL(URL)
113117
if err != nil {
114118
return err
@@ -129,7 +133,13 @@ func (f *NinaFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs [
129133
}
130134

131135
certificatesOffset := 0x10000
132-
return f.flashChunk(certificatesOffset, certificatesData)
136+
if err := f.flashChunk(certificatesOffset, certificatesData); err != nil {
137+
logrus.Error(err)
138+
return err
139+
}
140+
logrus.Infof("Flashed all the things")
141+
flasherOut.Write([]byte("Flashed all the things\n"))
142+
return nil
133143
}
134144

135145
func (f *NinaFlasher) certificateFromFile(certificateFile *paths.Path) ([]byte, error) {

flasher/sara.go

+7-3
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,9 @@ 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\n", firmwareFile)))
5052
data, err := firmwareFile.ReadFile()
5153
if err != nil {
5254
logrus.Error(err)
@@ -101,10 +103,12 @@ func (f *SaraFlasher) FlashFirmware(firmwareFile *paths.Path) error {
101103
if err != nil {
102104
logrus.Error(err)
103105
}
104-
return err
106+
logrus.Infof("Flashed all the things")
107+
flasherOut.Write([]byte("Flashed all the things\n"))
108+
return nil
105109
}
106110

107-
func (f *SaraFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs []string) error {
111+
func (f *SaraFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs []string, _ io.Writer) error {
108112
return fmt.Errorf("not supported by SaraFlasher")
109113
}
110114

flasher/winc.go

+21-6
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
"encoding/binary"
2828
"errors"
2929
"fmt"
30-
"log"
30+
"io"
3131
"strconv"
3232
"time"
3333

@@ -60,22 +60,30 @@ type WincFlasher struct {
6060
payloadSize int
6161
}
6262

63-
func (f *WincFlasher) FlashFirmware(firmwareFile *paths.Path) error {
63+
func (f *WincFlasher) FlashFirmware(firmwareFile *paths.Path, flasherOut io.Writer) error {
6464
logrus.Infof("Flashing firmware %s", firmwareFile)
65+
flasherOut.Write([]byte(fmt.Sprintf("Flashing firmware %s\n", firmwareFile)))
6566
data, err := firmwareFile.ReadFile()
6667
if err != nil {
6768
logrus.Error(err)
6869
return err
6970
}
7071
firmwareOffset := 0x0000
71-
return f.flashChunk(firmwareOffset, data)
72+
if err = f.flashChunk(firmwareOffset, data); err != nil {
73+
logrus.Error(err)
74+
return err
75+
}
76+
logrus.Infof("Flashed all the things")
77+
flasherOut.Write([]byte("Flashed all the things\n"))
78+
return nil
7279
}
7380

74-
func (f *WincFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs []string) error {
81+
func (f *WincFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs []string, flasherOut io.Writer) error {
7582
var certificatesData []byte
7683
certificatesNumber := 0
7784
for _, certPath := range *certificatePaths {
7885
logrus.Infof("Converting and flashing certificate %s", certPath)
86+
flasherOut.Write([]byte(fmt.Sprintf("Converting and flashing certificate %s\n", certPath)))
7987

8088
data, err := f.certificateFromFile(certPath)
8189
if err != nil {
@@ -87,6 +95,7 @@ func (f *WincFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs [
8795

8896
for _, URL := range URLs {
8997
logrus.Infof("Converting and flashing certificate from %s", URL)
98+
flasherOut.Write([]byte(fmt.Sprintf("Converting and flashing certificate from %s\n", URL)))
9099
data, err := f.certificateFromURL(URL)
91100
if err != nil {
92101
return err
@@ -96,7 +105,13 @@ func (f *WincFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs [
96105
}
97106

98107
certificatesOffset := 0x4000
99-
return f.flashChunk(certificatesOffset, certificatesData)
108+
if err := f.flashChunk(certificatesOffset, certificatesData); err != nil {
109+
logrus.Error(err)
110+
return err
111+
}
112+
logrus.Infof("Flashed all the things")
113+
flasherOut.Write([]byte("Flashed all the things\n"))
114+
return nil
100115
}
101116

102117
func (f *WincFlasher) certificateFromFile(certificateFile *paths.Path) ([]byte, error) {
@@ -431,7 +446,7 @@ func (f *WincFlasher) erase(address uint32, length uint32) error {
431446
return err
432447
}
433448

434-
log.Printf("Erasing %d bytes from address 0x%X\n", length, address)
449+
logrus.Debugf("Erasing %d bytes from address 0x%X\n", length, address)
435450

436451
// wait acknowledge
437452
ack := make([]byte, 2)

0 commit comments

Comments
 (0)