Skip to content

Commit e3afc0c

Browse files
committed
Read upload port from sketch.json
Read upload port from sketch.json
1 parent d80f119 commit e3afc0c

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

arduino/sketches/sketches.go

+18
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,24 @@ 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"`
42+
}
43+
44+
// NewSketchBook returns a new SketchBook object
45+
func NewSketchBook(path *paths.Path) *SketchBook {
46+
return &SketchBook{
47+
Path: path,
48+
}
49+
}
50+
51+
// NewSketch loads a sketch from the sketchbook
52+
func (sketchbook *SketchBook) NewSketch(name string) (*Sketch, error) {
53+
sketch := &Sketch{
54+
FullPath: sketchbook.Path.Join(name),
55+
Name: name,
56+
}
57+
sketch.ImportMetadata()
58+
return sketch, nil
4159
}
4260

4361
// NewSketchFromPath loads a sketch from the specified path

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)