Skip to content

Commit 3aa8ec1

Browse files
authored
add back --retries flag (#61)
* add retries flag (just like in #24) * forgot continue statement * applied suggestions * the json is printed even in case of error now
1 parent d4c7b34 commit 3aa8ec1

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

cli/firmware/flash.go

+28-8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/arduino/arduino-cli/arduino/serialutils"
3636
"github.com/arduino/arduino-cli/cli/errorcodes"
3737
"github.com/arduino/arduino-cli/cli/feedback"
38+
"github.com/arduino/go-paths-helper"
3839
"github.com/arduino/go-properties-orderedmap"
3940
"github.com/sirupsen/logrus"
4041
"github.com/spf13/cobra"
@@ -44,6 +45,7 @@ var (
4445
fqbn string
4546
address string
4647
module string
48+
retries uint8
4749
)
4850

4951
// NewCommand created a new `version` command
@@ -63,6 +65,7 @@ func NewFlashCommand() *cobra.Command {
6365
command.Flags().StringVarP(&fqbn, "fqbn", "b", "", "Fully Qualified Board Name, e.g.: arduino:samd:mkr1000, arduino:mbed_nano:nanorp2040connect")
6466
command.Flags().StringVarP(&address, "address", "a", "", "Upload port, e.g.: COM10, /dev/ttyACM0")
6567
command.Flags().StringVarP(&module, "module", "m", "", "Firmware module ID, e.g.: WINC1500, NINA")
68+
command.Flags().Uint8Var(&retries, "retries", 9, "Number of retries in case of upload failure (default 9)")
6669
return command
6770
}
6871

@@ -159,14 +162,31 @@ func run(cmd *cobra.Command, args []string) {
159162
os.Exit(errorcodes.ErrGeneric)
160163
}
161164

165+
for retry := 1; retry <= int(retries); retry++ {
166+
err = updateFirmware(board, commandLine, moduleName, firmwareFile)
167+
if err == nil {
168+
logrus.Info("Operation completed: success! :-)")
169+
break
170+
}
171+
feedback.Error(err)
172+
if retry == int(retries) {
173+
logrus.Fatal("Operation failed. :-(")
174+
}
175+
logrus.Info("Waiting 1 second before retrying...")
176+
time.Sleep(time.Second)
177+
logrus.Infof("Retrying upload (%d of %d)", retry, retries)
178+
}
179+
}
180+
181+
func updateFirmware(board *firmwareindex.IndexBoard, commandLine []string, moduleName string, firmwareFile *paths.Path) error {
182+
var err error
162183
// Check if board needs a 1200bps touch for upload
163184
uploadPort := address
164185
if board.UploadTouch {
165186
logrus.Info("Putting board into bootloader mode")
166187
newUploadPort, err := serialutils.Reset(address, board.UploadWait, nil)
167188
if err != nil {
168-
feedback.Errorf("Error during firmware flashing: missing board address")
169-
os.Exit(errorcodes.ErrGeneric)
189+
return fmt.Errorf("error during firmware flashing: missing board address. %s", err)
170190
}
171191
if newUploadPort != "" {
172192
logrus.Infof("Found port to upload Loader: %s", newUploadPort)
@@ -183,8 +203,7 @@ func run(cmd *cobra.Command, args []string) {
183203
err = programmer.Flash(commandLine, os.Stdout, os.Stderr)
184204
}
185205
if err != nil {
186-
feedback.Errorf("Error during firmware flashing: %s", err)
187-
os.Exit(errorcodes.ErrGeneric)
206+
return fmt.Errorf("error during loader sketch flashing: %s", err)
188207
}
189208

190209
// Wait a bit after flashing the loader sketch for the board to become
@@ -202,10 +221,12 @@ func run(cmd *cobra.Command, args []string) {
202221
f, err = flasher.NewWincFlasher(uploadPort)
203222
default:
204223
err = fmt.Errorf("unknown module: %s", moduleName)
224+
feedback.Errorf("Error during firmware flashing: %s", err)
225+
os.Exit(errorcodes.ErrGeneric)
205226
}
206227
if err != nil {
207228
feedback.Errorf("Error during firmware flashing: %s", err)
208-
os.Exit(errorcodes.ErrGeneric)
229+
return err
209230
}
210231
defer f.Close()
211232

@@ -218,7 +239,6 @@ func run(cmd *cobra.Command, args []string) {
218239
err = f.FlashFirmware(firmwareFile, os.Stdout)
219240
}
220241
if err != nil {
221-
feedback.Errorf("Error during firmware flashing: %s", err)
222242
flasherErr.Write([]byte(fmt.Sprintf("Error during firmware flashing: %s", err)))
223243
}
224244

@@ -233,8 +253,8 @@ func run(cmd *cobra.Command, args []string) {
233253
Stderr: flasherErr.String(),
234254
}),
235255
})
236-
// Exit if something went wrong but after printing
237256
if err != nil {
238-
os.Exit(errorcodes.ErrGeneric)
257+
return fmt.Errorf("error during firmware flashing: %s", err)
239258
}
259+
return nil
240260
}

0 commit comments

Comments
 (0)