Skip to content

Commit 149613a

Browse files
sanialescmaglie
authored andcommitted
Fixes #89, refactors arduino lib list output 👍
1 parent 1c08aa9 commit 149613a

File tree

2 files changed

+144
-3
lines changed

2 files changed

+144
-3
lines changed

cmd/arduino_boards.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/bcmi-labs/arduino-modules/sketches"
1414

1515
"github.com/bcmi-labs/arduino-cli/cmd/formatter"
16+
"github.com/bcmi-labs/arduino-cli/cmd/output"
1617

1718
"github.com/arduino/board-discovery"
1819
"github.com/spf13/cobra"
@@ -48,6 +49,19 @@ func executeBoardListCommand(cmd *cobra.Command, args []string) {
4849
formatter.PrintErrorMessage("Not accepting additional arguments")
4950
os.Exit(errBadCall)
5051
}
52+
53+
packageFolder, err := common.GetDefaultPkgFolder()
54+
if err != nil {
55+
formatter.PrintErrorMessage("Cannot Parse Board Index file")
56+
os.Exit(errCoreConfig)
57+
}
58+
59+
bs, err := boards.Find(packageFolder)
60+
if err != nil {
61+
formatter.PrintErrorMessage("Cannot Parse Board Index file")
62+
os.Exit(errCoreConfig)
63+
}
64+
5165
monitor := discovery.New(time.Millisecond)
5266
monitor.Start()
5367
duration, err := time.ParseDuration(arduinoBoardListFlags.SearchTimeout)
@@ -59,7 +73,7 @@ func executeBoardListCommand(cmd *cobra.Command, args []string) {
5973
for {
6074
select {
6175
case <-stop:
62-
fmt.Print("\r ")
76+
fmt.Print("\r \r")
6377
return
6478
default:
6579
fmt.Print("\rDiscovering. ")
@@ -69,7 +83,6 @@ func executeBoardListCommand(cmd *cobra.Command, args []string) {
6983
fmt.Print("\rDiscovering...")
7084
time.Sleep(time.Millisecond * 500)
7185
}
72-
7386
}
7487
})
7588

@@ -81,7 +94,9 @@ func executeBoardListCommand(cmd *cobra.Command, args []string) {
8194
} else {
8295
time.Sleep(duration)
8396
}
84-
formatter.Print(*monitor)
97+
98+
formatter.Print(output.NewBoardList(bs, monitor))
99+
85100
//monitor.Stop() //If called will slow like 1sec the program to close after print, with the same result (tested).
86101
// it closes ungracefully, but at the end of the command we can't have races.
87102
}

cmd/output/board_structs.go

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* This file is part of arduino-cli.
3+
*
4+
* arduino-cli is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*
18+
* As a special exception, you may use this file as part of a free software
19+
* library without restriction. Specifically, if other files instantiate
20+
* templates or use macros or inline functions from this file, or you compile
21+
* this file and link it with other files to produce an executable, this
22+
* file does not by itself cause the resulting executable to be covered by
23+
* the GNU General Public License. This exception does not however
24+
* invalidate any other reasons why the executable file might be covered by
25+
* the GNU General Public License.
26+
*
27+
* Copyright 2017 ARDUINO AG (http://www.arduino.cc/)
28+
*/
29+
30+
package output
31+
32+
import (
33+
"fmt"
34+
35+
discovery "github.com/arduino/board-discovery"
36+
"github.com/bcmi-labs/arduino-modules/boards"
37+
)
38+
39+
// SerialBoardListItem represents a board connected using serial port.
40+
type SerialBoardListItem struct {
41+
Name string `json:"name,required"`
42+
Fqbn string `json:"fqbn,required"`
43+
Port string `json:"port,required"`
44+
UsbID string `json:"usbID,reqiured"`
45+
}
46+
47+
// NetworkBoardListItem represents a board connected via network.
48+
type NetworkBoardListItem struct {
49+
Name string `json:"name,required"`
50+
Fqbn string `json:"fqbn,required"`
51+
Location string `json:"location,required"`
52+
}
53+
54+
// BoardList is a list of attached boards.
55+
type BoardList struct {
56+
SerialBoards []SerialBoardListItem `json:"serialBoards,required"`
57+
NetworkBoards []NetworkBoardListItem `json:"networkBoards,required"`
58+
}
59+
60+
func (bl *BoardList) String() string {
61+
ret := fmt.Sprintln("DEVICES:") +
62+
fmt.Sprintln("SERIAL:")
63+
if len(bl.SerialBoards) == 0 {
64+
ret += fmt.Sprintln(" <none>")
65+
} else {
66+
for _, item := range bl.SerialBoards {
67+
ret += fmt.Sprintln(" - BOARD NAME:", item.Name) +
68+
fmt.Sprintln(" FQBN:", item.Fqbn) +
69+
fmt.Sprintln(" PORT:", item.Port) +
70+
fmt.Sprintln(" USB ID:", item.UsbID)
71+
}
72+
}
73+
ret += fmt.Sprintln("NETWORK:")
74+
if len(bl.NetworkBoards) == 0 {
75+
ret += fmt.Sprintln(" <none>")
76+
} else {
77+
for _, item := range bl.NetworkBoards {
78+
ret += fmt.Sprintln(" - BOARD NAME:", item.Name) +
79+
fmt.Sprintln(" FQBN:", item.Fqbn) +
80+
fmt.Sprintln(" LOCATION:", item.Location)
81+
}
82+
}
83+
return ret
84+
}
85+
86+
// NewBoardList returns a new board list by adding discovered boards from the board list and a monitor.
87+
func NewBoardList(boards boards.Boards, monitor *discovery.Monitor) *BoardList {
88+
if monitor == nil || boards == nil {
89+
return nil
90+
}
91+
92+
serialDevices := monitor.Serial()
93+
networkDevices := monitor.Network()
94+
ret := &BoardList{
95+
SerialBoards: make([]SerialBoardListItem, 0, len(serialDevices)),
96+
NetworkBoards: make([]NetworkBoardListItem, 0, len(networkDevices)),
97+
}
98+
99+
for _, item := range serialDevices {
100+
board := boards.ByVidPid(item.VendorID, item.ProductID)
101+
if board == nil { // skip it if not recognized
102+
continue
103+
}
104+
105+
ret.SerialBoards = append(ret.SerialBoards, SerialBoardListItem{
106+
Name: board.Name,
107+
Fqbn: board.Fqbn,
108+
Port: item.Port,
109+
UsbID: fmt.Sprintf("%s:%s - %s", item.ProductID[2:len(item.ProductID)-1], item.VendorID[2:len(item.VendorID)-1], item.SerialNumber),
110+
})
111+
}
112+
113+
for _, item := range networkDevices {
114+
board := boards.ByID(item.Name)
115+
if board == nil { // skip it if not recognized
116+
continue
117+
}
118+
119+
ret.NetworkBoards = append(ret.NetworkBoards, NetworkBoardListItem{
120+
Name: board.Name,
121+
Fqbn: board.Fqbn,
122+
Location: fmt.Sprintf("%s:%d", item.Address, item.Port),
123+
})
124+
}
125+
return ret
126+
}

0 commit comments

Comments
 (0)