diff --git a/cli/certificates/flash.go b/cli/certificates/flash.go index 421209f9..6093457c 100644 --- a/cli/certificates/flash.go +++ b/cli/certificates/flash.go @@ -21,6 +21,7 @@ package certificates import ( "bytes" + "fmt" "os" "path/filepath" "strings" @@ -142,11 +143,10 @@ func run(cmd *cobra.Command, args []string) { } // Flash loader Sketch - flashOut := new(bytes.Buffer) - flashErr := new(bytes.Buffer) - // var err error + programmerOut := new(bytes.Buffer) + programmerErr := new(bytes.Buffer) if feedback.GetFormat() == feedback.JSON { - err = programmer.Flash(commandLine, flashOut, flashErr) + err = programmer.Flash(commandLine, programmerOut, programmerErr) } else { err = programmer.Flash(commandLine, os.Stdout, os.Stderr) } @@ -175,9 +175,33 @@ func run(cmd *cobra.Command, args []string) { } defer f.Close() + // now flash the certificate + flasherOut := new(bytes.Buffer) + flasherErr := new(bytes.Buffer) certFileList := paths.NewPathList(certificatePaths...) - if err := f.FlashCertificates(&certFileList, certificateURLs); err != nil { + if feedback.GetFormat() == feedback.JSON { + err = f.FlashCertificates(&certFileList, certificateURLs, flasherOut) + } else { + err = f.FlashCertificates(&certFileList, certificateURLs, os.Stdout) + } + if err != nil { feedback.Errorf("Error during certificates flashing: %s", err) + flasherErr.Write([]byte(fmt.Sprintf("Error during certificates flashing: %s", err))) + } + + // Print the results + feedback.PrintResult(&flasher.FlashResult{ + Programmer: (&flasher.ExecOutput{ + Stdout: programmerOut.String(), + Stderr: programmerErr.String(), + }), + Flasher: (&flasher.ExecOutput{ + Stdout: flasherOut.String(), + Stderr: flasherErr.String(), + }), + }) + // Exit if something went wrong but after printing + if err != nil { os.Exit(errorcodes.ErrGeneric) } } diff --git a/cli/firmware/flash.go b/cli/firmware/flash.go index 3d3f98fe..744c7db9 100644 --- a/cli/firmware/flash.go +++ b/cli/firmware/flash.go @@ -21,6 +21,7 @@ package firmware import ( "bytes" + "fmt" "os" "path/filepath" "strings" @@ -169,11 +170,10 @@ func run(cmd *cobra.Command, args []string) { } // Flash loader Sketch - flashOut := new(bytes.Buffer) - flashErr := new(bytes.Buffer) - // var err error + programmerOut := new(bytes.Buffer) + programmerErr := new(bytes.Buffer) if feedback.GetFormat() == feedback.JSON { - err = programmer.Flash(commandLine, flashOut, flashErr) + err = programmer.Flash(commandLine, programmerOut, programmerErr) } else { err = programmer.Flash(commandLine, os.Stdout, os.Stderr) } @@ -202,8 +202,32 @@ func run(cmd *cobra.Command, args []string) { } defer f.Close() - if err := f.FlashFirmware(firmwareFile); err != nil { + // now flash the actual firmware + flasherOut := new(bytes.Buffer) + flasherErr := new(bytes.Buffer) + if feedback.GetFormat() == feedback.JSON { + err = f.FlashFirmware(firmwareFile, flasherOut) + } else { + err = f.FlashFirmware(firmwareFile, os.Stdout) + } + if err != nil { feedback.Errorf("Error during firmware flashing: %s", err) + flasherErr.Write([]byte(fmt.Sprintf("Error during firmware flashing: %s", err))) + } + + // Print the results + feedback.PrintResult(&flasher.FlashResult{ + Programmer: (&flasher.ExecOutput{ + Stdout: programmerOut.String(), + Stderr: programmerErr.String(), + }), + Flasher: (&flasher.ExecOutput{ + Stdout: flasherOut.String(), + Stderr: flasherErr.String(), + }), + }) + // Exit if something went wrong but after printing + if err != nil { os.Exit(errorcodes.ErrGeneric) } } diff --git a/flasher/flasher.go b/flasher/flasher.go index 7dae5c11..6e25cfe6 100644 --- a/flasher/flasher.go +++ b/flasher/flasher.go @@ -21,6 +21,7 @@ package flasher import ( "fmt" + "io" "time" "github.com/arduino/go-paths-helper" @@ -48,8 +49,8 @@ func (e FlasherError) Error() string { } type Flasher interface { - FlashFirmware(firmwareFile *paths.Path) error - FlashCertificates(certificatePaths *paths.PathList, URLs []string) error + FlashFirmware(firmwareFile *paths.Path, flasherOut io.Writer) error + FlashCertificates(certificatePaths *paths.PathList, URLs []string, flasherOut io.Writer) error Close() error hello() error @@ -92,3 +93,22 @@ func openSerial(portAddress string) (serial.Port, error) { return nil, lastError } + +type FlashResult struct { + Programmer *ExecOutput `json:"programmer"` + Flasher *ExecOutput `json:"flasher"` +} + +type ExecOutput struct { + Stdout string `json:"stdout"` + Stderr string `json:"stderr"` +} + +func (r *FlashResult) Data() interface{} { + return r +} + +func (r *FlashResult) String() string { + // The output is already printed via os.Stdout/os.Stdin + return "" +} diff --git a/flasher/nina.go b/flasher/nina.go index a9eb42cc..aa07cd38 100644 --- a/flasher/nina.go +++ b/flasher/nina.go @@ -27,6 +27,7 @@ import ( "encoding/binary" "encoding/pem" "fmt" + "io" "strconv" "time" @@ -64,8 +65,9 @@ type NinaFlasher struct { } // FlashFirmware in board connected to port using data from firmwareFile -func (f *NinaFlasher) FlashFirmware(firmwareFile *paths.Path) error { +func (f *NinaFlasher) FlashFirmware(firmwareFile *paths.Path, flasherOut io.Writer) error { logrus.Infof("Flashing firmware %s", firmwareFile) + flasherOut.Write([]byte(fmt.Sprintf("Flashing firmware %s\n", firmwareFile))) if err := f.hello(); err != nil { logrus.Error(err) return err @@ -86,19 +88,20 @@ func (f *NinaFlasher) FlashFirmware(firmwareFile *paths.Path) error { } logrus.Debugf("Checking md5") - err = f.md5sum(data) - if err != nil { + if err := f.md5sum(data); err != nil { logrus.Error(err) return err } - logrus.Debugf("Flashed all the things") + logrus.Infof("Flashed all the things") + flasherOut.Write([]byte("Flashed all the things\n")) return nil } -func (f *NinaFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs []string) error { +func (f *NinaFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs []string, flasherOut io.Writer) error { var certificatesData []byte for _, certPath := range *certificatePaths { logrus.Infof("Converting and flashing certificate %s", certPath) + flasherOut.Write([]byte(fmt.Sprintf("Converting and flashing certificate %s\n", certPath))) data, err := f.certificateFromFile(certPath) if err != nil { @@ -109,6 +112,7 @@ func (f *NinaFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs [ for _, URL := range URLs { logrus.Infof("Converting and flashing certificate from %s", URL) + flasherOut.Write([]byte(fmt.Sprintf("Converting and flashing certificate from %s\n", URL))) data, err := f.certificateFromURL(URL) if err != nil { return err @@ -129,7 +133,13 @@ func (f *NinaFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs [ } certificatesOffset := 0x10000 - return f.flashChunk(certificatesOffset, certificatesData) + if err := f.flashChunk(certificatesOffset, certificatesData); err != nil { + logrus.Error(err) + return err + } + logrus.Infof("Flashed all the things") + flasherOut.Write([]byte("Flashed all the things\n")) + return nil } func (f *NinaFlasher) certificateFromFile(certificateFile *paths.Path) ([]byte, error) { diff --git a/flasher/sara.go b/flasher/sara.go index 876143b4..12ec9751 100644 --- a/flasher/sara.go +++ b/flasher/sara.go @@ -21,6 +21,7 @@ package flasher import ( "fmt" + "io" "strconv" "strings" "time" @@ -45,8 +46,9 @@ type SaraFlasher struct { payloadSize int } -func (f *SaraFlasher) FlashFirmware(firmwareFile *paths.Path) error { +func (f *SaraFlasher) FlashFirmware(firmwareFile *paths.Path, flasherOut io.Writer) error { logrus.Infof("Flashing firmware %s", firmwareFile) + flasherOut.Write([]byte(fmt.Sprintf("Flashing firmware %s\n", firmwareFile))) data, err := firmwareFile.ReadFile() if err != nil { logrus.Error(err) @@ -101,10 +103,12 @@ func (f *SaraFlasher) FlashFirmware(firmwareFile *paths.Path) error { if err != nil { logrus.Error(err) } - return err + logrus.Infof("Flashed all the things") + flasherOut.Write([]byte("Flashed all the things\n")) + return nil } -func (f *SaraFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs []string) error { +func (f *SaraFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs []string, _ io.Writer) error { return fmt.Errorf("not supported by SaraFlasher") } diff --git a/flasher/winc.go b/flasher/winc.go index 4ef972a4..a28a70fb 100644 --- a/flasher/winc.go +++ b/flasher/winc.go @@ -27,7 +27,7 @@ import ( "encoding/binary" "errors" "fmt" - "log" + "io" "strconv" "time" @@ -60,22 +60,30 @@ type WincFlasher struct { payloadSize int } -func (f *WincFlasher) FlashFirmware(firmwareFile *paths.Path) error { +func (f *WincFlasher) FlashFirmware(firmwareFile *paths.Path, flasherOut io.Writer) error { logrus.Infof("Flashing firmware %s", firmwareFile) + flasherOut.Write([]byte(fmt.Sprintf("Flashing firmware %s\n", firmwareFile))) data, err := firmwareFile.ReadFile() if err != nil { logrus.Error(err) return err } firmwareOffset := 0x0000 - return f.flashChunk(firmwareOffset, data) + if err = f.flashChunk(firmwareOffset, data); err != nil { + logrus.Error(err) + return err + } + logrus.Infof("Flashed all the things") + flasherOut.Write([]byte("Flashed all the things\n")) + return nil } -func (f *WincFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs []string) error { +func (f *WincFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs []string, flasherOut io.Writer) error { var certificatesData []byte certificatesNumber := 0 for _, certPath := range *certificatePaths { logrus.Infof("Converting and flashing certificate %s", certPath) + flasherOut.Write([]byte(fmt.Sprintf("Converting and flashing certificate %s\n", certPath))) data, err := f.certificateFromFile(certPath) if err != nil { @@ -87,6 +95,7 @@ func (f *WincFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs [ for _, URL := range URLs { logrus.Infof("Converting and flashing certificate from %s", URL) + flasherOut.Write([]byte(fmt.Sprintf("Converting and flashing certificate from %s\n", URL))) data, err := f.certificateFromURL(URL) if err != nil { return err @@ -96,7 +105,13 @@ func (f *WincFlasher) FlashCertificates(certificatePaths *paths.PathList, URLs [ } certificatesOffset := 0x4000 - return f.flashChunk(certificatesOffset, certificatesData) + if err := f.flashChunk(certificatesOffset, certificatesData); err != nil { + logrus.Error(err) + return err + } + logrus.Infof("Flashed all the things") + flasherOut.Write([]byte("Flashed all the things\n")) + return nil } func (f *WincFlasher) certificateFromFile(certificateFile *paths.Path) ([]byte, error) { @@ -431,7 +446,7 @@ func (f *WincFlasher) erase(address uint32, length uint32) error { return err } - log.Printf("Erasing %d bytes from address 0x%X\n", length, address) + logrus.Debugf("Erasing %d bytes from address 0x%X\n", length, address) // wait acknowledge ack := make([]byte, 2)