Skip to content

Commit f06a599

Browse files
committed
pluggable-discovery: use Discovery cmd-line as ID
1 parent e97f574 commit f06a599

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

arduino/discovery/discovery.go

+36-19
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,18 @@ import (
2222
"fmt"
2323
"io"
2424
"os/exec"
25+
"strings"
2526
"time"
2627

28+
"github.com/arduino/arduino-cli/arduino/cores"
2729
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
28-
29-
properties "github.com/arduino/go-properties-orderedmap"
30-
3130
"github.com/arduino/arduino-cli/executils"
31+
properties "github.com/arduino/go-properties-orderedmap"
3232
)
3333

3434
// Discovery is an instance of a discovery tool
3535
type Discovery struct {
36+
ID string
3637
in io.WriteCloser
3738
out io.ReadCloser
3839
outJSON *json.Decoder
@@ -60,7 +61,10 @@ func NewFromCommandLine(args ...string) (*Discovery, error) {
6061
if err != nil {
6162
return nil, fmt.Errorf("creating discovery process: %s", err)
6263
}
63-
return &Discovery{cmd: cmd}, nil
64+
return &Discovery{
65+
ID: strings.Join(args, " "),
66+
cmd: cmd,
67+
}, nil
6468
}
6569

6670
// Start starts the specified discovery
@@ -124,23 +128,36 @@ func (d *Discovery) Close() error {
124128
}
125129

126130
// ExtractDiscoveriesFromPlatforms returns all Discovery from all the installed platforms.
127-
func ExtractDiscoveriesFromPlatforms(pm *packagemanager.PackageManager) map[string]*Discovery {
128-
res := map[string]*Discovery{}
129-
131+
func ExtractDiscoveriesFromPlatforms(pm *packagemanager.PackageManager) []*Discovery {
132+
res := []*Discovery{}
133+
taken := map[string]bool{}
130134
for _, platformRelease := range pm.InstalledPlatformReleases() {
131-
discoveries := platformRelease.Properties.SubTree("discovery").FirstLevelOf()
135+
for _, disc := range ExtractDiscoveriesFromPlatform(platformRelease) {
136+
if taken[disc.ID] {
137+
continue
138+
}
139+
taken[disc.ID] = true
140+
res = append(res, disc)
141+
}
142+
}
143+
return res
144+
}
132145

133-
for name, props := range discoveries {
134-
if pattern, has := props.GetOk("pattern"); has {
135-
props.Merge(platformRelease.Properties)
136-
cmdLine := props.ExpandPropsInString(pattern)
137-
if cmdArgs, err := properties.SplitQuotedString(cmdLine, `"`, false); err != nil {
138-
// TODO
139-
} else if disc, err := NewFromCommandLine(cmdArgs...); err != nil {
140-
// TODO
141-
} else {
142-
res[name] = disc
143-
}
146+
// ExtractDiscoveriesFromPlatform returns all Discovery from the specified platform.
147+
func ExtractDiscoveriesFromPlatform(platformRelease *cores.PlatformRelease) []*Discovery {
148+
discoveries := platformRelease.Properties.SubTree("discovery").FirstLevelOf()
149+
150+
res := []*Discovery{}
151+
for _, props := range discoveries {
152+
if pattern, has := props.GetOk("pattern"); has {
153+
props.Merge(platformRelease.Properties)
154+
cmdLine := props.ExpandPropsInString(pattern)
155+
if cmdArgs, err := properties.SplitQuotedString(cmdLine, `"`, false); err != nil {
156+
// TODO
157+
} else if disc, err := NewFromCommandLine(cmdArgs...); err != nil {
158+
// TODO
159+
} else {
160+
res = append(res, disc)
144161
}
145162
}
146163
}

commands/board/list.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ func BoardList(ctx context.Context, req *rpc.BoardListReq) (*rpc.BoardListResp,
6767
discoveries["serial"] = serialDiscovery
6868

6969
resp := &rpc.BoardListResp{Ports: []*rpc.DetectedPort{}}
70-
for discName, disc := range discoveries {
70+
for _, disc := range discoveries {
7171
disc.Start()
7272
defer disc.Close()
7373

7474
ports, err := disc.List()
7575
if err != nil {
76-
fmt.Printf("Error getting port list from discovery %s: %s\n", discName, err)
76+
fmt.Printf("Error getting port list from discovery %s: %s\n", disc.ID, err)
7777
continue
7878
}
7979
for _, port := range ports {

0 commit comments

Comments
 (0)