Skip to content

Commit cd7c399

Browse files
committed
add package feedback, since the arduino-cli one has been internalized
1 parent 88a0f5a commit cd7c399

File tree

9 files changed

+262
-128
lines changed

9 files changed

+262
-128
lines changed

cli/certificates/flash.go

+28-32
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ import (
2525
"strings"
2626
"time"
2727

28-
"github.com/arduino/arduino-cli/cli/errorcodes"
29-
"github.com/arduino/arduino-cli/cli/feedback"
3028
"github.com/arduino/arduino-fwuploader/cli/arguments"
3129
"github.com/arduino/arduino-fwuploader/cli/common"
30+
"github.com/arduino/arduino-fwuploader/cli/feedback"
3231
"github.com/arduino/arduino-fwuploader/flasher"
3332
"github.com/arduino/arduino-fwuploader/indexes/download"
3433
"github.com/arduino/go-paths-helper"
@@ -69,23 +68,20 @@ func runFlash(cmd *cobra.Command, args []string) {
6968
uploadToolDir := common.GetUploadToolDir(packageIndex, board)
7069

7170
if len(certificateURLs) == 0 && len(certificatePaths) == 0 {
72-
feedback.Errorf("Error during certificates flashing: no certificates provided")
73-
os.Exit(errorcodes.ErrBadArgument)
71+
feedback.Fatal("Error during certificates flashing: no certificates provided", feedback.ErrBadArgument)
7472
}
7573

7674
loaderSketchPath, err := download.DownloadSketch(board.LoaderSketch)
7775
if err != nil {
78-
feedback.Errorf("Error downloading loader sketch from %s: %s", board.LoaderSketch.URL, err)
79-
os.Exit(errorcodes.ErrGeneric)
76+
feedback.Fatal(fmt.Sprintf("Error downloading loader sketch from %s: %s", board.LoaderSketch.URL, err), feedback.ErrGeneric)
8077
}
8178
logrus.Debugf("loader sketch downloaded in %s", loaderSketchPath.String())
8279

8380
loaderSketch := strings.ReplaceAll(loaderSketchPath.String(), loaderSketchPath.Ext(), "")
8481

8582
programmerOut, programmerErr, err := common.FlashSketch(board, loaderSketch, uploadToolDir, commonFlags.Address)
8683
if err != nil {
87-
feedback.Error(err)
88-
os.Exit(errorcodes.ErrGeneric)
84+
feedback.FatalError(err, feedback.ErrGeneric)
8985
}
9086

9187
// Wait a bit after flashing the loader sketch for the board to become
@@ -110,38 +106,38 @@ func runFlash(cmd *cobra.Command, args []string) {
110106
err = fmt.Errorf("unknown module: %s", moduleName)
111107
}
112108
if err != nil {
113-
feedback.Errorf("Error during certificates flashing: %s", err)
114-
os.Exit(errorcodes.ErrGeneric)
109+
110+
feedback.Fatal(fmt.Sprintf("Error during certificates flashing: %s", err), feedback.ErrGeneric)
115111
}
116112
defer f.Close()
117113

118114
// now flash the certificate
119-
flasherOut := new(bytes.Buffer)
120-
flasherErr := new(bytes.Buffer)
121115
certFileList := paths.NewPathList(certificatePaths...)
122116
if feedback.GetFormat() == feedback.JSON {
117+
flasherOut := new(bytes.Buffer)
118+
flasherErr := new(bytes.Buffer)
123119
err = f.FlashCertificates(&certFileList, certificateURLs, flasherOut)
120+
if err != nil {
121+
flasherErr.Write([]byte(fmt.Sprintf("Error during certificates flashing: %s", err)))
122+
}
123+
// Print the results
124+
feedback.PrintResult(&flasher.FlashResult{
125+
Programmer: (&flasher.ExecOutput{
126+
Stdout: programmerOut.String(),
127+
Stderr: programmerErr.String(),
128+
}),
129+
Flasher: (&flasher.ExecOutput{
130+
Stdout: flasherOut.String(),
131+
Stderr: flasherErr.String(),
132+
}),
133+
})
134+
if err != nil {
135+
os.Exit(int(feedback.ErrGeneric))
136+
}
124137
} else {
125138
err = f.FlashCertificates(&certFileList, certificateURLs, os.Stdout)
126-
}
127-
if err != nil {
128-
feedback.Errorf("Error during certificates flashing: %s", err)
129-
flasherErr.Write([]byte(fmt.Sprintf("Error during certificates flashing: %s", err)))
130-
}
131-
132-
// Print the results
133-
feedback.PrintResult(&flasher.FlashResult{
134-
Programmer: (&flasher.ExecOutput{
135-
Stdout: programmerOut.String(),
136-
Stderr: programmerErr.String(),
137-
}),
138-
Flasher: (&flasher.ExecOutput{
139-
Stdout: flasherOut.String(),
140-
Stderr: flasherErr.String(),
141-
}),
142-
})
143-
// Exit if something went wrong but after printing
144-
if err != nil {
145-
os.Exit(errorcodes.ErrGeneric)
139+
if err != nil {
140+
feedback.Fatal(fmt.Sprintf("Error during certificates flashing: %s", err), feedback.ErrGeneric)
141+
}
146142
}
147143
}

cli/cli.go

+11-38
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@ import (
2222
"fmt"
2323
"io/ioutil"
2424
"os"
25-
"strings"
2625

2726
"github.com/arduino/arduino-fwuploader/cli/certificates"
2827
"github.com/arduino/arduino-fwuploader/cli/firmware"
2928
"github.com/arduino/arduino-fwuploader/cli/version"
3029

31-
"github.com/arduino/arduino-cli/cli/errorcodes"
32-
"github.com/arduino/arduino-cli/cli/feedback"
30+
"github.com/arduino/arduino-fwuploader/cli/feedback"
3331
v "github.com/arduino/arduino-fwuploader/version"
3432
"github.com/mattn/go-colorable"
3533
"github.com/rifflock/lfshook"
@@ -85,16 +83,16 @@ func toLogLevel(s string) (t logrus.Level, found bool) {
8583
return
8684
}
8785

88-
func parseFormatString(arg string) (feedback.OutputFormat, bool) {
89-
f, found := map[string]feedback.OutputFormat{
90-
"json": feedback.JSON,
91-
"text": feedback.Text,
92-
}[arg]
86+
func preRun(cmd *cobra.Command, args []string) {
9387

94-
return f, found
95-
}
88+
// Prepare the Feedback system
89+
// check the right output format was passed
90+
format, found := feedback.ParseOutputFormat(outputFormat)
91+
if !found {
92+
feedback.Fatal(fmt.Sprintf("Invalid output format: %s", outputFormat), feedback.ErrBadArgument)
93+
}
94+
feedback.SetFormat(format)
9695

97-
func preRun(cmd *cobra.Command, args []string) {
9896
// Prepare logging
9997
if verbose {
10098
// if we print on stdout, do it in full colors
@@ -107,7 +105,6 @@ func preRun(cmd *cobra.Command, args []string) {
107105
}
108106

109107
// Normalize the format strings
110-
logFormat = strings.ToLower(logFormat)
111108
if logFormat == "json" {
112109
logrus.SetFormatter(&logrus.JSONFormatter{})
113110
}
@@ -116,8 +113,7 @@ func preRun(cmd *cobra.Command, args []string) {
116113
if logFile != "" {
117114
file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
118115
if err != nil {
119-
fmt.Printf("Unable to open file for logging: %s", logFile)
120-
os.Exit(errorcodes.ErrBadCall)
116+
feedback.Fatal(fmt.Sprintf("Unable to open file for logging: %s", logFile), feedback.ErrBadArgument)
121117
}
122118

123119
// Use a hook so we don't get color codes in the log file
@@ -130,33 +126,10 @@ func preRun(cmd *cobra.Command, args []string) {
130126

131127
// Configure logging filter
132128
if lvl, found := toLogLevel(logLevel); !found {
133-
feedback.Errorf("Invalid option for --log-level: %s", logLevel)
134-
os.Exit(errorcodes.ErrBadArgument)
129+
feedback.Fatal(fmt.Sprintf("Invalid option for --log-level: %s", logLevel), feedback.ErrBadArgument)
135130
} else {
136131
logrus.SetLevel(lvl)
137132
}
138133

139-
// Prepare the Feedback system
140-
141-
// normalize the format strings
142-
outputFormat = strings.ToLower(outputFormat)
143-
// check the right output format was passed
144-
format, found := parseFormatString(outputFormat)
145-
if !found {
146-
feedback.Errorf("Invalid output format: %s", outputFormat)
147-
os.Exit(errorcodes.ErrBadCall)
148-
}
149-
150-
// use the output format to configure the Feedback
151-
feedback.SetFormat(format)
152-
153134
logrus.Info(v.VersionInfo)
154-
155-
if outputFormat != "text" {
156-
cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
157-
logrus.Warn("Calling help on JSON format")
158-
feedback.Error("Invalid Call : should show Help, but it is available only in TEXT mode.")
159-
os.Exit(errorcodes.ErrBadCall)
160-
})
161-
}
162135
}

cli/common/common.go

+9-18
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ import (
2626

2727
"github.com/arduino/arduino-cli/arduino/cores/packageindex"
2828
"github.com/arduino/arduino-cli/arduino/serialutils"
29-
"github.com/arduino/arduino-cli/cli/errorcodes"
30-
"github.com/arduino/arduino-cli/cli/feedback"
29+
"github.com/arduino/arduino-fwuploader/cli/feedback"
3130
"github.com/arduino/arduino-fwuploader/indexes"
3231
"github.com/arduino/arduino-fwuploader/indexes/download"
3332
"github.com/arduino/arduino-fwuploader/indexes/firmwareindex"
@@ -41,28 +40,24 @@ import (
4140
func InitIndexes() (*packageindex.Index, *firmwareindex.Index) {
4241
packageIndex, err := indexes.GetPackageIndex()
4342
if err != nil {
44-
feedback.Errorf("Can't load package index: %s", err)
45-
os.Exit(errorcodes.ErrGeneric)
43+
feedback.Fatal(fmt.Sprintf("Can't load package index: %s", err), feedback.ErrGeneric)
4644
}
4745

4846
firmwareIndex, err := indexes.GetFirmwareIndex()
4947
if err != nil {
50-
feedback.Errorf("Can't load firmware index: %s", err)
51-
os.Exit(errorcodes.ErrGeneric)
48+
feedback.Fatal(fmt.Sprintf("Can't load firmware index: %s", err), feedback.ErrGeneric)
5249
}
5350
return packageIndex, firmwareIndex
5451
}
5552

5653
// CheckFlags runs a basic check, errors if the flags are not defined
5754
func CheckFlags(fqbn, address string) {
5855
if fqbn == "" {
59-
feedback.Errorf("Error during firmware flashing: missing board fqbn")
60-
os.Exit(errorcodes.ErrBadArgument)
56+
feedback.Fatal("Error during firmware flashing: missing board fqbn", feedback.ErrBadArgument)
6157
}
6258

6359
if address == "" {
64-
feedback.Errorf("Error during firmware flashing: missing board address")
65-
os.Exit(errorcodes.ErrBadArgument)
60+
feedback.Fatal("Error during firmware flashing: missing board address", feedback.ErrBadArgument)
6661
}
6762
logrus.Debugf("fqbn: %s, address: %s", fqbn, address)
6863
}
@@ -72,8 +67,7 @@ func CheckFlags(fqbn, address string) {
7267
func GetBoard(firmwareIndex *firmwareindex.Index, fqbn string) *firmwareindex.IndexBoard {
7368
board := firmwareIndex.GetBoard(fqbn)
7469
if board == nil {
75-
feedback.Errorf("Can't find board with %s fqbn", fqbn)
76-
os.Exit(errorcodes.ErrBadArgument)
70+
feedback.Fatal(fmt.Sprintf("Can't find board with %s fqbn", fqbn), feedback.ErrBadArgument)
7771
}
7872
logrus.Debugf("got board: %s", board.Fqbn)
7973
return board
@@ -84,13 +78,11 @@ func GetBoard(firmwareIndex *firmwareindex.Index, fqbn string) *firmwareindex.In
8478
func GetUploadToolDir(packageIndex *packageindex.Index, board *firmwareindex.IndexBoard) *paths.Path {
8579
toolRelease := indexes.GetToolRelease(packageIndex, board.Uploader)
8680
if toolRelease == nil {
87-
feedback.Errorf("Error getting upload tool %s for board %s", board.Uploader, board.Fqbn)
88-
os.Exit(errorcodes.ErrGeneric)
81+
feedback.Fatal(fmt.Sprintf("Error getting upload tool %s for board %s", board.Uploader, board.Fqbn), feedback.ErrGeneric)
8982
}
9083
uploadToolDir, err := download.DownloadTool(toolRelease)
9184
if err != nil {
92-
feedback.Errorf("Error downloading tool %s: %s", board.Uploader, err)
93-
os.Exit(errorcodes.ErrGeneric)
85+
feedback.Fatal(fmt.Sprintf("Error downloading tool %s: %s", board.Uploader, err), feedback.ErrGeneric)
9486
}
9587
logrus.Debugf("upload tool downloaded in %s", uploadToolDir.String())
9688
return uploadToolDir
@@ -112,8 +104,7 @@ func FlashSketch(board *firmwareindex.IndexBoard, sketch string, uploadToolDir *
112104
logrus.Debugf("uploading with command: %s", uploaderCommand)
113105
commandLine, err := properties.SplitQuotedString(uploaderCommand, "\"", false)
114106
if err != nil {
115-
feedback.Errorf(`Error splitting command line "%s": %s`, uploaderCommand, err)
116-
os.Exit(errorcodes.ErrGeneric)
107+
feedback.Fatal(fmt.Sprintf(`Error splitting command line "%s": %s`, uploaderCommand, err), feedback.ErrGeneric)
117108
}
118109

119110
// Flash the actual sketch

0 commit comments

Comments
 (0)