Skip to content

Commit b501a51

Browse files
committed
refactor business logic to prepare for ´get-version´ command
1 parent a733f34 commit b501a51

File tree

4 files changed

+178
-176
lines changed

4 files changed

+178
-176
lines changed

cli/arguments/arguments.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package arguments
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
// Flags contains various common flags.
8+
// This is useful so all flags used by commands that need
9+
// this information are consistent with each other.
10+
type Flags struct {
11+
Address string
12+
Fqbn string
13+
}
14+
15+
// AddToCommand adds the flags used to set address and fqbn to the specified Command
16+
func (f *Flags) AddToCommand(cmd *cobra.Command) {
17+
cmd.Flags().StringVarP(&f.Fqbn, "fqbn", "b", "", "Fully Qualified Board Name, e.g.: arduino:samd:mkr1000, arduino:mbed_nano:nanorp2040connect")
18+
cmd.Flags().StringVarP(&f.Address, "address", "a", "", "Upload port, e.g.: COM10, /dev/ttyACM0")
19+
}

cli/certificates/flash.go

+12-85
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,21 @@ import (
2323
"bytes"
2424
"fmt"
2525
"os"
26-
"path/filepath"
2726
"strings"
2827
"time"
2928

30-
"github.com/arduino/arduino-cli/arduino/serialutils"
3129
"github.com/arduino/arduino-cli/cli/errorcodes"
3230
"github.com/arduino/arduino-cli/cli/feedback"
31+
"github.com/arduino/arduino-fwuploader/cli/arguments"
32+
"github.com/arduino/arduino-fwuploader/cli/common"
3333
"github.com/arduino/arduino-fwuploader/flasher"
34-
"github.com/arduino/arduino-fwuploader/indexes"
3534
"github.com/arduino/arduino-fwuploader/indexes/download"
36-
programmer "github.com/arduino/arduino-fwuploader/programmers"
3735
"github.com/arduino/go-paths-helper"
38-
"github.com/arduino/go-properties-orderedmap"
39-
"github.com/sirupsen/logrus"
4036
"github.com/spf13/cobra"
4137
)
4238

4339
var (
44-
fqbn string
45-
address string
40+
commonFlags arguments.Flags
4641
certificateURLs []string
4742
certificatePaths []string
4843
)
@@ -58,61 +53,26 @@ func NewFlashCommand() *cobra.Command {
5853
" " + os.Args[0] + " certificates flash -b arduino:samd:mkr1000 -a COM10 -u arduino.cc:443 -u google.cc:443\n" +
5954
" " + os.Args[0] + " certificates flash -b arduino:samd:mkr1000 -a COM10 -f /home/me/VeriSign.cer -f /home/me/Digicert.cer\n",
6055
Args: cobra.NoArgs,
61-
Run: run,
56+
Run: runFlash,
6257
}
63-
64-
command.Flags().StringVarP(&fqbn, "fqbn", "b", "", "Fully Qualified Board Name, e.g.: arduino:samd:mkr1000, arduino:mbed_nano:nanorp2040connect")
65-
command.Flags().StringVarP(&address, "address", "a", "", "Upload port, e.g.: COM10, /dev/ttyACM0")
58+
commonFlags.AddToCommand(command)
6659
command.Flags().StringSliceVarP(&certificateURLs, "url", "u", []string{}, "List of urls to download root certificates, e.g.: arduino.cc:443")
6760
command.Flags().StringSliceVarP(&certificatePaths, "file", "f", []string{}, "List of paths to certificate file, e.g.: /home/me/Digicert.cer")
6861
return command
6962
}
7063

71-
func run(cmd *cobra.Command, args []string) {
72-
packageIndex, err := indexes.GetPackageIndex()
73-
if err != nil {
74-
feedback.Errorf("Can't load package index: %s", err)
75-
os.Exit(errorcodes.ErrGeneric)
76-
}
64+
func runFlash(cmd *cobra.Command, args []string) {
7765

78-
firmwareIndex, err := indexes.GetFirmwareIndex()
79-
if err != nil {
80-
feedback.Errorf("Can't load firmware index: %s", err)
81-
os.Exit(errorcodes.ErrGeneric)
82-
}
83-
84-
if fqbn == "" {
85-
feedback.Errorf("Error during certificates flashing: missing board fqbn")
86-
os.Exit(errorcodes.ErrBadArgument)
87-
}
88-
89-
if address == "" {
90-
feedback.Errorf("Error during certificates flashing: missing board address")
91-
os.Exit(errorcodes.ErrBadArgument)
92-
}
66+
packageIndex, firmwareIndex := common.InitIndexes()
67+
common.CheckFlags(commonFlags.Fqbn, commonFlags.Address)
68+
board := common.GetBoard(firmwareIndex, commonFlags.Fqbn)
69+
uploadToolDir := common.GetUploadToolDir(packageIndex, board)
9370

9471
if len(certificateURLs) == 0 && len(certificatePaths) == 0 {
9572
feedback.Errorf("Error during certificates flashing: no certificates provided")
9673
os.Exit(errorcodes.ErrBadArgument)
9774
}
9875

99-
board := firmwareIndex.GetBoard(fqbn)
100-
if board == nil {
101-
feedback.Errorf("Can't find board with %s fqbn", fqbn)
102-
os.Exit(errorcodes.ErrBadArgument)
103-
}
104-
105-
toolRelease := indexes.GetToolRelease(packageIndex, board.Uploader)
106-
if toolRelease == nil {
107-
feedback.Errorf("Error getting upload tool %s for board %s", board.Uploader, board.Fqbn)
108-
os.Exit(errorcodes.ErrGeneric)
109-
}
110-
uploadToolDir, err := download.DownloadTool(toolRelease)
111-
if err != nil {
112-
feedback.Errorf("Error downloading tool %s: %s", board.Uploader, err)
113-
os.Exit(errorcodes.ErrGeneric)
114-
}
115-
11676
loaderSketchPath, err := download.DownloadSketch(board.LoaderSketch)
11777
if err != nil {
11878
feedback.Errorf("Error downloading loader sketch from %s: %s", board.LoaderSketch.URL, err)
@@ -121,42 +81,9 @@ func run(cmd *cobra.Command, args []string) {
12181

12282
loaderSketch := strings.ReplaceAll(loaderSketchPath.String(), loaderSketchPath.Ext(), "")
12383

124-
// Check if board needs a 1200bps touch for upload
125-
bootloaderPort := address
126-
if board.UploadTouch {
127-
logrus.Info("Putting board into bootloader mode")
128-
newUploadPort, err := serialutils.Reset(address, board.UploadWait, nil)
129-
if err != nil {
130-
feedback.Errorf("Error during certificates flashing: missing board address")
131-
os.Exit(errorcodes.ErrGeneric)
132-
}
133-
if newUploadPort != "" {
134-
logrus.Infof("Found port to upload Loader: %s", newUploadPort)
135-
bootloaderPort = newUploadPort
136-
}
137-
}
138-
139-
uploaderCommand := board.GetUploaderCommand()
140-
uploaderCommand = strings.ReplaceAll(uploaderCommand, "{tool_dir}", filepath.FromSlash(uploadToolDir.String()))
141-
uploaderCommand = strings.ReplaceAll(uploaderCommand, "{serial.port.file}", bootloaderPort)
142-
uploaderCommand = strings.ReplaceAll(uploaderCommand, "{loader.sketch}", loaderSketch)
143-
144-
commandLine, err := properties.SplitQuotedString(uploaderCommand, "\"", false)
84+
programmerOut, programmerErr, err := common.FlashSketch(board, loaderSketch, uploadToolDir, commonFlags.Address)
14585
if err != nil {
146-
feedback.Errorf(`Error splitting command line "%s": %s`, uploaderCommand, err)
147-
os.Exit(errorcodes.ErrGeneric)
148-
}
149-
150-
// Flash loader Sketch
151-
programmerOut := new(bytes.Buffer)
152-
programmerErr := new(bytes.Buffer)
153-
if feedback.GetFormat() == feedback.JSON {
154-
err = programmer.Flash(commandLine, programmerOut, programmerErr)
155-
} else {
156-
err = programmer.Flash(commandLine, os.Stdout, os.Stderr)
157-
}
158-
if err != nil {
159-
feedback.Errorf("Error during certificates flashing: %s", err)
86+
feedback.Error(err)
16087
os.Exit(errorcodes.ErrGeneric)
16188
}
16289

cli/common/common.go

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package common
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
"strings"
9+
10+
"github.com/arduino/arduino-cli/arduino/cores/packageindex"
11+
"github.com/arduino/arduino-cli/arduino/serialutils"
12+
"github.com/arduino/arduino-cli/cli/errorcodes"
13+
"github.com/arduino/arduino-cli/cli/feedback"
14+
"github.com/arduino/arduino-fwuploader/indexes"
15+
"github.com/arduino/arduino-fwuploader/indexes/download"
16+
"github.com/arduino/arduino-fwuploader/indexes/firmwareindex"
17+
programmer "github.com/arduino/arduino-fwuploader/programmers"
18+
"github.com/arduino/go-paths-helper"
19+
"github.com/arduino/go-properties-orderedmap"
20+
"github.com/sirupsen/logrus"
21+
)
22+
23+
// InitIndexes does exactly what the name implies
24+
func InitIndexes() (*packageindex.Index, *firmwareindex.Index) {
25+
packageIndex, err := indexes.GetPackageIndex()
26+
if err != nil {
27+
feedback.Errorf("Can't load package index: %s", err)
28+
os.Exit(errorcodes.ErrGeneric)
29+
}
30+
31+
firmwareIndex, err := indexes.GetFirmwareIndex()
32+
if err != nil {
33+
feedback.Errorf("Can't load firmware index: %s", err)
34+
os.Exit(errorcodes.ErrGeneric)
35+
}
36+
return packageIndex, firmwareIndex
37+
}
38+
39+
// CheckFlags runs a basic check, errors if the flags are not defined
40+
func CheckFlags(fqbn, address string) {
41+
if fqbn == "" {
42+
feedback.Errorf("Error during firmware flashing: missing board fqbn")
43+
os.Exit(errorcodes.ErrBadArgument)
44+
}
45+
46+
if address == "" {
47+
feedback.Errorf("Error during firmware flashing: missing board address")
48+
os.Exit(errorcodes.ErrBadArgument)
49+
}
50+
}
51+
52+
// GetBoard is an helper function useful to get the IndexBoard,
53+
// the struct that contains all the infos to make all the operations possible
54+
func GetBoard(firmwareIndex *firmwareindex.Index, fqbn string) *firmwareindex.IndexBoard {
55+
board := firmwareIndex.GetBoard(fqbn)
56+
if board == nil {
57+
feedback.Errorf("Can't find board with %s fqbn", fqbn)
58+
os.Exit(errorcodes.ErrBadArgument)
59+
}
60+
return board
61+
}
62+
63+
// GetUploadToolDir is an helper function that downloads the correct tool to flash a board,
64+
// it returns the path of the downloaded tool
65+
func GetUploadToolDir(packageIndex *packageindex.Index, board *firmwareindex.IndexBoard) *paths.Path {
66+
toolRelease := indexes.GetToolRelease(packageIndex, board.Uploader)
67+
if toolRelease == nil {
68+
feedback.Errorf("Error getting upload tool %s for board %s", board.Uploader, board.Fqbn)
69+
os.Exit(errorcodes.ErrGeneric)
70+
}
71+
uploadToolDir, err := download.DownloadTool(toolRelease)
72+
if err != nil {
73+
feedback.Errorf("Error downloading tool %s: %s", board.Uploader, err)
74+
os.Exit(errorcodes.ErrGeneric)
75+
}
76+
return uploadToolDir
77+
}
78+
79+
// flashSketch is the business logic that handles the flashing procedure,
80+
// it returns using a buffer the out and the err of the programmer
81+
func FlashSketch(board *firmwareindex.IndexBoard, sketch string, uploadToolDir *paths.Path, address string) (programmerOut, programmerErr *bytes.Buffer, err error) {
82+
bootloaderPort, err := GetNewAddress(board, address)
83+
if err != nil {
84+
return nil, nil, err
85+
}
86+
87+
uploaderCommand := board.GetUploaderCommand()
88+
uploaderCommand = strings.ReplaceAll(uploaderCommand, "{tool_dir}", filepath.FromSlash(uploadToolDir.String()))
89+
uploaderCommand = strings.ReplaceAll(uploaderCommand, "{serial.port.file}", bootloaderPort)
90+
uploaderCommand = strings.ReplaceAll(uploaderCommand, "{loader.sketch}", sketch) // we leave that name here because it's only a template,
91+
92+
commandLine, err := properties.SplitQuotedString(uploaderCommand, "\"", false)
93+
if err != nil {
94+
feedback.Errorf(`Error splitting command line "%s": %s`, uploaderCommand, err)
95+
os.Exit(errorcodes.ErrGeneric)
96+
}
97+
98+
// Flash the actual sketch
99+
programmerOut = new(bytes.Buffer)
100+
programmerErr = new(bytes.Buffer)
101+
if feedback.GetFormat() == feedback.JSON {
102+
err = programmer.Flash(commandLine, programmerOut, programmerErr)
103+
} else {
104+
err = programmer.Flash(commandLine, os.Stdout, os.Stderr)
105+
}
106+
if err != nil {
107+
return nil, nil, fmt.Errorf("error during sketch flashing: %s", err)
108+
}
109+
return programmerOut, programmerErr, err
110+
}
111+
112+
// getNewAddress is a function used to reset a board and put it in bootloader mode
113+
// it could happen that the board is assigned to a different serial port, after the reset,
114+
// this fuction handles also this possibility
115+
func GetNewAddress(board *firmwareindex.IndexBoard, oldAddress string) (string, error) {
116+
// Check if board needs a 1200bps touch for upload
117+
bootloaderPort := oldAddress
118+
if board.UploadTouch {
119+
logrus.Info("Putting board into bootloader mode")
120+
newUploadPort, err := serialutils.Reset(oldAddress, board.UploadWait, nil)
121+
if err != nil {
122+
return "", fmt.Errorf("error during sketch flashing: missing board address. %s", err)
123+
}
124+
if newUploadPort != "" {
125+
logrus.Infof("Found port to upload Loader: %s", newUploadPort)
126+
bootloaderPort = newUploadPort
127+
}
128+
}
129+
return bootloaderPort, nil
130+
}

0 commit comments

Comments
 (0)