Skip to content

Commit 55bbbe4

Browse files
committed
Re-Implementation of 'board attach' command
1 parent 6832cf2 commit 55bbbe4

File tree

4 files changed

+93
-12
lines changed

4 files changed

+93
-12
lines changed

Diff for: arduino/sketch/sketch.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ type Metadata struct {
4949

5050
// BoardMetadata represents the board metadata for the sketch
5151
type BoardMetadata struct {
52-
Fqbn string `json:"fqbn"`
53-
Name string `json:"name,omitempty"`
54-
Port string `json:"port,omitempty"`
52+
Fqbn string `json:"fqbn"`
53+
Name string `json:"name,omitempty"`
54+
Port string `json:"port,omitempty"`
55+
Protocol string `json:"protocol,omitempty"`
5556
}
5657

5758
var tr = i18n.Tr

Diff for: cli/arguments/port.go

+13-9
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/arduino/arduino-cli/commands"
3030
"github.com/arduino/arduino-cli/commands/board"
3131
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
32-
"github.com/pkg/errors"
3332
"github.com/sirupsen/logrus"
3433
"github.com/spf13/cobra"
3534
)
@@ -57,11 +56,12 @@ func (p *Port) AddToCommand(cmd *cobra.Command) {
5756
}
5857

5958
// GetPortAddressAndProtocol returns only the port address and the port protocol
60-
// without any other port metadata obtained from the discoveries. This method allows
61-
// to bypass the discoveries unless the protocol is not specified: in this
62-
// case the discoveries are needed to autodetect the protocol.
59+
// without any other port metadata obtained from the discoveries.
60+
// This method allows will bypass the discoveries if:
61+
// - a nil instance is passed: in this case the plain port and protocol arguments are returned (even if empty)
62+
// - a protocol is specified: in this case the discoveries are not needed to autodetect the protocol.
6363
func (p *Port) GetPortAddressAndProtocol(instance *rpc.Instance, sk *sketch.Sketch) (string, string, error) {
64-
if p.protocol != "" {
64+
if p.protocol != "" || instance == nil {
6565
return p.address, p.protocol, nil
6666
}
6767
port, err := p.GetPort(instance, sk)
@@ -81,13 +81,17 @@ func (p *Port) GetPort(instance *rpc.Instance, sk *sketch.Sketch) (*discovery.Po
8181
protocol := p.protocol
8282

8383
if address == "" && sk != nil && sk.Metadata != nil {
84+
// This is for compatibility with old sketch.json where the address
85+
// for serial ports was stored as URL like serial:///dev/ttyACM0, so we
86+
// check if we have a URI and the scheme is "serial". In all other
87+
// cases we pick the address as is.
8488
deviceURI, err := url.Parse(sk.Metadata.CPU.Port)
85-
if err != nil {
86-
return nil, errors.Errorf("invalid Device URL format: %s", err)
87-
}
88-
if deviceURI.Scheme == "serial" {
89+
if err == nil && deviceURI.Scheme == "serial" {
8990
address = deviceURI.Host + deviceURI.Path
91+
} else {
92+
address = sk.Metadata.CPU.Port
9093
}
94+
protocol = sk.Metadata.CPU.Protocol
9195
}
9296
if address == "" {
9397
// If no address is provided we assume the user is trying to upload

Diff for: cli/board/attach.go

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 [email protected].
15+
16+
package board
17+
18+
import (
19+
"fmt"
20+
"os"
21+
22+
"github.com/arduino/arduino-cli/cli/arguments"
23+
"github.com/arduino/arduino-cli/cli/errorcodes"
24+
"github.com/arduino/arduino-cli/cli/feedback"
25+
"github.com/spf13/cobra"
26+
)
27+
28+
func initAttachCommand() *cobra.Command {
29+
var port arguments.Port
30+
attachCommand := &cobra.Command{
31+
Use: fmt.Sprintf("attach [-p <%s>] [-b <%s>] [%s]", tr("port"), tr("FQBN"), tr("sketchPath")),
32+
Short: tr("Attaches a sketch to a board."),
33+
Long: tr("Attaches a sketch to a board."),
34+
Example: " " + os.Args[0] + " board attach -p /dev/ttyACM0\n" +
35+
" " + os.Args[0] + " board attach -p /dev/ttyACM0 HelloWorld\n" +
36+
" " + os.Args[0] + " board attach -b arduino:samd:mkr1000",
37+
Args: cobra.MaximumNArgs(1),
38+
Run: func(cmd *cobra.Command, args []string) {
39+
sketchPath := ""
40+
if len(args) > 0 {
41+
sketchPath = args[0]
42+
}
43+
runAttachCommand(sketchPath, &port, fqbn.String())
44+
},
45+
}
46+
fqbn.AddToCommand(attachCommand)
47+
port.AddToCommand(attachCommand)
48+
49+
return attachCommand
50+
}
51+
52+
func runAttachCommand(path string, port *arguments.Port, fqbn string) {
53+
sketchPath := arguments.InitSketchPath(path)
54+
sk := arguments.NewSketch(sketchPath)
55+
56+
address, protocol, _ := port.GetPortAddressAndProtocol(nil, sk)
57+
if address != "" {
58+
sk.Metadata.CPU.Port = address
59+
sk.Metadata.CPU.Protocol = protocol
60+
msg := fmt.Sprintf("%s: %s", tr("Set port to"), address)
61+
if protocol != "" {
62+
msg += " / " + protocol
63+
}
64+
feedback.Print(msg)
65+
}
66+
if fqbn != "" {
67+
sk.Metadata.CPU.Fqbn = fqbn
68+
msg := fmt.Sprintf("%s: %s", tr("Set FQBN to"), fqbn)
69+
feedback.Print(msg)
70+
}
71+
if err := sk.ExportMetadata(); err != nil {
72+
feedback.Errorf("%s: %s", tr("Error saving sketch metadata"), err)
73+
os.Exit(errorcodes.ErrGeneric)
74+
}
75+
}

Diff for: cli/board/board.go

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func NewCommand() *cobra.Command {
3434
" " + os.Args[0] + " board list",
3535
}
3636

37+
boardCommand.AddCommand(initAttachCommand())
3738
boardCommand.AddCommand(initDetailsCommand())
3839
boardCommand.AddCommand(initListCommand())
3940
boardCommand.AddCommand(initListAllCommand())

0 commit comments

Comments
 (0)