Skip to content

Commit 399d727

Browse files
committed
Remove some direct access to sketch.Sketch
1 parent 82e6f5d commit 399d727

File tree

9 files changed

+27
-23
lines changed

9 files changed

+27
-23
lines changed

Diff for: internal/cli/arguments/fqbn.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"strings"
2020

2121
"github.com/arduino/arduino-cli/arduino"
22-
"github.com/arduino/arduino-cli/arduino/sketch"
2322
"github.com/arduino/arduino-cli/internal/cli/feedback"
2423
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2524
"github.com/spf13/cobra"
@@ -70,12 +69,10 @@ func (f *Fqbn) Set(fqbn string) {
7069
// - the port is not found, in this case nil is returned
7170
// - the FQBN autodetection fail, in this case the function prints an error and
7271
// terminates the execution
73-
func CalculateFQBNAndPort(portArgs *Port, fqbnArg *Fqbn, instance *rpc.Instance, sk *sketch.Sketch) (string, *rpc.Port) {
74-
// TODO: REMOVE sketch.Sketch from here
75-
72+
func CalculateFQBNAndPort(portArgs *Port, fqbnArg *Fqbn, instance *rpc.Instance, defaultFQBN, defaultAddress, defaultProtocol string) (string, *rpc.Port) {
7673
fqbn := fqbnArg.String()
77-
if fqbn == "" && sk != nil {
78-
fqbn = sk.GetDefaultFQBN()
74+
if fqbn == "" {
75+
fqbn = defaultFQBN
7976
}
8077
if fqbn == "" {
8178
if portArgs == nil || portArgs.address == "" {
@@ -88,7 +85,7 @@ func CalculateFQBNAndPort(portArgs *Port, fqbnArg *Fqbn, instance *rpc.Instance,
8885
return fqbn, port
8986
}
9087

91-
port, err := portArgs.GetPort(instance, sk)
88+
port, err := portArgs.GetPort(instance, defaultAddress, defaultProtocol)
9289
if err != nil {
9390
feedback.Fatal(tr("Error getting port metadata: %v", err), feedback.ErrGeneric)
9491
}

Diff for: internal/cli/arguments/port.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
"github.com/arduino/arduino-cli/arduino"
2323
"github.com/arduino/arduino-cli/arduino/discovery"
24-
"github.com/arduino/arduino-cli/arduino/sketch"
2524
"github.com/arduino/arduino-cli/commands"
2625
"github.com/arduino/arduino-cli/commands/board"
2726
"github.com/arduino/arduino-cli/internal/cli/feedback"
@@ -57,11 +56,12 @@ func (p *Port) AddToCommand(cmd *cobra.Command) {
5756
// This method allows will bypass the discoveries if:
5857
// - a nil instance is passed: in this case the plain port and protocol arguments are returned (even if empty)
5958
// - a protocol is specified: in this case the discoveries are not needed to autodetect the protocol.
60-
func (p *Port) GetPortAddressAndProtocol(instance *rpc.Instance, sk *sketch.Sketch) (string, string, error) {
59+
func (p *Port) GetPortAddressAndProtocol(instance *rpc.Instance, defaultAddress, defaultProtocol string) (string, string, error) {
6160
if p.protocol != "" || instance == nil {
6261
return p.address, p.protocol, nil
6362
}
64-
port, err := p.GetPort(instance, sk)
63+
64+
port, err := p.GetPort(instance, defaultAddress, defaultProtocol)
6565
if err != nil {
6666
return "", "", err
6767
}
@@ -70,15 +70,13 @@ func (p *Port) GetPortAddressAndProtocol(instance *rpc.Instance, sk *sketch.Sket
7070

7171
// GetPort returns the Port obtained by parsing command line arguments.
7272
// The extra metadata for the ports is obtained using the pluggable discoveries.
73-
func (p *Port) GetPort(instance *rpc.Instance, sk *sketch.Sketch) (*discovery.Port, error) {
74-
// TODO: REMOVE sketch.Sketch from here
73+
func (p *Port) GetPort(instance *rpc.Instance, defaultAddress, defaultProtocol string) (*discovery.Port, error) {
7574
// TODO: REMOVE discovery from here (use board.List instead)
7675

7776
address := p.address
7877
protocol := p.protocol
79-
80-
if address == "" && sk != nil {
81-
address, protocol = sk.GetDefaultPortAddressAndProtocol()
78+
if address == "" && (defaultAddress != "" || defaultProtocol != "") {
79+
address, protocol = defaultAddress, defaultProtocol
8280
}
8381
if address == "" {
8482
// If no address is provided we assume the user is trying to upload

Diff for: internal/cli/arguments/sketch.go

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func InitSketchPath(path string) (sketchPath *paths.Path) {
4343
}
4444

4545
// NewSketch is a helper function useful to create a sketch instance
46+
// TODO: Remove this function, we should not access sketch:Sketch directly
4647
func NewSketch(sketchPath *paths.Path) *sketch.Sketch {
4748
sketch, err := sketch.New(sketchPath)
4849
if err != nil {

Diff for: internal/cli/board/attach.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ func runAttachCommand(path string, port *arguments.Port, fqbn string) {
6464
Port: currentPort,
6565
Fqbn: sk.GetDefaultFQBN(),
6666
}
67-
address, protocol, _ := port.GetPortAddressAndProtocol(nil, sk)
67+
68+
defaultAddress, defaultProtocol := sk.GetDefaultPortAddressAndProtocol()
69+
address, protocol, _ := port.GetPortAddressAndProtocol(nil, defaultAddress, defaultProtocol)
6870
if address != "" {
6971
if err := sk.SetDefaultPort(address, protocol); err != nil {
7072
feedback.Fatal(fmt.Sprintf("%s: %s", tr("Error saving sketch metadata"), err), feedback.ErrGeneric)

Diff for: internal/cli/burnbootloader/burnbootloader.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func runBootloaderCommand(command *cobra.Command, args []string) {
6767
logrus.Info("Executing `arduino-cli burn-bootloader`")
6868

6969
// We don't need a Sketch to upload a board's bootloader
70-
discoveryPort, err := port.GetPort(instance, nil)
70+
discoveryPort, err := port.GetPort(instance, "", "")
7171
if err != nil {
7272
feedback.Fatal(tr("Error during Upload: %v", err), feedback.ErrGeneric)
7373
}

Diff for: internal/cli/compile/compile.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,15 @@ func runCompileCommand(cmd *cobra.Command, args []string) {
158158
}
159159

160160
sketchPath := arguments.InitSketchPath(path)
161-
sk := arguments.NewSketch(sketchPath)
162-
163161
inst, profile := instance.CreateAndInitWithProfile(profileArg.Get(), sketchPath)
164162
if fqbnArg.String() == "" {
165163
fqbnArg.Set(profile.GetFqbn())
166164
}
167165

168-
fqbn, port := arguments.CalculateFQBNAndPort(&portArgs, &fqbnArg, inst, sk)
166+
sk := arguments.NewSketch(sketchPath)
167+
defaultFQBN := sk.GetDefaultFQBN()
168+
defaultAddress, defaultProtocol := sk.GetDefaultPortAddressAndProtocol()
169+
fqbn, port := arguments.CalculateFQBNAndPort(&portArgs, &fqbnArg, inst, defaultFQBN, defaultAddress, defaultProtocol)
169170

170171
if keysKeychain != "" || signKey != "" || encryptKey != "" {
171172
arguments.CheckFlagsMandatory(cmd, "keys-keychain", "sign-key", "encrypt-key")

Diff for: internal/cli/debug/debug.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ func runDebugCommand(command *cobra.Command, args []string) {
7676

7777
sketchPath := arguments.InitSketchPath(path)
7878
sk := arguments.NewSketch(sketchPath)
79-
fqbn, port := arguments.CalculateFQBNAndPort(&portArgs, &fqbnArg, instance, sk)
79+
defaultFQBN := sk.GetDefaultFQBN()
80+
defatulAddress, degaultProtocol := sk.GetDefaultPortAddressAndProtocol()
81+
fqbn, port := arguments.CalculateFQBNAndPort(&portArgs, &fqbnArg, instance, defaultFQBN, defatulAddress, degaultProtocol)
8082
debugConfigRequested := &dbg.DebugConfigRequest{
8183
Instance: instance,
8284
Fqbn: fqbn,

Diff for: internal/cli/monitor/monitor.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ func runMonitorCmd(cmd *cobra.Command, args []string) {
7474
quiet = true
7575
}
7676

77-
portAddress, portProtocol, err := portArgs.GetPortAddressAndProtocol(instance, nil)
77+
// TODO: Should use sketch default_port/protocol?
78+
portAddress, portProtocol, err := portArgs.GetPortAddressAndProtocol(instance, "", "")
7879
if err != nil {
7980
feedback.FatalError(err, feedback.ErrGeneric)
8081
}

Diff for: internal/cli/upload/upload.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ func runUploadCommand(command *cobra.Command, args []string) {
100100
fqbnArg.Set(profile.GetFqbn())
101101
}
102102

103-
fqbn, port := arguments.CalculateFQBNAndPort(&portArgs, &fqbnArg, instance, sk)
103+
defaultFQBN := sk.GetDefaultFQBN()
104+
defaultAddress, defaultProtocol := sk.GetDefaultPortAddressAndProtocol()
105+
fqbn, port := arguments.CalculateFQBNAndPort(&portArgs, &fqbnArg, instance, defaultFQBN, defaultAddress, defaultProtocol)
104106

105107
userFieldRes, err := upload.SupportedUserFields(context.Background(), &rpc.SupportedUserFieldsRequest{
106108
Instance: instance,

0 commit comments

Comments
 (0)