Skip to content

Commit be5022e

Browse files
committed
refactor sketch path calculation
1 parent 9771b99 commit be5022e

File tree

6 files changed

+56
-69
lines changed

6 files changed

+56
-69
lines changed

cli/arguments/port.go

+12
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ package arguments
1818
import (
1919
"fmt"
2020
"net/url"
21+
"os"
2122
"time"
2223

2324
"github.com/arduino/arduino-cli/arduino/discovery"
2425
"github.com/arduino/arduino-cli/arduino/sketch"
26+
"github.com/arduino/arduino-cli/cli/errorcodes"
2527
"github.com/arduino/arduino-cli/cli/feedback"
2628
"github.com/arduino/arduino-cli/commands"
2729
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
@@ -144,3 +146,13 @@ func (p *Port) GetPort(instance *rpc.Instance, sk *sketch.Sketch) (*discovery.Po
144146
}
145147
}
146148
}
149+
150+
// GetDiscoveryPort is a helper function useful to get the port and handle possible errors
151+
func (p *Port) GetDiscoveryPort(instance *rpc.Instance, sk *sketch.Sketch) *discovery.Port {
152+
discoveryPort, err := p.GetPort(instance, sk)
153+
if err != nil {
154+
feedback.Errorf(tr("Error discovering port: %v"), err)
155+
os.Exit(errorcodes.ErrGeneric)
156+
}
157+
return discoveryPort
158+
}

cli/arguments/sketch.go

+32-7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package arguments
1818
import (
1919
"os"
2020

21+
"github.com/arduino/arduino-cli/arduino/sketch"
2122
"github.com/arduino/arduino-cli/cli/errorcodes"
2223
"github.com/arduino/arduino-cli/cli/feedback"
2324
"github.com/arduino/go-paths-helper"
@@ -26,16 +27,40 @@ import (
2627

2728
// InitSketchPath returns an instance of paths.Path pointing to sketchPath.
2829
// 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)
30+
// In both cases it warns the user if he's using deprecated files
31+
func InitSketchPath(path string) (sketchPath *paths.Path) {
32+
if path != "" {
33+
sketchPath = paths.New(path)
34+
} else {
35+
wd, err := paths.Getwd()
36+
if err != nil {
37+
feedback.Errorf(tr("Couldn't get current working directory: %v"), err)
38+
os.Exit(errorcodes.ErrGeneric)
39+
}
40+
logrus.Infof("Reading sketch from dir: %s", wd)
41+
sketchPath = wd
3242
}
43+
WarnDeprecatedFiles(sketchPath)
44+
return sketchPath
45+
}
3346

34-
wd, err := paths.Getwd()
47+
// NewSketch is a helper function useful to create a sketch instance
48+
func NewSketch(sketchPath *paths.Path) *sketch.Sketch {
49+
sketch, err := sketch.New(sketchPath)
3550
if err != nil {
36-
feedback.Errorf(tr("Couldn't get current working directory: %v"), err)
51+
feedback.Errorf(tr("Error creating sketch: %v"), err)
3752
os.Exit(errorcodes.ErrGeneric)
3853
}
39-
logrus.Infof("Reading sketch from dir: %s", wd)
40-
return wd
54+
return sketch
55+
}
56+
57+
// WarnDeprecatedFiles warns the user that a type of sketch files are deprecated
58+
func WarnDeprecatedFiles(sketchPath *paths.Path) {
59+
// .pde files are still supported but deprecated, this warning urges the user to rename them
60+
if files := sketch.CheckForPdeFiles(sketchPath); len(files) > 0 {
61+
feedback.Error(tr("Sketches with .pde extension are deprecated, please rename the following files to .ino:"))
62+
for _, f := range files {
63+
feedback.Error(f)
64+
}
65+
}
4166
}

cli/compile/compile.go

+3-22
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import (
2121
"encoding/json"
2222
"os"
2323

24-
"github.com/arduino/arduino-cli/arduino/discovery"
25-
"github.com/arduino/arduino-cli/arduino/sketch"
2624
"github.com/arduino/arduino-cli/cli/arguments"
2725
"github.com/arduino/arduino-cli/cli/feedback"
2826
"github.com/arduino/arduino-cli/cli/output"
@@ -129,15 +127,8 @@ func run(cmd *cobra.Command, args []string) {
129127
if len(args) > 0 {
130128
path = args[0]
131129
}
132-
sketchPath := arguments.InitSketchPath(path)
133130

134-
// .pde files are still supported but deprecated, this warning urges the user to rename them
135-
if files := sketch.CheckForPdeFiles(sketchPath); len(files) > 0 {
136-
feedback.Error(tr("Sketches with .pde extension are deprecated, please rename the following files to .ino:"))
137-
for _, f := range files {
138-
feedback.Error(f)
139-
}
140-
}
131+
sketchPath := arguments.InitSketchPath(path)
141132

142133
var overrides map[string]string
143134
if sourceOverrides != "" {
@@ -189,18 +180,8 @@ func run(cmd *cobra.Command, args []string) {
189180
}
190181

191182
if compileError == nil && uploadAfterCompile {
192-
var sk *sketch.Sketch
193-
sk, err := sketch.New(sketchPath)
194-
if err != nil {
195-
feedback.Errorf(tr("Error during Upload: %v"), err)
196-
os.Exit(errorcodes.ErrGeneric)
197-
}
198-
var discoveryPort *discovery.Port
199-
discoveryPort, err = port.GetPort(inst, sk)
200-
if err != nil {
201-
feedback.Errorf(tr("Error during Upload: %v"), err)
202-
os.Exit(errorcodes.ErrGeneric)
203-
}
183+
sk := arguments.NewSketch(sketchPath)
184+
discoveryPort := port.GetDiscoveryPort(inst, sk)
204185

205186
userFieldRes, err := upload.SupportedUserFields(context.Background(), &rpc.SupportedUserFieldsRequest{
206187
Instance: inst,

cli/debug/debug.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"os/signal"
2222
"sort"
2323

24-
"github.com/arduino/arduino-cli/arduino/sketch"
2524
"github.com/arduino/arduino-cli/cli/arguments"
2625
"github.com/arduino/arduino-cli/cli/errorcodes"
2726
"github.com/arduino/arduino-cli/cli/feedback"
@@ -81,17 +80,11 @@ func run(command *cobra.Command, args []string) {
8180
if len(args) > 0 {
8281
path = args[0]
8382
}
83+
8484
sketchPath := arguments.InitSketchPath(path)
85-
sk, err := sketch.New(sketchPath)
86-
if err != nil {
87-
feedback.Errorf(tr("Error during Debug: %v"), err)
88-
os.Exit(errorcodes.ErrGeneric)
89-
}
90-
discoveryPort, err := port.GetPort(instance, sk)
91-
if err != nil {
92-
feedback.Errorf(tr("Error during Debug: %v"), err)
93-
os.Exit(errorcodes.ErrGeneric)
94-
}
85+
sk := arguments.NewSketch(sketchPath)
86+
discoveryPort := port.GetDiscoveryPort(instance, sk)
87+
9588
debugConfigRequested := &dbg.DebugConfigRequest{
9689
Instance: instance,
9790
Fqbn: fqbn,

cli/sketch/archive.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"fmt"
2121
"os"
2222

23-
"github.com/arduino/arduino-cli/arduino/sketch"
23+
"github.com/arduino/arduino-cli/cli/arguments"
2424
"github.com/arduino/arduino-cli/cli/errorcodes"
2525
"github.com/arduino/arduino-cli/cli/feedback"
2626
sk "github.com/arduino/arduino-cli/commands/sketch"
@@ -61,13 +61,7 @@ func runArchiveCommand(cmd *cobra.Command, args []string) {
6161
sketchPath = paths.New(args[0])
6262
}
6363

64-
// .pde files are still supported but deprecated, this warning urges the user to rename them
65-
if files := sketch.CheckForPdeFiles(sketchPath); len(files) > 0 {
66-
feedback.Error(tr("Sketches with .pde extension are deprecated, please rename the following files to .ino:"))
67-
for _, f := range files {
68-
feedback.Error(f)
69-
}
70-
}
64+
arguments.WarnDeprecatedFiles(sketchPath)
7165

7266
archivePath := ""
7367
if len(args) == 2 {

cli/upload/upload.go

+3-21
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"context"
2020
"os"
2121

22-
"github.com/arduino/arduino-cli/arduino/sketch"
2322
"github.com/arduino/arduino-cli/cli/arguments"
2423
"github.com/arduino/arduino-cli/cli/errorcodes"
2524
"github.com/arduino/arduino-cli/cli/feedback"
@@ -86,27 +85,10 @@ func run(command *cobra.Command, args []string) {
8685
if len(args) > 0 {
8786
path = args[0]
8887
}
89-
sketchPath := arguments.InitSketchPath(path)
90-
91-
// .pde files are still supported but deprecated, this warning urges the user to rename them
92-
if files := sketch.CheckForPdeFiles(sketchPath); len(files) > 0 && importDir == "" && importFile == "" {
93-
feedback.Error(tr("Sketches with .pde extension are deprecated, please rename the following files to .ino:"))
94-
for _, f := range files {
95-
feedback.Error(f)
96-
}
97-
}
98-
99-
sk, err := sketch.New(sketchPath)
100-
if err != nil && importDir == "" && importFile == "" {
101-
feedback.Errorf(tr("Error during Upload: %v"), err)
102-
os.Exit(errorcodes.ErrGeneric)
103-
}
10488

105-
discoveryPort, err := port.GetPort(instance, sk)
106-
if err != nil {
107-
feedback.Errorf(tr("Error during Upload: %v"), err)
108-
os.Exit(errorcodes.ErrGeneric)
109-
}
89+
sketchPath := arguments.InitSketchPath(path)
90+
sk := arguments.NewSketch(sketchPath)
91+
discoveryPort := port.GetDiscoveryPort(instance, sk)
11092

11193
if fqbn == "" && sk != nil && sk.Metadata != nil {
11294
// If the user didn't specify an FQBN and a sketch.json file is present

0 commit comments

Comments
 (0)