Skip to content

add --format json output enhacement to flash and certificates commands #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions cli/certificates/flash.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package certificates

import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
}
34 changes: 29 additions & 5 deletions cli/firmware/flash.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package firmware

import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
}
24 changes: 22 additions & 2 deletions flasher/flasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package flasher

import (
"fmt"
"io"
"time"

"github.com/arduino/go-paths-helper"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ""
}
29 changes: 22 additions & 7 deletions flasher/nina.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"encoding/binary"
"encoding/pem"
"fmt"
"io"
"strconv"
"time"

Expand Down Expand Up @@ -64,8 +65,10 @@ 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", firmwareFile)))
flasherOut.Write([]byte(fmt.Sprintln()))
if err := f.hello(); err != nil {
logrus.Error(err)
return err
Expand All @@ -86,19 +89,22 @@ 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")
return nil
logrus.Infof("Flashed all the things")
flasherOut.Write([]byte("Flashed all the things"))
flasherOut.Write([]byte(fmt.Sprintln()))
return err //should be 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", certPath)))
flasherOut.Write([]byte(fmt.Sprintln()))

data, err := f.certificateFromFile(certPath)
if err != nil {
Expand All @@ -109,6 +115,8 @@ 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", URL)))
flasherOut.Write([]byte(fmt.Sprintln()))
data, err := f.certificateFromURL(URL)
if err != nil {
return err
Expand All @@ -129,7 +137,14 @@ 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"))
flasherOut.Write([]byte(fmt.Sprintln()))
return nil
}

func (f *NinaFlasher) certificateFromFile(certificateFile *paths.Path) ([]byte, error) {
Expand Down
12 changes: 9 additions & 3 deletions flasher/sara.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package flasher

import (
"fmt"
"io"
"strconv"
"strings"
"time"
Expand All @@ -45,8 +46,10 @@ 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", firmwareFile)))
flasherOut.Write([]byte(fmt.Sprintln()))
data, err := firmwareFile.ReadFile()
if err != nil {
logrus.Error(err)
Expand Down Expand Up @@ -101,10 +104,13 @@ 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"))
flasherOut.Write([]byte(fmt.Sprintln()))
return err //should be 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")
}

Expand Down
32 changes: 26 additions & 6 deletions flasher/winc.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"encoding/binary"
"errors"
"fmt"
"log"
"io"
"strconv"
"time"

Expand Down Expand Up @@ -60,22 +60,33 @@ 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", firmwareFile)))
flasherOut.Write([]byte(fmt.Sprintln()))
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"))
flasherOut.Write([]byte(fmt.Sprintln()))
return err //should be 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", certPath)))
flasherOut.Write([]byte(fmt.Sprintln()))

data, err := f.certificateFromFile(certPath)
if err != nil {
Expand All @@ -87,6 +98,8 @@ 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", URL)))
flasherOut.Write([]byte(fmt.Sprintln()))
data, err := f.certificateFromURL(URL)
if err != nil {
return err
Expand All @@ -96,7 +109,14 @@ 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"))
flasherOut.Write([]byte(fmt.Sprintln()))
return nil
}

func (f *WincFlasher) certificateFromFile(certificateFile *paths.Path) ([]byte, error) {
Expand Down Expand Up @@ -431,7 +451,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)
Expand Down