Skip to content

Commit 27a9106

Browse files
authored
Read upload port from sketch.json (arduino#454)
* Read upload port from sketch.json * Fix 'attach' help message
1 parent d80f119 commit 27a9106

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

arduino/sketches/sketches.go

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type Metadata struct {
3838
type BoardMetadata struct {
3939
Fqbn string `json:"fqbn,required"`
4040
Name string `json:"name,omitempty"`
41+
Port string `json:"port,omitepty"`
4142
}
4243

4344
// NewSketchFromPath loads a sketch from the specified path

cli/board/attach.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ func initAttachCommand() *cobra.Command {
3535
Use: "attach <port>|<FQBN> [sketchPath]",
3636
Short: "Attaches a sketch to a board.",
3737
Long: "Attaches a sketch to a board.",
38-
Example: " " + os.Args[0] + " board attach serial:///dev/tty/ACM0\n" +
39-
" " + os.Args[0] + " board attach serial:///dev/tty/ACM0 HelloWorld\n" +
38+
Example: " " + os.Args[0] + " board attach serial:///dev/ttyACM0\n" +
39+
" " + os.Args[0] + " board attach serial:///dev/ttyACM0 HelloWorld\n" +
4040
" " + os.Args[0] + " board attach arduino:samd:mkr1000",
4141
Args: cobra.RangeArgs(1, 2),
4242
Run: runAttachCommand,
4343
}
4444
attachCommand.Flags().StringVar(&attachFlags.searchTimeout, "timeout", "5s",
45-
"The timeout of the search of connected devices, try to high it if your board is not found (e.g. to 10s).")
45+
"The connected devices search timeout, raise it if your board doesn't show up (e.g. to 10s).")
4646
return attachCommand
4747
}
4848

commands/board/attach.go

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ func Attach(ctx context.Context, req *rpc.BoardAttachReq, taskCB commands.TaskPr
9696
sketch.Metadata.CPU = sketches.BoardMetadata{
9797
Fqbn: board.FQBN(),
9898
Name: board.Name(),
99+
Port: deviceURI.String(),
99100
}
100101
}
101102

commands/upload/upload.go

+10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"context"
2020
"fmt"
2121
"io"
22+
"net/url"
2223
"os"
2324
"path/filepath"
2425
"strings"
@@ -53,6 +54,15 @@ func Upload(ctx context.Context, req *rpc.UploadReq, outStream io.Writer, errStr
5354

5455
// FIXME: make a specification on how a port is specified via command line
5556
port := req.GetPort()
57+
if port == "" && sketch != nil && sketch.Metadata != nil {
58+
deviceURI, err := url.Parse(sketch.Metadata.CPU.Port)
59+
if err != nil {
60+
return nil, fmt.Errorf("invalid Device URL format: %s", err)
61+
}
62+
if deviceURI.Scheme == "serial" {
63+
port = deviceURI.Host + deviceURI.Path
64+
}
65+
}
5666
if port == "" {
5767
return nil, fmt.Errorf("no upload port provided")
5868
}

0 commit comments

Comments
 (0)