Skip to content

Commit a3e9d70

Browse files
committed
apply same enhancements to FlashCertificates func
1 parent 3bd07eb commit a3e9d70

File tree

5 files changed

+75
-11
lines changed

5 files changed

+75
-11
lines changed

cli/certificates/flash.go

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

144144
// Flash loader Sketch
145-
flashOut := new(bytes.Buffer)
146-
flashErr := new(bytes.Buffer)
147-
// var err error
145+
programmerOut := new(bytes.Buffer)
146+
programmerErr := new(bytes.Buffer)
148147
if feedback.GetFormat() == feedback.JSON {
149-
err = programmer.Flash(commandLine, flashOut, flashErr)
148+
err = programmer.Flash(commandLine, programmerOut, programmerErr)
150149
} else {
151150
err = programmer.Flash(commandLine, os.Stdout, os.Stderr)
152151
}
@@ -175,9 +174,52 @@ func run(cmd *cobra.Command, args []string) {
175174
}
176175
defer f.Close()
177176

177+
// now flash the certificate
178+
flasherOut := new(bytes.Buffer)
179+
flasherErr := new(bytes.Buffer)
178180
certFileList := paths.NewPathList(certificatePaths...)
179-
if err := f.FlashCertificates(&certFileList, certificateURLs); err != nil {
181+
if feedback.GetFormat() == feedback.JSON {
182+
err = f.FlashCertificates(&certFileList, certificateURLs, flasherOut)
183+
} else {
184+
err = f.FlashCertificates(&certFileList, certificateURLs, os.Stdout)
185+
}
186+
if err != nil {
180187
feedback.Errorf("Error during certificates flashing: %s", err)
188+
flasherErr.Write([]byte(fmt.Sprintf("Error during certificates flashing: %s", err)))
189+
}
190+
191+
// Print the results
192+
feedback.PrintResult(&flashResult{
193+
Programmer: (&ExecOutput{
194+
Stdout: programmerOut.String(),
195+
Stderr: programmerErr.String(),
196+
}),
197+
Flasher: (&ExecOutput{
198+
Stdout: flasherOut.String(),
199+
Stderr: flasherErr.String(),
200+
}),
201+
})
202+
// Exit if something went wrong but after printing
203+
if err != nil {
181204
os.Exit(errorcodes.ErrGeneric)
182205
}
183206
}
207+
208+
type flashResult struct {
209+
Programmer *ExecOutput `json:"programmer"`
210+
Flasher *ExecOutput `json:"flasher"`
211+
}
212+
213+
type ExecOutput struct {
214+
Stdout string `json:"stdout"`
215+
Stderr string `json:"stderr"`
216+
}
217+
218+
func (r *flashResult) Data() interface{} {
219+
return r
220+
}
221+
222+
func (r *flashResult) String() string {
223+
// The output is already printed via os.Stdout/os.Stdin
224+
return ""
225+
}

flasher/flasher.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (e FlasherError) Error() string {
5050

5151
type Flasher interface {
5252
FlashFirmware(firmwareFile *paths.Path, flasherOut io.Writer) error
53-
FlashCertificates(certificatePaths *paths.PathList, URLs []string) error
53+
FlashCertificates(certificatePaths *paths.PathList, URLs []string, flasherOut io.Writer) error
5454
Close() error
5555

5656
hello() error

flasher/nina.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,12 @@ func (f *NinaFlasher) FlashFirmware(firmwareFile *paths.Path, flasherOut io.Writ
9999
return err //should be nil
100100
}
101101

102-
func (f *NinaFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs []string) error {
102+
func (f *NinaFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs []string, flasherOut io.Writer) error {
103103
var certificatesData []byte
104104
for _, certPath := range *certificatePaths {
105105
logrus.Infof("Converting and flashing certificate %s", certPath)
106+
flasherOut.Write([]byte(fmt.Sprintf("Converting and flashing certificate %s", certPath)))
107+
flasherOut.Write([]byte(fmt.Sprintln()))
106108

107109
data, err := f.certificateFromFile(certPath)
108110
if err != nil {
@@ -113,6 +115,8 @@ func (f *NinaFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs [
113115

114116
for _, URL := range URLs {
115117
logrus.Infof("Converting and flashing certificate from %s", URL)
118+
flasherOut.Write([]byte(fmt.Sprintf("Converting and flashing certificate from %s", URL)))
119+
flasherOut.Write([]byte(fmt.Sprintln()))
116120
data, err := f.certificateFromURL(URL)
117121
if err != nil {
118122
return err
@@ -133,7 +137,14 @@ func (f *NinaFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs [
133137
}
134138

135139
certificatesOffset := 0x10000
136-
return f.flashChunk(certificatesOffset, certificatesData)
140+
if err := f.flashChunk(certificatesOffset, certificatesData); err != nil {
141+
logrus.Error(err)
142+
return err
143+
}
144+
logrus.Infof("Flashed all the things")
145+
flasherOut.Write([]byte("Flashed all the things"))
146+
flasherOut.Write([]byte(fmt.Sprintln()))
147+
return nil
137148
}
138149

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

flasher/sara.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (f *SaraFlasher) FlashFirmware(firmwareFile *paths.Path, flasherOut io.Writ
110110
return err //should be nil
111111
}
112112

113-
func (f *SaraFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs []string) error {
113+
func (f *SaraFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs []string, _ io.Writer) error {
114114
return fmt.Errorf("not supported by SaraFlasher")
115115
}
116116

flasher/winc.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,13 @@ func (f *WincFlasher) FlashFirmware(firmwareFile *paths.Path, flasherOut io.Writ
8080
return err //should be nil
8181
}
8282

83-
func (f *WincFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs []string) error {
83+
func (f *WincFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs []string, flasherOut io.Writer) error {
8484
var certificatesData []byte
8585
certificatesNumber := 0
8686
for _, certPath := range *certificatePaths {
8787
logrus.Infof("Converting and flashing certificate %s", certPath)
88+
flasherOut.Write([]byte(fmt.Sprintf("Converting and flashing certificate %s", certPath)))
89+
flasherOut.Write([]byte(fmt.Sprintln()))
8890

8991
data, err := f.certificateFromFile(certPath)
9092
if err != nil {
@@ -96,6 +98,8 @@ func (f *WincFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs [
9698

9799
for _, URL := range URLs {
98100
logrus.Infof("Converting and flashing certificate from %s", URL)
101+
flasherOut.Write([]byte(fmt.Sprintf("Converting and flashing certificate from %s", URL)))
102+
flasherOut.Write([]byte(fmt.Sprintln()))
99103
data, err := f.certificateFromURL(URL)
100104
if err != nil {
101105
return err
@@ -105,7 +109,14 @@ func (f *WincFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs [
105109
}
106110

107111
certificatesOffset := 0x4000
108-
return f.flashChunk(certificatesOffset, certificatesData)
112+
if err := f.flashChunk(certificatesOffset, certificatesData); err != nil {
113+
logrus.Error(err)
114+
return err
115+
}
116+
logrus.Infof("Flashed all the things")
117+
flasherOut.Write([]byte("Flashed all the things"))
118+
flasherOut.Write([]byte(fmt.Sprintln()))
119+
return nil
109120
}
110121

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

0 commit comments

Comments
 (0)