Skip to content

Commit

Permalink
pluggable-discovery: use Discovery cmd-line as ID
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed May 23, 2019
1 parent e97f574 commit f06a599
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
55 changes: 36 additions & 19 deletions arduino/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@ import (
"fmt"
"io"
"os/exec"
"strings"
"time"

"github.com/arduino/arduino-cli/arduino/cores"
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"

properties "github.com/arduino/go-properties-orderedmap"

"github.com/arduino/arduino-cli/executils"
properties "github.com/arduino/go-properties-orderedmap"
)

// Discovery is an instance of a discovery tool
type Discovery struct {
ID string
in io.WriteCloser
out io.ReadCloser
outJSON *json.Decoder
Expand Down Expand Up @@ -60,7 +61,10 @@ func NewFromCommandLine(args ...string) (*Discovery, error) {
if err != nil {
return nil, fmt.Errorf("creating discovery process: %s", err)
}
return &Discovery{cmd: cmd}, nil
return &Discovery{
ID: strings.Join(args, " "),
cmd: cmd,
}, nil
}

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

// ExtractDiscoveriesFromPlatforms returns all Discovery from all the installed platforms.
func ExtractDiscoveriesFromPlatforms(pm *packagemanager.PackageManager) map[string]*Discovery {
res := map[string]*Discovery{}

func ExtractDiscoveriesFromPlatforms(pm *packagemanager.PackageManager) []*Discovery {
res := []*Discovery{}
taken := map[string]bool{}
for _, platformRelease := range pm.InstalledPlatformReleases() {
discoveries := platformRelease.Properties.SubTree("discovery").FirstLevelOf()
for _, disc := range ExtractDiscoveriesFromPlatform(platformRelease) {
if taken[disc.ID] {
continue
}
taken[disc.ID] = true
res = append(res, disc)
}
}
return res
}

for name, props := range discoveries {
if pattern, has := props.GetOk("pattern"); has {
props.Merge(platformRelease.Properties)
cmdLine := props.ExpandPropsInString(pattern)
if cmdArgs, err := properties.SplitQuotedString(cmdLine, `"`, false); err != nil {
// TODO
} else if disc, err := NewFromCommandLine(cmdArgs...); err != nil {
// TODO
} else {
res[name] = disc
}
// ExtractDiscoveriesFromPlatform returns all Discovery from the specified platform.
func ExtractDiscoveriesFromPlatform(platformRelease *cores.PlatformRelease) []*Discovery {
discoveries := platformRelease.Properties.SubTree("discovery").FirstLevelOf()

res := []*Discovery{}
for _, props := range discoveries {
if pattern, has := props.GetOk("pattern"); has {
props.Merge(platformRelease.Properties)
cmdLine := props.ExpandPropsInString(pattern)
if cmdArgs, err := properties.SplitQuotedString(cmdLine, `"`, false); err != nil {
// TODO
} else if disc, err := NewFromCommandLine(cmdArgs...); err != nil {
// TODO
} else {
res = append(res, disc)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions commands/board/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ func BoardList(ctx context.Context, req *rpc.BoardListReq) (*rpc.BoardListResp,
discoveries["serial"] = serialDiscovery

resp := &rpc.BoardListResp{Ports: []*rpc.DetectedPort{}}
for discName, disc := range discoveries {
for _, disc := range discoveries {
disc.Start()
defer disc.Close()

ports, err := disc.List()
if err != nil {
fmt.Printf("Error getting port list from discovery %s: %s\n", discName, err)
fmt.Printf("Error getting port list from discovery %s: %s\n", disc.ID, err)
continue
}
for _, port := range ports {
Expand Down

0 comments on commit f06a599

Please sign in to comment.