Skip to content

Commit 6e199e7

Browse files
committed
Implemented BoardListAll grpc method
1 parent 98d34b4 commit 6e199e7

File tree

10 files changed

+421
-272
lines changed

10 files changed

+421
-272
lines changed

cli/board/listall.go

+28-30
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@
1818
package board
1919

2020
import (
21+
"context"
22+
"fmt"
23+
"os"
2124
"sort"
22-
"strings"
2325

2426
"github.com/arduino/arduino-cli/cli"
27+
"github.com/arduino/arduino-cli/commands/board"
2528
"github.com/arduino/arduino-cli/common/formatter"
26-
"github.com/arduino/arduino-cli/common/formatter/output"
29+
"github.com/arduino/arduino-cli/output"
30+
"github.com/arduino/arduino-cli/rpc"
2731
"github.com/spf13/cobra"
2832
)
2933

@@ -45,36 +49,30 @@ func initListAllCommand() *cobra.Command {
4549

4650
// runListAllCommand list all installed boards
4751
func runListAllCommand(cmd *cobra.Command, args []string) {
48-
pm, _ := cli.InitPackageAndLibraryManager()
52+
instance := cli.CreateInstance()
4953

50-
match := func(name string) bool {
51-
name = strings.ToLower(name)
52-
for _, term := range args {
53-
if !strings.Contains(name, strings.ToLower(term)) {
54-
return false
55-
}
56-
}
57-
return true
54+
list, err := board.BoardListAll(context.Background(), &rpc.BoardListAllReq{
55+
Instance: instance,
56+
SearchArgs: args,
57+
})
58+
if err != nil {
59+
formatter.PrintError(err, "Error listing boards")
60+
os.Exit(cli.ErrGeneric)
5861
}
62+
if cli.OutputJSONOrElse(list) {
63+
outputBoardListAll(list)
64+
}
65+
}
66+
67+
func outputBoardListAll(list *rpc.BoardListAllResp) {
68+
sort.Slice(list.Boards, func(i, j int) bool {
69+
return list.Boards[i].GetName() < list.Boards[j].GetName()
70+
})
5971

60-
list := &output.BoardList{}
61-
for _, targetPackage := range pm.GetPackages().Packages {
62-
for _, platform := range targetPackage.Platforms {
63-
platformRelease := pm.GetInstalledPlatformRelease(platform)
64-
if platformRelease == nil {
65-
continue
66-
}
67-
for _, board := range platformRelease.Boards {
68-
if !match(board.Name()) {
69-
continue
70-
}
71-
list.Boards = append(list.Boards, &output.BoardListItem{
72-
Name: board.Name(),
73-
Fqbn: board.FQBN(),
74-
})
75-
}
76-
}
72+
table := output.NewTable()
73+
table.SetHeader("Board Name", "FQBN")
74+
for _, item := range list.GetBoards() {
75+
table.AddRow(item.GetName(), item.GetFQBN())
7776
}
78-
sort.Sort(list)
79-
formatter.Print(list)
77+
fmt.Print(table.Render())
8078
}

commands/board/list.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ func BoardList(ctx context.Context, req *rpc.BoardListReq) (*rpc.BoardListResp,
4040
continue
4141
}
4242
for _, port := range ports {
43-
b := []*rpc.DetectedBoard{}
43+
b := []*rpc.BoardListItem{}
4444
for _, board := range pm.IdentifyBoard(port.IdentificationPrefs) {
45-
b = append(b, &rpc.DetectedBoard{
45+
b = append(b, &rpc.BoardListItem{
4646
Name: board.Name(),
4747
FQBN: board.FQBN(),
4848
})

commands/board/listall.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 [email protected].
16+
*/
17+
18+
package board
19+
20+
import (
21+
"context"
22+
"errors"
23+
"strings"
24+
25+
"github.com/arduino/arduino-cli/commands"
26+
"github.com/arduino/arduino-cli/rpc"
27+
)
28+
29+
func BoardListAll(ctx context.Context, req *rpc.BoardListAllReq) (*rpc.BoardListAllResp, error) {
30+
pm := commands.GetPackageManager(req)
31+
if pm == nil {
32+
return nil, errors.New("invalid instance")
33+
}
34+
35+
args := req.GetSearchArgs()
36+
match := func(name string) bool {
37+
if len(args) == 0 {
38+
return true
39+
}
40+
name = strings.ToLower(name)
41+
for _, term := range args {
42+
if !strings.Contains(name, strings.ToLower(term)) {
43+
return false
44+
}
45+
}
46+
return true
47+
}
48+
49+
list := &rpc.BoardListAllResp{Boards: []*rpc.BoardListItem{}}
50+
for _, targetPackage := range pm.GetPackages().Packages {
51+
for _, platform := range targetPackage.Platforms {
52+
platformRelease := pm.GetInstalledPlatformRelease(platform)
53+
if platformRelease == nil {
54+
continue
55+
}
56+
for _, board := range platformRelease.Boards {
57+
if !match(board.Name()) {
58+
continue
59+
}
60+
list.Boards = append(list.Boards, &rpc.BoardListItem{
61+
Name: board.Name(),
62+
FQBN: board.FQBN(),
63+
})
64+
}
65+
}
66+
}
67+
68+
return list, nil
69+
}

common/formatter/output/board_structs.go

-35
Original file line numberDiff line numberDiff line change
@@ -58,38 +58,3 @@ func (bl *AttachedBoardList) String() string {
5858
}
5959
return fmt.Sprintln(table)
6060
}
61-
62-
// BoardListItem is a supported board
63-
type BoardListItem struct {
64-
Name string `json:"name,required"`
65-
Fqbn string `json:"fqbn,required"`
66-
}
67-
68-
// BoardList is a list of supported boards
69-
type BoardList struct {
70-
Boards []*BoardListItem `json:"boards,required"`
71-
}
72-
73-
func (bl *BoardList) String() string {
74-
table := uitable.New()
75-
table.MaxColWidth = 100
76-
table.Wrap = true // wrap columns
77-
78-
table.AddRow("Board Name", "FQBN")
79-
for _, item := range bl.Boards {
80-
table.AddRow(item.Name, item.Fqbn)
81-
}
82-
return fmt.Sprintln(table)
83-
}
84-
85-
func (bl *BoardList) Len() int {
86-
return len(bl.Boards)
87-
}
88-
89-
func (bl *BoardList) Less(i, j int) bool {
90-
return bl.Boards[i].Name < bl.Boards[j].Name
91-
}
92-
93-
func (bl *BoardList) Swap(i, j int) {
94-
bl.Boards[i], bl.Boards[j] = bl.Boards[j], bl.Boards[i]
95-
}

daemon/daemon.go

+4
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ func (s *ArduinoCoreServerImpl) BoardList(ctx context.Context, req *rpc.BoardLis
6464
return board.BoardList(ctx, req)
6565
}
6666

67+
func (s *ArduinoCoreServerImpl) BoardListAll(ctx context.Context, req *rpc.BoardListAllReq) (*rpc.BoardListAllResp, error) {
68+
return board.BoardListAll(ctx, req)
69+
}
70+
6771
func (s *ArduinoCoreServerImpl) BoardAttach(req *rpc.BoardAttachReq, stream rpc.ArduinoCore_BoardAttachServer) error {
6872

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

0 commit comments

Comments
 (0)