Skip to content

Commit 967b15a

Browse files
committed
Remove duplicated code to initialize Sketch path
1 parent b9b489d commit 967b15a

File tree

5 files changed

+58
-82
lines changed

5 files changed

+58
-82
lines changed

cli/arguments/sketch.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to license@arduino.cc.
15+
16+
package arguments
17+
18+
import (
19+
"os"
20+
21+
"github.com/arduino/arduino-cli/cli/errorcodes"
22+
"github.com/arduino/arduino-cli/cli/feedback"
23+
"github.com/arduino/go-paths-helper"
24+
"github.com/sirupsen/logrus"
25+
)
26+
27+
// InitSketchPath returns an instance of paths.Path pointing to sketchPath.
28+
// If sketchPath is an empty string returns the current working directory.
29+
func InitSketchPath(sketchPath string) *paths.Path {
30+
if sketchPath != "" {
31+
return paths.New(sketchPath)
32+
}
33+
34+
wd, err := paths.Getwd()
35+
if err != nil {
36+
feedback.Errorf("Couldn't get current working directory: %v", err)
37+
os.Exit(errorcodes.ErrGeneric)
38+
}
39+
logrus.Infof("Reading sketch from dir: %s", wd)
40+
return wd
41+
}

cli/board/attach.go

+5-22
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@ import (
1919
"context"
2020
"os"
2121

22+
"github.com/arduino/arduino-cli/cli/arguments"
2223
"github.com/arduino/arduino-cli/cli/errorcodes"
2324
"github.com/arduino/arduino-cli/cli/feedback"
2425
"github.com/arduino/arduino-cli/cli/instance"
2526
"github.com/arduino/arduino-cli/cli/output"
2627
"github.com/arduino/arduino-cli/commands/board"
2728
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
28-
"github.com/arduino/go-paths-helper"
29-
"github.com/sirupsen/logrus"
3029
"github.com/spf13/cobra"
3130
)
3231

@@ -53,35 +52,19 @@ var attachFlags struct {
5352
func runAttachCommand(cmd *cobra.Command, args []string) {
5453
instance := instance.CreateAndInit()
5554

56-
var path *paths.Path
55+
path := ""
5756
if len(args) > 1 {
58-
path = paths.New(args[1])
59-
} else {
60-
path = initSketchPath(path)
57+
path = args[1]
6158
}
59+
sketchPath := arguments.InitSketchPath(path)
6260

6361
if _, err := board.Attach(context.Background(), &rpc.BoardAttachRequest{
6462
Instance: instance,
6563
BoardUri: args[0],
66-
SketchPath: path.String(),
64+
SketchPath: sketchPath.String(),
6765
SearchTimeout: attachFlags.searchTimeout,
6866
}, output.TaskProgress()); err != nil {
6967
feedback.Errorf("Attach board error: %v", err)
7068
os.Exit(errorcodes.ErrGeneric)
7169
}
7270
}
73-
74-
// initSketchPath returns the current working directory
75-
func initSketchPath(sketchPath *paths.Path) *paths.Path {
76-
if sketchPath != nil {
77-
return sketchPath
78-
}
79-
80-
wd, err := paths.Getwd()
81-
if err != nil {
82-
feedback.Errorf("Couldn't get current working directory: %v", err)
83-
os.Exit(errorcodes.ErrGeneric)
84-
}
85-
logrus.Infof("Reading sketch from dir: %s", wd)
86-
return wd
87-
}

cli/compile/compile.go

+4-20
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os"
2323

2424
"github.com/arduino/arduino-cli/arduino/sketch"
25+
"github.com/arduino/arduino-cli/cli/arguments"
2526
"github.com/arduino/arduino-cli/cli/feedback"
2627
"github.com/arduino/arduino-cli/cli/output"
2728
"github.com/arduino/arduino-cli/configuration"
@@ -32,7 +33,6 @@ import (
3233
"github.com/arduino/arduino-cli/commands/upload"
3334
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3435
"github.com/arduino/go-paths-helper"
35-
"github.com/sirupsen/logrus"
3636
"github.com/spf13/cobra"
3737
)
3838

@@ -122,12 +122,11 @@ func NewCommand() *cobra.Command {
122122
func run(cmd *cobra.Command, args []string) {
123123
inst := instance.CreateAndInit()
124124

125-
var path *paths.Path
125+
path := ""
126126
if len(args) > 0 {
127-
path = paths.New(args[0])
127+
path = args[0]
128128
}
129-
130-
sketchPath := initSketchPath(path)
129+
sketchPath := arguments.InitSketchPath(path)
131130

132131
// .pde files are still supported but deprecated, this warning urges the user to rename them
133132
if files := sketch.CheckForPdeFiles(sketchPath); len(files) > 0 {
@@ -224,21 +223,6 @@ func run(cmd *cobra.Command, args []string) {
224223
}
225224
}
226225

227-
// initSketchPath returns the current working directory
228-
func initSketchPath(sketchPath *paths.Path) *paths.Path {
229-
if sketchPath != nil {
230-
return sketchPath
231-
}
232-
233-
wd, err := paths.Getwd()
234-
if err != nil {
235-
feedback.Errorf("Couldn't get current working directory: %v", err)
236-
os.Exit(errorcodes.ErrGeneric)
237-
}
238-
logrus.Infof("Reading sketch from dir: %s", wd)
239-
return wd
240-
}
241-
242226
type compileResult struct {
243227
CompileOut string `json:"compiler_out"`
244228
CompileErr string `json:"compiler_err"`

cli/debug/debug.go

+4-20
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,15 @@ import (
2222
"os/signal"
2323
"sort"
2424

25+
"github.com/arduino/arduino-cli/cli/arguments"
2526
"github.com/arduino/arduino-cli/cli/errorcodes"
2627
"github.com/arduino/arduino-cli/cli/feedback"
2728
"github.com/arduino/arduino-cli/cli/instance"
2829
"github.com/arduino/arduino-cli/commands/debug"
2930
dbg "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/debug/v1"
3031
"github.com/arduino/arduino-cli/table"
31-
"github.com/arduino/go-paths-helper"
3232
"github.com/arduino/go-properties-orderedmap"
3333
"github.com/fatih/color"
34-
"github.com/sirupsen/logrus"
3534
"github.com/spf13/cobra"
3635
"google.golang.org/grpc/status"
3736
)
@@ -71,11 +70,11 @@ func NewCommand() *cobra.Command {
7170
func run(command *cobra.Command, args []string) {
7271
instance := instance.CreateAndInit()
7372

74-
var path *paths.Path
73+
path := ""
7574
if len(args) > 0 {
76-
path = paths.New(args[0])
75+
path = args[0]
7776
}
78-
sketchPath := initSketchPath(path)
77+
sketchPath := arguments.InitSketchPath(path)
7978

8079
debugConfigRequested := &dbg.DebugConfigRequest{
8180
Instance: instance,
@@ -114,21 +113,6 @@ func run(command *cobra.Command, args []string) {
114113
}
115114
}
116115

117-
// initSketchPath returns the current working directory
118-
func initSketchPath(sketchPath *paths.Path) *paths.Path {
119-
if sketchPath != nil {
120-
return sketchPath
121-
}
122-
123-
wd, err := paths.Getwd()
124-
if err != nil {
125-
feedback.Errorf("Couldn't get current working directory: %v", err)
126-
os.Exit(errorcodes.ErrGeneric)
127-
}
128-
logrus.Infof("Reading sketch from dir: %s", wd)
129-
return wd
130-
}
131-
132116
type debugInfoResult struct {
133117
info *dbg.GetDebugConfigResponse
134118
}

cli/upload/upload.go

+4-20
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ import (
2020
"os"
2121

2222
"github.com/arduino/arduino-cli/arduino/sketch"
23+
"github.com/arduino/arduino-cli/cli/arguments"
2324
"github.com/arduino/arduino-cli/cli/errorcodes"
2425
"github.com/arduino/arduino-cli/cli/feedback"
2526
"github.com/arduino/arduino-cli/cli/instance"
2627
"github.com/arduino/arduino-cli/commands/upload"
2728
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
28-
"github.com/arduino/go-paths-helper"
29-
"github.com/sirupsen/logrus"
3029
"github.com/spf13/cobra"
3130
)
3231

@@ -75,11 +74,11 @@ func checkFlagsConflicts(command *cobra.Command, args []string) {
7574
func run(command *cobra.Command, args []string) {
7675
instance := instance.CreateAndInit()
7776

78-
var path *paths.Path
77+
path := ""
7978
if len(args) > 0 {
80-
path = paths.New(args[0])
79+
path = args[0]
8180
}
82-
sketchPath := initSketchPath(path)
81+
sketchPath := arguments.InitSketchPath(path)
8382

8483
// .pde files are still supported but deprecated, this warning urges the user to rename them
8584
if files := sketch.CheckForPdeFiles(sketchPath); len(files) > 0 {
@@ -105,18 +104,3 @@ func run(command *cobra.Command, args []string) {
105104
os.Exit(errorcodes.ErrGeneric)
106105
}
107106
}
108-
109-
// initSketchPath returns the current working directory
110-
func initSketchPath(sketchPath *paths.Path) *paths.Path {
111-
if sketchPath != nil {
112-
return sketchPath
113-
}
114-
115-
wd, err := paths.Getwd()
116-
if err != nil {
117-
feedback.Errorf("Couldn't get current working directory: %v", err)
118-
os.Exit(errorcodes.ErrGeneric)
119-
}
120-
logrus.Infof("Reading sketch from dir: %s", wd)
121-
return wd
122-
}

0 commit comments

Comments
 (0)