Skip to content

Commit c6915fb

Browse files
committed
add Attach command stream support
1 parent 269e4d7 commit c6915fb

File tree

8 files changed

+221
-167
lines changed

8 files changed

+221
-167
lines changed

cli/board/attach.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ func runAttachCommand(cmd *cobra.Command, args []string) {
5959
}
6060
_, err := board.BoardAttach(context.Background(), &rpc.BoardAttachReq{
6161
Instance: instance,
62-
BoardURI: args[0],
62+
BoardUri: args[0],
6363
SketchPath: path,
6464
BoardFlavour: attachFlags.boardFlavour,
6565
SearchTimeout: attachFlags.searchTimeout,
66-
})
66+
}, cli.OutputTaskProgress())
6767
if err != nil {
6868
formatter.PrintError(err, "attach board error")
6969
os.Exit(cli.ErrGeneric)

commands/board/attach.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@ import (
2929
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
3030
"github.com/arduino/arduino-cli/arduino/sketches"
3131
"github.com/arduino/arduino-cli/commands"
32-
"github.com/arduino/arduino-cli/common/formatter"
3332
"github.com/arduino/arduino-cli/rpc"
3433
discovery "github.com/arduino/board-discovery"
3534
paths "github.com/arduino/go-paths-helper"
3635
)
3736

38-
func BoardAttach(ctx context.Context, req *rpc.BoardAttachReq) (*rpc.BoardAttachResp, error) {
37+
func BoardAttach(ctx context.Context, req *rpc.BoardAttachReq, taskCB commands.TaskProgressCB) (*rpc.BoardAttachResp, error) {
3938

4039
pm := commands.GetPackageManager(req)
4140
if pm == nil {
@@ -49,10 +48,8 @@ func BoardAttach(ctx context.Context, req *rpc.BoardAttachReq) (*rpc.BoardAttach
4948
if err != nil {
5049
return nil, fmt.Errorf("opening sketch: %s", err)
5150
}
52-
if sketch.Metadata == nil {
53-
formatter.Print("sketch errrorrrerereererer")
54-
}
55-
boardURI := req.GetBoardURI()
51+
52+
boardURI := req.GetBoardUri()
5653
fqbn, err := cores.ParseFQBN(boardURI)
5754
if err != nil && !strings.HasPrefix(boardURI, "serial") {
5855
boardURI = "serial://" + boardURI
@@ -80,7 +77,6 @@ func BoardAttach(ctx context.Context, req *rpc.BoardAttachReq) (*rpc.BoardAttach
8077

8178
duration, err := time.ParseDuration(req.GetSearchTimeout())
8279
if err != nil {
83-
//logrus.WithError(err).Warnf("Invalid interval `%s` provided, using default (5s).", req.GetSearchTimeout())
8480
duration = time.Second * 5
8581
}
8682

@@ -94,8 +90,10 @@ func BoardAttach(ctx context.Context, req *rpc.BoardAttachReq) (*rpc.BoardAttach
9490
if board == nil {
9591
return nil, fmt.Errorf("no supported board found at %s", deviceURI.String())
9692
}
97-
formatter.Print("Board found: " + board.Name())
93+
taskCB(&rpc.TaskProgress{Name: "Board found: " + board.Name()})
9894

95+
// TODO: should be stoped the monitor: when running as a pure CLI is released
96+
// by the OS, when run as daemon the resource's state is unknown and could be leaked.
9997
sketch.Metadata.CPU = sketches.BoardMetadata{
10098
Fqbn: board.FQBN(),
10199
Name: board.Name(),
@@ -104,14 +102,9 @@ func BoardAttach(ctx context.Context, req *rpc.BoardAttachReq) (*rpc.BoardAttach
104102

105103
err = sketch.ExportMetadata()
106104
if err != nil {
107-
<<<<<<< HEAD
108105
return nil, fmt.Errorf("cannot export sketch metadata: %s", err)
109-
=======
110-
formatter.PrintError(err, "Cannot export sketch metadata.")
111-
os.Exit(commands.ErrGeneric)
112-
>>>>>>> 5358b8ed08945f8db0242c77427e79e44f03c7d9
113106
}
114-
formatter.PrintResult("Selected fqbn: " + sketch.Metadata.CPU.Fqbn)
107+
taskCB(&rpc.TaskProgress{Name: "Selected fqbn: " + sketch.Metadata.CPU.Fqbn, Completed: true})
115108
return &rpc.BoardAttachResp{}, nil
116109
}
117110

daemon/client/client.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func main() {
8888
fmt.Println("=== calling PlatformSearch(samd)")
8989
searchResp, err := client.PlatformSearch(context.Background(), &rpc.PlatformSearchReq{
9090
Instance: instance,
91-
SearchArgs: "samd",
91+
SearchArgs: "uno",
9292
})
9393
if err != nil {
9494
fmt.Printf("Search error: %s\n", err)
@@ -196,15 +196,30 @@ func main() {
196196

197197
// BOARDS ATTACH
198198
fmt.Println("=== calling BoardAttach(serial:///dev/ttyACM0)")
199-
_, err = client.BoardAttach(context.Background(), &rpc.BoardAttachReq{
199+
boardattachresp, err := client.BoardAttach(context.Background(), &rpc.BoardAttachReq{
200200
Instance: instance,
201-
BoardURI: "serial:///dev/ttyACM0",
202-
SketchPath: "/home/riccardo/Arduino/MyFirstSketch",
201+
BoardUri: "/dev/ttyACM0",
202+
SketchPath: os.Args[2],
203203
})
204204
if err != nil {
205-
fmt.Printf("attach error : %s\n", err)
205+
fmt.Printf("Attach error: %s\n", err)
206206
os.Exit(1)
207207
}
208+
for {
209+
attachResp, err := boardattachresp.Recv()
210+
if err == io.EOF {
211+
fmt.Printf("---> %+v\n", attachResp)
212+
fmt.Println()
213+
break
214+
}
215+
if err != nil {
216+
fmt.Printf("Attach error: %s\n", err)
217+
os.Exit(1)
218+
}
219+
if attachResp.GetTaskProgress() != nil {
220+
fmt.Printf(">> TASK: %s\n", attachResp.GetTaskProgress())
221+
}
222+
}
208223

209224
// COMPILE
210225
fmt.Println("=== calling Compile(arduino:samd:mkr1000, VERBOSE, " + os.Args[2] + ")")

daemon/daemon.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,15 @@ func (s *ArduinoCoreServerImpl) BoardDetails(ctx context.Context, req *rpc.Board
6060
return board.BoardDetails(ctx, req)
6161
}
6262

63-
func (s *ArduinoCoreServerImpl) BoardAttach(ctx context.Context, req *rpc.BoardAttachReq) (*rpc.BoardAttachResp, error) {
64-
return board.BoardAttach(ctx, req)
63+
func (s *ArduinoCoreServerImpl) BoardAttach(req *rpc.BoardAttachReq, stream rpc.ArduinoCore_BoardAttachServer) error {
64+
65+
resp, err := board.BoardAttach(stream.Context(), req,
66+
func(p *rpc.TaskProgress) { stream.Send(&rpc.BoardAttachResp{TaskProgress: p}) },
67+
)
68+
if err != nil {
69+
return err
70+
}
71+
return stream.Send(resp)
6572
}
6673

6774
func (s *ArduinoCoreServerImpl) Destroy(ctx context.Context, req *rpc.DestroyReq) (*rpc.DestroyResp, error) {

rpc/board.pb.go

Lines changed: 60 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/board.proto

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@ message RequiredTool {
5454

5555
message BoardAttachReq {
5656
Instance instance = 1;
57-
string boardURI = 2;
58-
string sketchPath = 3;
59-
string boardFlavour = 4;
60-
string searchTimeout = 5;
57+
string board_uri = 2;
58+
string sketch_path = 3;
59+
string board_flavour = 4;
60+
string search_timeout = 5;
6161
}
6262

6363
message BoardAttachResp {
64+
TaskProgress task_progress = 1;
6465
}

0 commit comments

Comments
 (0)