Skip to content

Commit e97f574

Browse files
committed
Implemented GRPC call BoardList (WIP)
1 parent b8cc951 commit e97f574

File tree

8 files changed

+494
-226
lines changed

8 files changed

+494
-226
lines changed

cli/board/list.go

+25-110
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818
package board
1919

2020
import (
21+
"context"
2122
"fmt"
2223
"os"
2324
"sort"
24-
"time"
2525

26-
"github.com/arduino/arduino-cli/arduino/discovery"
2726
"github.com/arduino/arduino-cli/cli"
28-
"github.com/arduino/arduino-cli/commands/core"
27+
"github.com/arduino/arduino-cli/commands/board"
2928
"github.com/arduino/arduino-cli/common/formatter"
3029
"github.com/arduino/arduino-cli/output"
30+
"github.com/arduino/arduino-cli/rpc"
3131
"github.com/spf13/cobra"
3232
)
3333

@@ -52,128 +52,43 @@ var listFlags struct {
5252

5353
// runListCommand detects and lists the connected arduino boards
5454
func runListCommand(cmd *cobra.Command, args []string) {
55-
pm, _ := cli.InitPackageAndLibraryManager()
55+
instance := cli.CreateInstance()
5656

57-
timeout, err := time.ParseDuration(listFlags.timeout)
58-
if err != nil {
59-
formatter.PrintError(err, "Invalid timeout.")
60-
os.Exit(cli.ErrBadArgument)
61-
}
62-
63-
// Check for bultin serial-discovery tool
64-
loadBuiltinSerialDiscoveryMetadata(pm)
65-
serialDiscoveryTool, _ := getBuiltinSerialDiscoveryTool(pm)
66-
if !serialDiscoveryTool.IsInstalled() {
67-
formatter.Print("Downloading and installing missing tool: " + serialDiscoveryTool.String())
68-
core.DownloadToolRelease(pm, serialDiscoveryTool, cli.OutputProgressBar())
69-
core.InstallToolRelease(pm, serialDiscoveryTool, cli.OutputTaskProgress())
70-
71-
if err := pm.LoadHardware(cli.Config); err != nil {
72-
formatter.PrintError(err, "Could not load hardware packages.")
73-
os.Exit(cli.ErrCoreConfig)
74-
}
75-
serialDiscoveryTool, _ = getBuiltinSerialDiscoveryTool(pm)
76-
if !serialDiscoveryTool.IsInstalled() {
77-
formatter.PrintErrorMessage("Missing serial-discovery tool.")
78-
os.Exit(cli.ErrCoreConfig)
79-
}
80-
}
57+
// timeout, err := time.ParseDuration(listFlags.timeout)
58+
// if err != nil {
59+
// formatter.PrintError(err, "Invalid timeout.")
60+
// os.Exit(cli.ErrBadArgument)
61+
// }
8162

82-
serialDiscovery, err := discovery.NewFromCommandLine(serialDiscoveryTool.InstallDir.Join("serial-discovery").String())
63+
resp, err := board.BoardList(context.Background(), &rpc.BoardListReq{Instance: instance})
8364
if err != nil {
84-
formatter.PrintError(err, "Error setting up serial-discovery tool.")
85-
os.Exit(cli.ErrCoreConfig)
86-
}
87-
88-
// Find all installed discoveries
89-
discoveries := discovery.ExtractDiscoveriesFromPlatforms(pm)
90-
discoveries["serial"] = serialDiscovery
91-
92-
res := &detectedPorts{Ports: []*detectedPort{}}
93-
for discName, disc := range discoveries {
94-
disc.Timeout = timeout
95-
disc.Start()
96-
defer disc.Close()
97-
98-
ports, err := disc.List()
99-
if err != nil {
100-
fmt.Printf("Error getting port list from discovery %s: %s\n", discName, err)
101-
continue
102-
}
103-
for _, port := range ports {
104-
b := detectedBoards{}
105-
for _, board := range pm.IdentifyBoard(port.IdentificationPrefs) {
106-
b = append(b, &detectedBoard{
107-
Name: board.Name(),
108-
FQBN: board.FQBN(),
109-
})
110-
}
111-
p := &detectedPort{
112-
Address: port.Address,
113-
Protocol: port.Protocol,
114-
ProtocolLabel: port.ProtocolLabel,
115-
Boards: b,
116-
}
117-
res.Ports = append(res.Ports, p)
118-
}
65+
formatter.PrintError(err, "Error detecting boards")
66+
os.Exit(cli.ErrNetwork)
11967
}
12068

121-
if cli.OutputJSONOrElse(res) {
122-
fmt.Print(res.EmitTerminal())
123-
}
124-
}
125-
126-
type detectedPorts struct {
127-
Ports []*detectedPort `json:"ports"`
128-
}
129-
130-
type detectedPort struct {
131-
Address string `json:"address"`
132-
Protocol string `json:"protocol"`
133-
ProtocolLabel string `json:"protocol_label"`
134-
Boards detectedBoards `json:"boards"`
135-
}
136-
137-
type detectedBoards []*detectedBoard
138-
139-
type detectedBoard struct {
140-
Name string `json:"name"`
141-
FQBN string `json:"fqbn"`
142-
}
143-
144-
func (b detectedBoards) Less(i, j int) bool {
145-
x := b[i]
146-
y := b[j]
147-
if x.Name < y.Name {
148-
return true
149-
}
150-
return x.FQBN < y.FQBN
151-
}
152-
153-
func (p detectedPorts) Less(i, j int) bool {
154-
x := p.Ports[i]
155-
y := p.Ports[j]
156-
if x.Protocol < y.Protocol {
157-
return true
158-
}
159-
if x.Address < y.Address {
160-
return true
69+
if cli.OutputJSONOrElse(resp) {
70+
outputListResp(resp)
16171
}
162-
return false
16372
}
16473

165-
func (p detectedPorts) EmitTerminal() string {
166-
sort.Slice(p.Ports, p.Less)
74+
func outputListResp(resp *rpc.BoardListResp) {
75+
sort.Slice(resp.Ports, func(i, j int) bool {
76+
x, y := resp.Ports[i], resp.Ports[j]
77+
return x.Protocol < y.Protocol || (x.Protocol == y.Protocol && x.Address < y.Address)
78+
})
16779
table := output.NewTable()
16880
table.SetHeader("Port", "Type", "Board Name", "FQBN")
169-
for _, port := range p.Ports {
81+
for _, port := range resp.Ports {
17082
address := port.Protocol + "://" + port.Address
17183
if port.Protocol == "serial" {
17284
address = port.Address
17385
}
17486
protocol := port.ProtocolLabel
17587
if len(port.Boards) > 0 {
176-
sort.Slice(port.Boards, port.Boards.Less)
88+
sort.Slice(port.Boards, func(i, j int) bool {
89+
x, y := port.Boards[i], port.Boards[j]
90+
return x.Name < y.Name || (x.Name == y.Name && x.FQBN < y.FQBN)
91+
})
17792
for _, b := range port.Boards {
17893
board := b.Name
17994
fqbn := b.FQBN
@@ -188,5 +103,5 @@ func (p detectedPorts) EmitTerminal() string {
188103
table.AddRow(address, protocol, board, fqbn)
189104
}
190105
}
191-
return table.Render()
106+
fmt.Print(table.Render())
192107
}

commands/board/list.go

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* This file is part of arduino-cli.
3+
*
4+
* Copyright 2018 ARDUINO SA (http://www.arduino.cc/)
5+
*
6+
* This software is released under the GNU General Public License version 3,
7+
* which covers the main part of arduino-cli.
8+
* The terms of this license can be found at:
9+
* https://www.gnu.org/licenses/gpl-3.0.en.html
10+
*
11+
* You can be released from the requirements of the above licenses by purchasing
12+
* a commercial license. Buying such a license is mandatory if you want to modify or
13+
* otherwise use the software for commercial activities involving the Arduino
14+
* software without disclosing the source code of your own applications. To purchase
15+
* a commercial license, send an email to license@arduino.cc.
16+
*/
17+
18+
package board
19+
20+
import (
21+
"context"
22+
"errors"
23+
"fmt"
24+
"os"
25+
26+
"github.com/arduino/arduino-cli/arduino/discovery"
27+
"github.com/arduino/arduino-cli/cli"
28+
"github.com/arduino/arduino-cli/commands"
29+
"github.com/arduino/arduino-cli/commands/core"
30+
"github.com/arduino/arduino-cli/common/formatter"
31+
"github.com/arduino/arduino-cli/rpc"
32+
)
33+
34+
func BoardList(ctx context.Context, req *rpc.BoardListReq) (*rpc.BoardListResp, error) {
35+
pm := commands.GetPackageManager(req)
36+
if pm == nil {
37+
return nil, errors.New("invalid instance")
38+
}
39+
40+
// Check for bultin serial-discovery tool
41+
loadBuiltinSerialDiscoveryMetadata(pm)
42+
serialDiscoveryTool, _ := getBuiltinSerialDiscoveryTool(pm)
43+
if !serialDiscoveryTool.IsInstalled() {
44+
formatter.Print("Downloading and installing missing tool: " + serialDiscoveryTool.String())
45+
core.DownloadToolRelease(pm, serialDiscoveryTool, cli.OutputProgressBar())
46+
core.InstallToolRelease(pm, serialDiscoveryTool, cli.OutputTaskProgress())
47+
48+
if err := pm.LoadHardware(cli.Config); err != nil {
49+
formatter.PrintError(err, "Could not load hardware packages.")
50+
os.Exit(cli.ErrCoreConfig)
51+
}
52+
serialDiscoveryTool, _ = getBuiltinSerialDiscoveryTool(pm)
53+
if !serialDiscoveryTool.IsInstalled() {
54+
formatter.PrintErrorMessage("Missing serial-discovery tool.")
55+
os.Exit(cli.ErrCoreConfig)
56+
}
57+
}
58+
59+
serialDiscovery, err := discovery.NewFromCommandLine(serialDiscoveryTool.InstallDir.Join("serial-discovery").String())
60+
if err != nil {
61+
formatter.PrintError(err, "Error setting up serial-discovery tool.")
62+
os.Exit(cli.ErrCoreConfig)
63+
}
64+
65+
// Find all installed discoveries
66+
discoveries := discovery.ExtractDiscoveriesFromPlatforms(pm)
67+
discoveries["serial"] = serialDiscovery
68+
69+
resp := &rpc.BoardListResp{Ports: []*rpc.DetectedPort{}}
70+
for discName, disc := range discoveries {
71+
disc.Start()
72+
defer disc.Close()
73+
74+
ports, err := disc.List()
75+
if err != nil {
76+
fmt.Printf("Error getting port list from discovery %s: %s\n", discName, err)
77+
continue
78+
}
79+
for _, port := range ports {
80+
b := []*rpc.DetectedBoard{}
81+
for _, board := range pm.IdentifyBoard(port.IdentificationPrefs) {
82+
b = append(b, &rpc.DetectedBoard{
83+
Name: board.Name(),
84+
FQBN: board.FQBN(),
85+
})
86+
}
87+
p := &rpc.DetectedPort{
88+
Address: port.Address,
89+
Protocol: port.Protocol,
90+
ProtocolLabel: port.ProtocolLabel,
91+
Boards: b,
92+
}
93+
resp.Ports = append(resp.Ports, p)
94+
}
95+
}
96+
97+
return resp, nil
98+
}
File renamed without changes.

daemon/daemon.go

+4
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ func (s *ArduinoCoreServerImpl) BoardDetails(ctx context.Context, req *rpc.Board
6060
return board.BoardDetails(ctx, req)
6161
}
6262

63+
func (s *ArduinoCoreServerImpl) BoardList(ctx context.Context, req *rpc.BoardListReq) (*rpc.BoardListResp, error) {
64+
return board.BoardList(ctx, req)
65+
}
66+
6367
func (s *ArduinoCoreServerImpl) BoardAttach(req *rpc.BoardAttachReq, stream rpc.ArduinoCore_BoardAttachServer) error {
6468

6569
resp, err := board.BoardAttach(stream.Context(), req,

0 commit comments

Comments
 (0)