Skip to content

Commit 175cb77

Browse files
cleanup get-version command
1 parent b8b94c7 commit 175cb77

File tree

1 file changed

+5
-78
lines changed

1 file changed

+5
-78
lines changed

cli/firmware/getversion.go

+5-78
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,14 @@ package firmware
2121
import (
2222
"fmt"
2323
"io"
24-
"log"
2524
"os"
26-
"strings"
27-
"time"
2825

2926
"github.com/arduino/arduino-fwuploader/cli/common"
3027
"github.com/arduino/arduino-fwuploader/cli/feedback"
3128
"github.com/arduino/arduino-fwuploader/cli/globals"
3229
"github.com/arduino/arduino-fwuploader/flasher"
33-
"github.com/arduino/arduino-fwuploader/indexes/download"
34-
"github.com/arduino/arduino-fwuploader/indexes/firmwareindex"
3530
"github.com/arduino/arduino-fwuploader/plugin"
36-
"github.com/arduino/go-paths-helper"
37-
"github.com/sirupsen/logrus"
3831
"github.com/spf13/cobra"
39-
semver "go.bug.st/relaxed-semver"
4032
)
4133

4234
// NewGetVersionCommand creates a new `get-version` command
@@ -64,26 +56,20 @@ func runGetVersion(cmd *cobra.Command, args []string) {
6456
board := common.GetBoard(firmwareIndex, commonFlags.Fqbn)
6557
uploadToolDir := common.DownloadRequiredToolsForBoard(packageIndex, board)
6658

67-
var result *flasher.FlashResult
68-
if !board.IsPlugin() {
69-
result = getVersion(board, uploadToolDir)
70-
} else {
71-
uploader, err := plugin.NewFWUploaderPlugin(uploadToolDir)
72-
if err != nil {
73-
feedback.Fatal(fmt.Sprintf("Could not open uploader plugin: %s", err), feedback.ErrGeneric)
74-
}
75-
result = getVersionWithPlugin(uploader)
59+
uploader, err := plugin.NewFWUploaderPlugin(uploadToolDir)
60+
if err != nil {
61+
feedback.Fatal(fmt.Sprintf("Could not open uploader plugin: %s", err), feedback.ErrGeneric)
7662
}
7763

64+
result := getVersion(uploader)
7865
if feedback.GetFormat() == feedback.Text {
7966
fmt.Printf("Firmware version installed: %s", result.Version)
8067
} else {
81-
// Print the results
8268
feedback.PrintResult(result)
8369
}
8470
}
8571

86-
func getVersionWithPlugin(uploader *plugin.FwUploader) *flasher.FlashResult {
72+
func getVersion(uploader *plugin.FwUploader) *flasher.FlashResult {
8773
var stdout, stderr io.Writer
8874
if feedback.GetFormat() == feedback.Text {
8975
stdout = os.Stdout
@@ -102,62 +88,3 @@ func getVersionWithPlugin(uploader *plugin.FwUploader) *flasher.FlashResult {
10288
Version: res.FirmwareVersion.String(),
10389
}
10490
}
105-
106-
func getVersion(board *firmwareindex.IndexBoard, uploadToolDir *paths.Path) *flasher.FlashResult {
107-
versionSketchPath, err := download.DownloadSketch(board.VersionSketch)
108-
if err != nil {
109-
feedback.Fatal(fmt.Sprintf("Error downloading loader sketch from %s: %s", board.LoaderSketch.URL, err), feedback.ErrGeneric)
110-
}
111-
logrus.Debugf("version sketch downloaded in %s", versionSketchPath.String())
112-
113-
versionSketch := strings.ReplaceAll(versionSketchPath.String(), versionSketchPath.Ext(), "")
114-
115-
programmerOut, programmerErr, err := common.FlashSketch(board, versionSketch, uploadToolDir, commonFlags.Address)
116-
if err != nil {
117-
feedback.FatalError(err, feedback.ErrGeneric)
118-
}
119-
120-
// Wait a bit after flashing the sketch for the board to become available again.
121-
logrus.Debug("sleeping for 3 sec")
122-
time.Sleep(3 * time.Second)
123-
124-
// 9600 is the baudrate used in the CheckVersion sketch
125-
port, err := flasher.OpenSerial(commonFlags.Address, 9600, 2)
126-
if err != nil {
127-
feedback.FatalError(err, feedback.ErrGeneric)
128-
}
129-
130-
buff := make([]byte, 200)
131-
serialResult := make([]byte, 0)
132-
for {
133-
n, err := port.Read(buff)
134-
if err != nil {
135-
log.Fatal(err)
136-
break
137-
}
138-
serialResult = append(serialResult, buff[:n]...)
139-
if n == 0 { // exit when done reading from serial
140-
break
141-
}
142-
logrus.Info(string(buff[:n]))
143-
}
144-
lines := strings.Split(string(serialResult), "\n")
145-
for _, line := range lines {
146-
if strings.HasPrefix(line, "Firmware version installed: ") {
147-
version := strings.TrimSpace(strings.Replace(line, "Firmware version installed: ", "", 1))
148-
semver := semver.ParseRelaxed(version)
149-
return &flasher.FlashResult{
150-
Programmer: (&flasher.ExecOutput{
151-
Stdout: programmerOut.String(),
152-
Stderr: programmerErr.String(),
153-
}),
154-
Version: semver.String(),
155-
}
156-
}
157-
if strings.HasPrefix(line, "Communication with WiFi module failed!") {
158-
feedback.Fatal("communication with WiFi module failed", feedback.ErrGeneric)
159-
}
160-
}
161-
feedback.Fatal("could not find the version string to parse", feedback.ErrGeneric)
162-
return nil
163-
}

0 commit comments

Comments
 (0)