Skip to content

Commit 4417fdd

Browse files
committed
[breaking] Refactored cli package to reflect commands changes
1 parent cfe62ec commit 4417fdd

29 files changed

+120
-212
lines changed

cli/board/attach.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,7 @@ var attachFlags struct {
5151
}
5252

5353
func runAttachCommand(cmd *cobra.Command, args []string) {
54-
instance, err := instance.CreateInstance()
55-
if err != nil {
56-
feedback.Errorf("Attach board error: %v", err)
57-
os.Exit(errorcodes.ErrGeneric)
58-
}
54+
instance := instance.CreateAndInit()
5955

6056
var path *paths.Path
6157
if len(args) > 1 {
@@ -64,7 +60,7 @@ func runAttachCommand(cmd *cobra.Command, args []string) {
6460
path = initSketchPath(path)
6561
}
6662

67-
if _, err = board.Attach(context.Background(), &rpc.BoardAttachRequest{
63+
if _, err := board.Attach(context.Background(), &rpc.BoardAttachRequest{
6864
Instance: instance,
6965
BoardUri: args[0],
7066
SketchPath: path.String(),

cli/board/details.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,7 @@ func initDetailsCommand() *cobra.Command {
5555
}
5656

5757
func runDetailsCommand(cmd *cobra.Command, args []string) {
58-
inst, err := instance.CreateInstance()
59-
if err != nil {
60-
feedback.Errorf(tr("Error getting board details: %v"), err)
61-
os.Exit(errorcodes.ErrGeneric)
62-
}
58+
inst := instance.CreateAndInit()
6359

6460
// remove once `board details <fqbn>` is removed
6561
if fqbn == "" && len(args) > 0 {

cli/board/list.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,7 @@ var listFlags struct {
5757
// runListCommand detects and lists the connected arduino boards
5858
func runListCommand(cmd *cobra.Command, args []string) {
5959
if listFlags.watch {
60-
inst, err := instance.CreateInstance()
61-
if err != nil {
62-
feedback.Errorf("Error detecting boards: %v", err)
63-
os.Exit(errorcodes.ErrGeneric)
64-
}
60+
inst := instance.CreateAndInit()
6561
watchList(cmd, inst)
6662
os.Exit(0)
6763
}
@@ -73,12 +69,7 @@ func runListCommand(cmd *cobra.Command, args []string) {
7369
time.Sleep(timeout)
7470
}
7571

76-
inst, err := instance.CreateInstance()
77-
if err != nil {
78-
feedback.Errorf("Error detecting boards: %v", err)
79-
os.Exit(errorcodes.ErrGeneric)
80-
}
81-
72+
inst := instance.CreateAndInit()
8273
ports, err := board.List(inst.GetId())
8374
if err != nil {
8475
feedback.Errorf("Error detecting boards: %v", err)

cli/board/listall.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,7 @@ var showHiddenBoard bool
5050

5151
// runListAllCommand list all installed boards
5252
func runListAllCommand(cmd *cobra.Command, args []string) {
53-
inst, err := instance.CreateInstance()
54-
if err != nil {
55-
feedback.Errorf("Error listing boards: %v", err)
56-
os.Exit(errorcodes.ErrGeneric)
57-
}
53+
inst := instance.CreateAndInit()
5854

5955
list, err := board.ListAll(context.Background(), &rpc.BoardListAllRequest{
6056
Instance: inst,

cli/board/search.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,7 @@ var searchFlags struct {
5252
}
5353

5454
func runSearchCommand(cmd *cobra.Command, args []string) {
55-
inst, err := instance.CreateInstance()
56-
if err != nil {
57-
feedback.Errorf("Error searching boards: %v", err)
58-
os.Exit(errorcodes.ErrGeneric)
59-
}
55+
inst := instance.CreateAndInit()
6056

6157
res, err := board.Search(context.Background(), &rpc.BoardSearchRequest{
6258
Instance: inst,

cli/burnbootloader/burnbootloader.go

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,15 @@ import (
2424
"github.com/arduino/arduino-cli/cli/instance"
2525
"github.com/arduino/arduino-cli/commands/upload"
2626
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
27-
"github.com/arduino/go-paths-helper"
28-
"github.com/sirupsen/logrus"
2927
"github.com/spf13/cobra"
3028
)
3129

3230
var (
33-
fqbn string
34-
port string
35-
verbose bool
36-
verify bool
37-
importDir string
38-
programmer string
39-
burnBootloader bool
31+
fqbn string
32+
port string
33+
verbose bool
34+
verify bool
35+
programmer string
4036
)
4137

4238
// NewCommand created a new `burn-bootloader` command
@@ -60,11 +56,7 @@ func NewCommand() *cobra.Command {
6056
}
6157

6258
func run(command *cobra.Command, args []string) {
63-
instance, err := instance.CreateInstance()
64-
if err != nil {
65-
feedback.Errorf("Error during Upload: %v", err)
66-
os.Exit(errorcodes.ErrGeneric)
67-
}
59+
instance := instance.CreateAndInit()
6860

6961
if _, err := upload.BurnBootloader(context.Background(), &rpc.BurnBootloaderRequest{
7062
Instance: instance,
@@ -79,18 +71,3 @@ func run(command *cobra.Command, args []string) {
7971
}
8072
os.Exit(0)
8173
}
82-
83-
// initSketchPath returns the current working directory
84-
func initSketchPath(sketchPath *paths.Path) *paths.Path {
85-
if sketchPath != nil {
86-
return sketchPath
87-
}
88-
89-
wd, err := paths.Getwd()
90-
if err != nil {
91-
feedback.Errorf("Couldn't get current working directory: %v", err)
92-
os.Exit(errorcodes.ErrGeneric)
93-
}
94-
logrus.Infof("Reading sketch from dir: %s", wd)
95-
return wd
96-
}

cli/compile/compile.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,7 @@ func NewCommand() *cobra.Command {
120120
}
121121

122122
func run(cmd *cobra.Command, args []string) {
123-
inst, err := instance.CreateInstance()
124-
if err != nil {
125-
feedback.Errorf("Error creating instance: %v", err)
126-
os.Exit(errorcodes.ErrGeneric)
127-
}
123+
inst := instance.CreateAndInit()
128124

129125
var path *paths.Path
130126
if len(args) > 0 {
@@ -183,6 +179,7 @@ func run(cmd *cobra.Command, args []string) {
183179
compileErr := new(bytes.Buffer)
184180
verboseCompile := configuration.Settings.GetString("logging.level") == "debug"
185181
var compileRes *rpc.CompileResponse
182+
var err error
186183
if output.OutputFormat == "json" {
187184
compileRes, err = compile.Compile(context.Background(), compileRequest, compileOut, compileErr, verboseCompile)
188185
} else {

cli/core/download.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,7 @@ func initDownloadCommand() *cobra.Command {
4545
}
4646

4747
func runDownloadCommand(cmd *cobra.Command, args []string) {
48-
inst, err := instance.CreateInstance()
49-
if err != nil {
50-
feedback.Errorf("Error downloading: %v", err)
51-
os.Exit(errorcodes.ErrGeneric)
52-
}
48+
inst := instance.CreateAndInit()
5349

5450
logrus.Info("Executing `arduino core download`")
5551

cli/core/install.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,7 @@ func DetectSkipPostInstallValue() bool {
8383
}
8484

8585
func runInstallCommand(cmd *cobra.Command, args []string) {
86-
inst, err := instance.CreateInstance()
87-
if err != nil {
88-
feedback.Errorf("Error installing: %v", err)
89-
os.Exit(errorcodes.ErrGeneric)
90-
}
91-
86+
inst := instance.CreateAndInit()
9287
logrus.Info("Executing `arduino core install`")
9388

9489
platformsRefs, err := globals.ParseReferenceArgs(args, true)

cli/core/list.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,7 @@ var listFlags struct {
4949
}
5050

5151
func runListCommand(cmd *cobra.Command, args []string) {
52-
inst, err := instance.CreateInstance()
53-
if err != nil {
54-
feedback.Errorf("Error listing platforms: %v", err)
55-
os.Exit(errorcodes.ErrGeneric)
56-
}
57-
52+
inst := instance.CreateAndInit()
5853
logrus.Info("Executing `arduino core list`")
5954

6055
platforms, err := core.GetPlatforms(&rpc.PlatformListRequest{

cli/core/search.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,10 @@ func initSearchCommand() *cobra.Command {
6161
const indexUpdateInterval = "24h"
6262

6363
func runSearchCommand(cmd *cobra.Command, args []string) {
64-
inst, err := instance.CreateInstance()
65-
if err != nil {
66-
feedback.Errorf("Error searching for platforms: %v", err)
67-
os.Exit(errorcodes.ErrGeneric)
68-
}
64+
inst := instance.CreateAndInit()
6965

7066
if indexesNeedUpdating(indexUpdateInterval) {
71-
_, err = commands.UpdateIndex(context.Background(), &rpc.UpdateIndexRequest{
67+
_, err := commands.UpdateIndex(context.Background(), &rpc.UpdateIndexRequest{
7268
Instance: inst,
7369
}, output.ProgressBar())
7470
if err != nil {

cli/core/uninstall.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,7 @@ func initUninstallCommand() *cobra.Command {
4242
}
4343

4444
func runUninstallCommand(cmd *cobra.Command, args []string) {
45-
inst, err := instance.CreateInstance()
46-
if err != nil {
47-
feedback.Errorf("Error uninstalling: %v", err)
48-
os.Exit(errorcodes.ErrGeneric)
49-
}
50-
45+
inst := instance.CreateAndInit()
5146
logrus.Info("Executing `arduino core uninstall`")
5247

5348
platformsRefs, err := globals.ParseReferenceArgs(args, true)

cli/core/update_index.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func initUpdateIndexCommand() *cobra.Command {
4242
}
4343

4444
func runUpdateIndexCommand(cmd *cobra.Command, args []string) {
45-
instance := instance.CreateInstanceIgnorePlatformIndexErrors()
45+
instance := instance.CreateAndInit()
4646
logrus.Info("Executing `arduino core update-index`")
4747

4848
_, err := commands.UpdateIndex(context.Background(), &rpc.UpdateIndexRequest{

cli/core/upgrade.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,7 @@ func initUpgradeCommand() *cobra.Command {
4747
}
4848

4949
func runUpgradeCommand(cmd *cobra.Command, args []string) {
50-
inst, err := instance.CreateInstance()
51-
if err != nil {
52-
feedback.Errorf("Error upgrading: %v", err)
53-
os.Exit(errorcodes.ErrGeneric)
54-
}
55-
50+
inst := instance.CreateAndInit()
5651
logrus.Info("Executing `arduino core upgrade`")
5752

5853
// if no platform was passed, upgrade allthethings

cli/debug/debug.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,7 @@ func NewCommand() *cobra.Command {
6969
}
7070

7171
func run(command *cobra.Command, args []string) {
72-
instance, err := instance.CreateInstance()
73-
if err != nil {
74-
feedback.Errorf("Error during Debug: %v", err)
75-
os.Exit(errorcodes.ErrGeneric)
76-
}
72+
instance := instance.CreateAndInit()
7773

7874
var path *paths.Path
7975
if len(args) > 0 {

0 commit comments

Comments
 (0)