Skip to content

Commit 7d59776

Browse files
authored
[skip-changelog] Removed now useless boilerplate code for BoardList (#1328)
* Removed now useless boilerplate for BoardList * Fixed wrong 'break' I guess it was meant to exit from the outer loop and not the select.
1 parent b4b4ad6 commit 7d59776

File tree

3 files changed

+20
-85
lines changed

3 files changed

+20
-85
lines changed

Diff for: commands/board/list.go

+5-10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"sync"
2727

2828
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
29+
"github.com/arduino/arduino-cli/arduino/discovery"
2930
"github.com/arduino/arduino-cli/commands"
3031
"github.com/arduino/arduino-cli/httpclient"
3132
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
@@ -101,7 +102,7 @@ func apiByVidPid(vid, pid string) ([]*rpc.BoardListItem, error) {
101102
return retVal, nil
102103
}
103104

104-
func identifyViaCloudAPI(port *commands.BoardPort) ([]*rpc.BoardListItem, error) {
105+
func identifyViaCloudAPI(port *discovery.Port) ([]*rpc.BoardListItem, error) {
105106
// If the port is not USB do not try identification via cloud
106107
id := port.Properties
107108
if !id.ContainsKey("vid") || !id.ContainsKey("pid") {
@@ -113,7 +114,7 @@ func identifyViaCloudAPI(port *commands.BoardPort) ([]*rpc.BoardListItem, error)
113114
}
114115

115116
// identify returns a list of boards checking first the installed platforms or the Cloud API
116-
func identify(pm *packagemanager.PackageManager, port *commands.BoardPort) ([]*rpc.BoardListItem, error) {
117+
func identify(pm *packagemanager.PackageManager, port *discovery.Port) ([]*rpc.BoardListItem, error) {
117118
boards := []*rpc.BoardListItem{}
118119

119120
// first query installed cores through the Package Manager
@@ -236,13 +237,7 @@ func Watch(instanceID int32, interrupt <-chan bool) (<-chan *rpc.BoardListWatchR
236237
boards := []*rpc.BoardListItem{}
237238
boardsError := ""
238239
if event.Type == "add" {
239-
boards, err = identify(pm, &commands.BoardPort{
240-
Address: event.Port.Address,
241-
Label: event.Port.AddressLabel,
242-
Properties: event.Port.Properties,
243-
Protocol: event.Port.Protocol,
244-
ProtocolLabel: event.Port.ProtocolLabel,
245-
})
240+
boards, err = identify(pm, event.Port)
246241
if err != nil {
247242
boardsError = err.Error()
248243
}
@@ -265,7 +260,7 @@ func Watch(instanceID int32, interrupt <-chan bool) (<-chan *rpc.BoardListWatchR
265260
Error: boardsError,
266261
}
267262
case <-interrupt:
268-
break
263+
return
269264
}
270265
}
271266
}()

Diff for: commands/board/list_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"testing"
2424

2525
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
26-
"github.com/arduino/arduino-cli/commands"
26+
"github.com/arduino/arduino-cli/arduino/discovery"
2727
"github.com/arduino/arduino-cli/configuration"
2828
"github.com/arduino/go-paths-helper"
2929
"github.com/arduino/go-properties-orderedmap"
@@ -106,7 +106,7 @@ func TestGetByVidPidMalformedResponse(t *testing.T) {
106106
}
107107

108108
func TestBoardDetectionViaAPIWithNonUSBPort(t *testing.T) {
109-
port := &commands.BoardPort{
109+
port := &discovery.Port{
110110
Properties: properties.NewMap(),
111111
}
112112
items, err := identifyViaCloudAPI(port)
@@ -152,7 +152,7 @@ func TestBoardIdentifySorting(t *testing.T) {
152152
idPrefs := properties.NewMap()
153153
idPrefs.Set("vid", "0x0000")
154154
idPrefs.Set("pid", "0x0000")
155-
res, err := identify(pm, &commands.BoardPort{Properties: idPrefs})
155+
res, err := identify(pm, &discovery.Port{Properties: idPrefs})
156156
require.NoError(t, err)
157157
require.NotNil(t, res)
158158
require.Len(t, res, 4)

Diff for: commands/bundled_tools_serial_discovery.go

+12-72
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,13 @@
1616
package commands
1717

1818
import (
19-
"encoding/json"
2019
"fmt"
2120
"sync"
22-
"time"
2321

2422
"github.com/arduino/arduino-cli/arduino/cores"
2523
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2624
"github.com/arduino/arduino-cli/arduino/discovery"
2725
"github.com/arduino/arduino-cli/arduino/resources"
28-
"github.com/arduino/arduino-cli/executils"
29-
"github.com/arduino/go-properties-orderedmap"
30-
"github.com/pkg/errors"
3126
semver "go.bug.st/relaxed-semver"
3227
)
3328

@@ -107,24 +102,10 @@ var (
107102
}
108103
)
109104

110-
// BoardPort is a generic port descriptor
111-
type BoardPort struct {
112-
Address string `json:"address"`
113-
Label string `json:"label"`
114-
Properties *properties.Map `json:"properties"`
115-
Protocol string `json:"protocol"`
116-
ProtocolLabel string `json:"protocolLabel"`
117-
}
118-
119-
type eventJSON struct {
120-
EventType string `json:"eventType,required"`
121-
Ports []*BoardPort `json:"ports"`
122-
}
123-
124105
var listBoardMutex sync.Mutex
125106

126107
// ListBoards foo
127-
func ListBoards(pm *packagemanager.PackageManager) ([]*BoardPort, error) {
108+
func ListBoards(pm *packagemanager.PackageManager) ([]*discovery.Port, error) {
128109
// ensure the connection to the discoverer is unique to avoid messing up
129110
// the messages exchanged
130111
listBoardMutex.Lock()
@@ -141,67 +122,26 @@ func ListBoards(pm *packagemanager.PackageManager) ([]*BoardPort, error) {
141122
return nil, fmt.Errorf("missing serial-discovery tool")
142123
}
143124

144-
// build the command to be executed
145-
cmd, err := executils.NewProcessFromPath(t.InstallDir.Join("serial-discovery"))
146-
if err != nil {
147-
return nil, errors.Wrap(err, "creating discovery process")
148-
}
149-
150-
// attach in/out pipes to the process
151-
in, err := cmd.StdinPipe()
152-
if err != nil {
153-
return nil, fmt.Errorf("creating stdin pipe for discovery: %s", err)
154-
}
155-
156-
out, err := cmd.StdoutPipe()
125+
disc, err := discovery.New("serial-discovery", t.InstallDir.Join(t.Tool.Name).String())
157126
if err != nil {
158-
return nil, fmt.Errorf("creating stdout pipe for discovery: %s", err)
127+
return nil, err
159128
}
160-
outJSON := json.NewDecoder(out)
129+
defer disc.Quit()
161130

162-
// start the process
163-
if err := cmd.Start(); err != nil {
164-
return nil, fmt.Errorf("starting discovery process: %s", err)
131+
if err = disc.Hello(); err != nil {
132+
return nil, fmt.Errorf("starting discovery: %v", err)
165133
}
166134

167-
// send the LIST command
168-
if _, err := in.Write([]byte("LIST\n")); err != nil {
169-
return nil, fmt.Errorf("sending LIST command to discovery: %s", err)
135+
if err = disc.Start(); err != nil {
136+
return nil, fmt.Errorf("starting discovery: %v", err)
170137
}
171138

172-
// read the response from the pipe
173-
decodeResult := make(chan error)
174-
var event eventJSON
175-
go func() {
176-
decodeResult <- outJSON.Decode(&event)
177-
}()
178-
179-
var finalError error
180-
var retVal []*BoardPort
181-
182-
// wait for the response
183-
select {
184-
case err := <-decodeResult:
185-
if err == nil {
186-
retVal = event.Ports
187-
} else {
188-
finalError = err
189-
}
190-
case <-time.After(10 * time.Second):
191-
finalError = fmt.Errorf("decoding LIST command: timeout")
139+
res, err := disc.List()
140+
if err != nil {
141+
return nil, fmt.Errorf("getting port list from discovery: %v", err)
192142
}
193143

194-
// tell the process to quit
195-
in.Write([]byte("QUIT\n"))
196-
in.Close()
197-
out.Close()
198-
// kill the process if it takes too long to quit
199-
time.AfterFunc(time.Second, func() {
200-
cmd.Kill()
201-
})
202-
cmd.Wait()
203-
204-
return retVal, finalError
144+
return res, nil
205145
}
206146

207147
// WatchListBoards returns a channel that receives events from the bundled discovery tool

0 commit comments

Comments
 (0)