Skip to content

Commit 7e547b0

Browse files
authored
clean up plugglable discovery and refactor serial-discovery usage (#327)
1 parent a57cf78 commit 7e547b0

17 files changed

+190
-356
lines changed

Diff for: arduino/discovery/discovery.go

-171
This file was deleted.

Diff for: cli/board/list.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package board
1919

2020
import (
21-
"context"
2221
"fmt"
2322
"os"
2423
"sort"
@@ -61,7 +60,7 @@ func runListCommand(cmd *cobra.Command, args []string) {
6160
time.Sleep(timeout)
6261
}
6362

64-
resp, err := board.List(context.Background(), &rpc.BoardListReq{Instance: instance.CreateInstance()})
63+
resp, err := board.List(instance.CreateInstance().GetId())
6564
if err != nil {
6665
formatter.PrintError(err, "Error detecting boards")
6766
os.Exit(errorcodes.ErrNetwork)

Diff for: commands/board/attach.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import (
3737
// Attach FIXMEDOC
3838
func Attach(ctx context.Context, req *rpc.BoardAttachReq, taskCB commands.TaskProgressCB) (*rpc.BoardAttachResp, error) {
3939

40-
pm := commands.GetPackageManager(req)
40+
pm := commands.GetPackageManager(req.GetInstance().GetId())
4141
if pm == nil {
4242
return nil, errors.New("invalid instance")
4343
}

Diff for: commands/board/details.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929

3030
// Details FIXMEDOC
3131
func Details(ctx context.Context, req *rpc.BoardDetailsReq) (*rpc.BoardDetailsResp, error) {
32-
pm := commands.GetPackageManager(req)
32+
pm := commands.GetPackageManager(req.GetInstance().GetId())
3333
if pm == nil {
3434
return nil, errors.New("invalid instance")
3535
}

Diff for: commands/board/list.go

+32-26
Original file line numberDiff line numberDiff line change
@@ -18,44 +18,50 @@
1818
package board
1919

2020
import (
21-
"context"
22-
"errors"
23-
"fmt"
24-
2521
"github.com/arduino/arduino-cli/commands"
2622
rpc "github.com/arduino/arduino-cli/rpc/commands"
23+
"github.com/pkg/errors"
2724
)
2825

2926
// List FIXMEDOC
30-
func List(ctx context.Context, req *rpc.BoardListReq) (*rpc.BoardListResp, error) {
31-
pm := commands.GetPackageManager(req)
27+
func List(instanceID int32) (*rpc.BoardListResp, error) {
28+
pm := commands.GetPackageManager(instanceID)
3229
if pm == nil {
3330
return nil, errors.New("invalid instance")
3431
}
3532

33+
serialDiscovery, err := commands.NewBuiltinSerialDiscovery(pm)
34+
if err != nil {
35+
return nil, errors.Wrap(err, "unable to instance serial-discovery")
36+
}
37+
38+
if err := serialDiscovery.Start(); err != nil {
39+
return nil, errors.Wrap(err, "unable to start serial-discovery")
40+
}
41+
defer serialDiscovery.Close()
42+
3643
resp := &rpc.BoardListResp{Ports: []*rpc.DetectedPort{}}
37-
for _, disc := range commands.GetDiscoveries(req) {
38-
ports, err := disc.List()
39-
if err != nil {
40-
fmt.Printf("Error getting port list from discovery %s: %s\n", disc.ID, err)
41-
continue
44+
45+
ports, err := serialDiscovery.List()
46+
if err != nil {
47+
return nil, errors.Wrap(err, "error getting port list from serial-discovery")
48+
}
49+
50+
for _, port := range ports {
51+
b := []*rpc.BoardListItem{}
52+
for _, board := range pm.IdentifyBoard(port.IdentificationPrefs) {
53+
b = append(b, &rpc.BoardListItem{
54+
Name: board.Name(),
55+
FQBN: board.FQBN(),
56+
})
4257
}
43-
for _, port := range ports {
44-
b := []*rpc.BoardListItem{}
45-
for _, board := range pm.IdentifyBoard(port.IdentificationPrefs) {
46-
b = append(b, &rpc.BoardListItem{
47-
Name: board.Name(),
48-
FQBN: board.FQBN(),
49-
})
50-
}
51-
p := &rpc.DetectedPort{
52-
Address: port.Address,
53-
Protocol: port.Protocol,
54-
ProtocolLabel: port.ProtocolLabel,
55-
Boards: b,
56-
}
57-
resp.Ports = append(resp.Ports, p)
58+
p := &rpc.DetectedPort{
59+
Address: port.Address,
60+
Protocol: port.Protocol,
61+
ProtocolLabel: port.ProtocolLabel,
62+
Boards: b,
5863
}
64+
resp.Ports = append(resp.Ports, p)
5965
}
6066

6167
return resp, nil

Diff for: commands/board/listall.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828

2929
// ListAll FIXMEDOC
3030
func ListAll(ctx context.Context, req *rpc.BoardListAllReq) (*rpc.BoardListAllResp, error) {
31-
pm := commands.GetPackageManager(req)
31+
pm := commands.GetPackageManager(req.GetInstance().GetId())
3232
if pm == nil {
3333
return nil, errors.New("invalid instance")
3434
}

0 commit comments

Comments
 (0)