18
18
package board
19
19
20
20
import (
21
+ "context"
21
22
"fmt"
22
23
"os"
23
24
"sort"
24
- "time"
25
25
26
- "github.com/arduino/arduino-cli/arduino/discovery"
27
26
"github.com/arduino/arduino-cli/cli"
28
- "github.com/arduino/arduino-cli/commands/core "
27
+ "github.com/arduino/arduino-cli/commands/board "
29
28
"github.com/arduino/arduino-cli/common/formatter"
30
29
"github.com/arduino/arduino-cli/output"
30
+ "github.com/arduino/arduino-cli/rpc"
31
31
"github.com/spf13/cobra"
32
32
)
33
33
@@ -52,128 +52,43 @@ var listFlags struct {
52
52
53
53
// runListCommand detects and lists the connected arduino boards
54
54
func runListCommand (cmd * cobra.Command , args []string ) {
55
- pm , _ := cli .InitPackageAndLibraryManager ()
55
+ instance := cli .CreateInstance ()
56
56
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
+ // }
81
62
82
- serialDiscovery , err := discovery . NewFromCommandLine ( serialDiscoveryTool . InstallDir . Join ( "serial-discovery" ). String () )
63
+ resp , err := board . BoardList ( context . Background (), & rpc. BoardListReq { Instance : instance } )
83
64
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 )
119
67
}
120
68
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 )
161
71
}
162
- return false
163
72
}
164
73
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
+ })
167
79
table := output .NewTable ()
168
80
table .SetHeader ("Port" , "Type" , "Board Name" , "FQBN" )
169
- for _ , port := range p .Ports {
81
+ for _ , port := range resp .Ports {
170
82
address := port .Protocol + "://" + port .Address
171
83
if port .Protocol == "serial" {
172
84
address = port .Address
173
85
}
174
86
protocol := port .ProtocolLabel
175
87
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
+ })
177
92
for _ , b := range port .Boards {
178
93
board := b .Name
179
94
fqbn := b .FQBN
@@ -188,5 +103,5 @@ func (p detectedPorts) EmitTerminal() string {
188
103
table .AddRow (address , protocol , board , fqbn )
189
104
}
190
105
}
191
- return table .Render ()
106
+ fmt . Print ( table .Render () )
192
107
}
0 commit comments