Skip to content

Commit 6c9b1fd

Browse files
sanialescmaglie
authored andcommitted
added error codes
1 parent 09b45fa commit 6c9b1fd

File tree

7 files changed

+99
-83
lines changed

7 files changed

+99
-83
lines changed

cmd/arduino.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ var ArduinoCmd = &cobra.Command{
9191
Long: "Arduino Create Command Line Interface (arduino-cli)",
9292
BashCompletionFunction: bashAutoCompletionFunction,
9393
PersistentPreRun: arduinoPreRun,
94-
RunE: arduinoRun,
94+
Run: arduinoRun,
9595
Example: `arduino --generate-docs to generate the docs and autocompletion for the whole CLI.`,
9696
}
9797

@@ -246,6 +246,7 @@ func arduinoPreRun(cmd *cobra.Command, args []string) {
246246
if !formatter.IsCurrentFormat("text") {
247247
cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
248248
formatter.PrintErrorMessage("Invalid Call : should show Help, but it is available only in TEXT mode")
249+
os.Exit(errBadCall)
249250
})
250251
}
251252

@@ -254,7 +255,7 @@ func arduinoPreRun(cmd *cobra.Command, args []string) {
254255
}
255256
}
256257

257-
func arduinoRun(cmd *cobra.Command, args []string) error {
258+
func arduinoRun(cmd *cobra.Command, args []string) {
258259
if rootCmdFlags.GenerateDocs {
259260
errorText := ""
260261
err := cmd.GenBashCompletionFile("docs/bash_completions/arduino")
@@ -268,9 +269,10 @@ func arduinoRun(cmd *cobra.Command, args []string) error {
268269
if errorText != "" {
269270
formatter.PrintErrorMessage(errorText)
270271
}
271-
return nil
272+
} else {
273+
cmd.Help()
274+
os.Exit(errBadCall)
272275
}
273-
return cmd.Help()
274276
}
275277

276278
// Execute adds all child commands to the root command sets flags appropriately.
@@ -353,8 +355,9 @@ func initViper() {
353355

354356
err := viper.ReadInConfig()
355357
if err != nil {
356-
formatter.PrintError(err)
358+
//formatter.PrintError(err)
357359
formatter.PrintErrorMessage("Cannot read configuration file in any of the default folders")
360+
os.Exit(errNoConfigFile)
358361
}
359362

360363
viper.SetDefault("paths.sketchbook", defHome)
@@ -384,7 +387,6 @@ func initViper() {
384387
if username != "" { // put username and pass somewhere
385388

386389
}
387-
388390
}
389391
}
390392

@@ -417,7 +419,7 @@ func executeLoginCommand(cmd *cobra.Command, args []string) {
417419
netRCHome, err := homedir.Dir()
418420
if err != nil {
419421
formatter.PrintError(err)
420-
return
422+
os.Exit(errGeneric)
421423
}
422424

423425
netRCFile := filepath.Join(netRCHome, ".netrc")
@@ -430,7 +432,7 @@ func executeLoginCommand(cmd *cobra.Command, args []string) {
430432
NetRC, err := netrc.Parse(file)
431433
if err != nil {
432434
formatter.PrintError(err)
433-
return
435+
os.Exit(errGeneric)
434436
}
435437

436438
usr := arduinoLoginFlags.User
@@ -440,21 +442,21 @@ func executeLoginCommand(cmd *cobra.Command, args []string) {
440442
token, err := authConf.Token(usr, pwd)
441443
if err != nil {
442444
formatter.PrintError(err)
443-
return
445+
os.Exit(errNetwork)
444446
}
445447

446448
NetRC.RemoveMachine("arduino.cc")
447449
NetRC.NewMachine("arduino.cc", usr, token.Access, token.Refresh)
448450
content, err := NetRC.MarshalText()
449451
if err != nil {
450452
formatter.PrintError(err)
451-
return
453+
os.Exit(errGeneric)
452454
}
453455

454456
err = ioutil.WriteFile(netRCFile, content, 0666)
455457
if err != nil {
456458
formatter.PrintError(err)
457-
return
459+
os.Exit(errGeneric)
458460
}
459461

460462
formatter.PrintResult(`Successfully logged into the system

cmd/arduino_boards.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package cmd
22

33
import (
4-
"errors"
54
"fmt"
65
"net/url"
6+
"os"
77
"time"
88

99
"github.com/bcmi-labs/arduino-cli/common"
@@ -38,7 +38,7 @@ var arduinoBoardAttachCmd = &cobra.Command{
3838
Long: `Attaches a board to a sketch`,
3939
Example: `arduino board attach --board serial:///dev/tty/ACM0 \
4040
--sketch mySketch # Attaches a sketch to a board`,
41-
RunE: executeBoardAttachCommand,
41+
Run: executeBoardAttachCommand,
4242
}
4343

4444
// executeBoardListCommand detects and lists the connected arduino boards
@@ -77,17 +77,20 @@ func executeBoardListCommand(cmd *cobra.Command, args []string) {
7777
// it closes ungracefully, but at the end of the command we can't have races.
7878
}
7979

80-
func executeBoardAttachCommand(cmd *cobra.Command, args []string) error {
80+
func executeBoardAttachCommand(cmd *cobra.Command, args []string) {
8181
if len(args) > 0 {
82-
return errors.New("Not accepting additional arguments")
82+
formatter.PrintErrorMessage("Not accepting additional arguments")
83+
os.Exit(errBadCall)
8384
}
8485

8586
if arduinoBoardAttachFlags.SketchName == "" {
86-
return errors.New("No sketch name provided")
87+
formatter.PrintErrorMessage("No sketch name provided")
88+
os.Exit(errBadCall)
8789
}
8890

8991
if arduinoBoardAttachFlags.BoardURI == "" {
90-
return errors.New("No board URI provided")
92+
formatter.PrintErrorMessage("No board URI provided")
93+
os.Exit(errBadCall)
9194
}
9295

9396
duration, err := time.ParseDuration(arduinoBoardListFlags.SearchTimeout)
@@ -103,33 +106,33 @@ func executeBoardAttachCommand(cmd *cobra.Command, args []string) error {
103106
homeFolder, err := common.GetDefaultArduinoHomeFolder()
104107
if err != nil {
105108
formatter.PrintErrorMessage("Cannot Parse Board Index file")
106-
return nil
109+
os.Exit(errCoreConfig)
107110
}
108111

109112
packageFolder, err := common.GetDefaultPkgFolder()
110113
if err != nil {
111114
formatter.PrintErrorMessage("Cannot Parse Board Index file")
112-
return nil
115+
os.Exit(errCoreConfig)
113116
}
114117

115118
bs, err := boards.Find(packageFolder)
116119
if err != nil {
117120
formatter.PrintErrorMessage("Cannot Parse Board Index file")
118-
return nil
121+
os.Exit(errCoreConfig)
119122
}
120123

121124
ss := sketches.Find(homeFolder)
122125

123126
sketch, exists := ss[arduinoBoardAttachFlags.SketchName]
124127
if !exists {
125128
formatter.PrintErrorMessage("Cannot find specified sketch in the Sketchbook")
126-
return nil
129+
os.Exit(errGeneric)
127130
}
128131

129132
deviceURI, err := url.Parse(arduinoBoardAttachFlags.BoardURI)
130133
if err != nil {
131134
formatter.PrintErrorMessage("The provided Device URL is not in a valid format")
132-
return nil
135+
os.Exit(errBadCall)
133136
}
134137

135138
var findBoardFunc func(boards.Boards, *discovery.Monitor, *url.URL) *boards.Board
@@ -143,7 +146,7 @@ func executeBoardAttachCommand(cmd *cobra.Command, args []string) error {
143146
Type = "network"
144147
} else {
145148
formatter.PrintErrorMessage("Invalid device port type provided. Accepted types are: serial://, tty://, http://, https://, tcp://, udp://")
146-
return nil
149+
os.Exit(errBadCall)
147150
}
148151

149152
board := findBoardFunc(bs, monitor, deviceURI)
@@ -158,7 +161,6 @@ func executeBoardAttachCommand(cmd *cobra.Command, args []string) error {
158161
formatter.PrintError(err)
159162
}
160163
formatter.PrintResult("BOARD ATTACHED")
161-
return nil
162164
}
163165

164166
// findSerialConnectedBoard find the board which is connected to the specified URI via serial port, using a monitor and a set of Boards
@@ -182,7 +184,7 @@ func findSerialConnectedBoard(bs boards.Boards, monitor *discovery.Monitor, devi
182184
board := bs.ByVidPid(serialDevice.VendorID, serialDevice.ProductID)
183185
if board == nil {
184186
formatter.PrintErrorMessage("No Supported board has been found, try either install new cores or check your board URI")
185-
return nil
187+
os.Exit(errGeneric)
186188
}
187189

188190
formatter.Print("SUPPORTED BOARD FOUND:")
@@ -207,7 +209,7 @@ func findNetworkConnectedBoard(bs boards.Boards, monitor *discovery.Monitor, dev
207209
}
208210
if !found {
209211
formatter.PrintErrorMessage("No Supported board has been found at the specified board URI, try either install new cores or check your board URI")
210-
return nil
212+
os.Exit(errGeneric)
211213
}
212214

213215
formatter.Print("SUPPORTED BOARD FOUND:")

cmd/arduino_core.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
package cmd
3131

3232
import (
33-
"fmt"
3433
"os"
3534
"path/filepath"
3635

@@ -78,7 +77,7 @@ var arduinoCoreDownloadCmd = &cobra.Command{
7877
Use: "download [PACKAGER:ARCH[=VERSION]](S)",
7978
Short: "Downloads one or more cores and relative tool dependencies",
8079
Long: `Downloads one or more cores and relative tool dependencies`,
81-
RunE: executeCoreDownloadCommand,
80+
Run: executeCoreDownloadCommand,
8281
Example: `
8382
arduino core download arduino:samd #to download latest version of arduino SAMD core.
8483
arduino core download arduino:samd=1.6.9 #for the specific version (in this case 1.6.9)`,
@@ -88,7 +87,7 @@ var arduinoCoreInstallCmd = &cobra.Command{
8887
Use: "install [PACKAGER:ARCH[=VERSION]](S)",
8988
Short: "Installs one or more cores and relative tool dependencies",
9089
Long: `Installs one or more cores and relative tool dependencies`,
91-
RunE: executeCoreInstallCommand,
90+
Run: executeCoreInstallCommand,
9291
Example: `
9392
arduino core install arduino:samd #to download latest version of arduino SAMD core.
9493
arduino core installteele arduino:samd=1.6.9 #for the specific version (in this case 1.6.9)`,
@@ -103,27 +102,28 @@ func executeCoreCommand(cmd *cobra.Command, args []string) {
103102
common.ExecUpdateIndex(prettyPrints.DownloadCoreFileIndex(), GlobalFlags.Verbose)
104103
} else {
105104
cmd.Help()
105+
os.Exit(errBadCall)
106106
}
107107
}
108108

109109
func executeCoreListCommand(cmd *cobra.Command, args []string) {
110110
pkgHome, err := common.GetDefaultPkgFolder()
111111
if err != nil {
112112
formatter.PrintError(err)
113-
return
113+
os.Exit(errCoreConfig)
114114
}
115115

116116
dir, err := os.Open(pkgHome)
117117
if err != nil {
118118
formatter.PrintErrorMessage("Cannot open packages folder")
119-
return
119+
os.Exit(errCoreConfig)
120120
}
121121
defer dir.Close()
122122

123123
dirFiles, err := dir.Readdir(0)
124124
if err != nil {
125125
formatter.PrintErrorMessage("Cannot read into packages folder")
126-
return
126+
os.Exit(errCoreConfig)
127127
}
128128

129129
pkgs := output.InstalledPackageList{
@@ -148,14 +148,15 @@ func executeCoreListCommand(cmd *cobra.Command, args []string) {
148148
formatter.Print(pkgs)
149149
}
150150

151-
func executeCoreDownloadCommand(cmd *cobra.Command, args []string) error {
151+
func executeCoreDownloadCommand(cmd *cobra.Command, args []string) {
152152
if len(args) < 1 {
153-
return fmt.Errorf("No core specified for download command")
153+
formatter.PrintErrorMessage("No core specified for download command")
154+
os.Exit(errBadCall)
154155
}
155156

156157
status, err := getPackagesStatusContext(GlobalFlags.Verbose)
157158
if err != nil {
158-
return nil
159+
os.Exit(errGeneric)
159160
}
160161

161162
IDTuples := cores.ParseArgs(args)
@@ -178,17 +179,18 @@ func executeCoreDownloadCommand(cmd *cobra.Command, args []string) error {
178179
releases.ParallelDownload(downloads, true, "Downloaded", GlobalFlags.Verbose, &outputResults.Cores, "core")
179180

180181
formatter.Print(outputResults)
181-
return nil
182182
}
183183

184-
func executeCoreInstallCommand(cmd *cobra.Command, args []string) error {
184+
func executeCoreInstallCommand(cmd *cobra.Command, args []string) {
185185
if len(args) < 1 {
186-
return fmt.Errorf("No core specified for download command")
186+
formatter.PrintErrorMessage("No core specified for download command")
187+
os.Exit(errBadCall)
187188
}
188189

189190
status, err := getPackagesStatusContext(GlobalFlags.Verbose)
190191
if err != nil {
191-
return nil
192+
formatter.PrintError(err)
193+
os.Exit(errCoreConfig)
192194
}
193195

194196
IDTuples := cores.ParseArgs(args)
@@ -221,7 +223,7 @@ func executeCoreInstallCommand(cmd *cobra.Command, args []string) error {
221223
toolRoot, err := common.GetDefaultToolsFolder(item.Package)
222224
if err != nil {
223225
formatter.PrintErrorMessage("Cannot get tool install path, try again.")
224-
return nil
226+
os.Exit(errCoreConfig)
225227
}
226228
outputResults.Tools[i].Path = filepath.Join(toolRoot, item.Name, item.Release.VersionName())
227229
}
@@ -239,14 +241,13 @@ func executeCoreInstallCommand(cmd *cobra.Command, args []string) error {
239241
coreRoot, err := common.GetDefaultCoresFolder(item.Package)
240242
if err != nil {
241243
formatter.PrintErrorMessage("Cannot get core install path, try again.")
242-
return nil
244+
os.Exit(errCoreConfig)
243245
}
244246
outputResults.Cores[i+failOutputsCount].Path = filepath.Join(coreRoot, item.Name, item.Release.VersionName())
245247
}
246248
}
247249

248250
formatter.Print(outputResults)
249-
return nil
250251
}
251252

252253
// getInstalledCores gets the installed cores and puts them in the output struct.

0 commit comments

Comments
 (0)