Skip to content

Commit 4954c4d

Browse files
committed
Replaced direct access to PackageManager to get discovery protocols
Previously the function GetConnectedBoards() counter-intuitively returned a list of port address. Now it has been reneamed GetAvailablePorts() and returns the full Port object that is mapped into an array of Addresses or into an array of Prorocols based on the auto-completion request.
1 parent 0ca03b6 commit 4954c4d

File tree

2 files changed

+9
-36
lines changed

2 files changed

+9
-36
lines changed

Diff for: internal/cli/arguments/completion.go

+6-34
Original file line numberDiff line numberDiff line change
@@ -45,34 +45,6 @@ func GetInstalledBoards() []string {
4545
return res
4646
}
4747

48-
// GetInstalledProtocols is an helper function useful to autocomplete.
49-
// It returns a list of protocols available based on the installed boards
50-
func GetInstalledProtocols() []string {
51-
inst := instance.CreateAndInit()
52-
53-
boards := pme.InstalledBoards()
54-
55-
installedProtocols := make(map[string]struct{})
56-
for _, board := range boards {
57-
for _, protocol := range board.Properties.SubTree("upload.tool").FirstLevelKeys() {
58-
if protocol == "default" {
59-
// default is used as fallback when trying to upload to a board
60-
// using a protocol not defined for it, it's useless showing it
61-
// in autocompletion
62-
continue
63-
}
64-
installedProtocols[protocol] = struct{}{}
65-
}
66-
}
67-
res := make([]string, len(installedProtocols))
68-
i := 0
69-
for k := range installedProtocols {
70-
res[i] = k
71-
i++
72-
}
73-
return res
74-
}
75-
7648
// GetInstalledProgrammers is an helper function useful to autocomplete.
7749
// It returns a list of programmers available based on the installed boards
7850
func GetInstalledProgrammers() []string {
@@ -188,19 +160,19 @@ func GetInstallableLibs() []string {
188160
return res
189161
}
190162

191-
// GetConnectedBoards is an helper function useful to autocomplete.
192-
// It returns a list of boards which are currently connected
193-
// Obviously it does not suggests network ports because of the timeout
194-
func GetConnectedBoards() []string {
163+
// GetAvailablePorts is an helper function useful to autocomplete.
164+
// It returns a list of upload port of the boards which are currently connected.
165+
// It will not suggests network ports because the timeout is not set.
166+
func GetAvailablePorts() []*rpc.Port {
195167
inst := instance.CreateAndInit()
196168

197169
list, _, _ := board.List(&rpc.BoardListRequest{
198170
Instance: inst,
199171
})
200-
var res []string
172+
var res []*rpc.Port
201173
// transform the data structure for the completion
202174
for _, i := range list {
203-
res = append(res, i.Port.Address)
175+
res = append(res, i.Port)
204176
}
205177
return res
206178
}

Diff for: internal/cli/arguments/port.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/arduino/arduino-cli/arduino"
2424
"github.com/arduino/arduino-cli/commands/board"
25+
f "github.com/arduino/arduino-cli/internal/algorithms"
2526
"github.com/arduino/arduino-cli/internal/cli/feedback"
2627
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2728
"github.com/sirupsen/logrus"
@@ -41,11 +42,11 @@ type Port struct {
4142
func (p *Port) AddToCommand(cmd *cobra.Command) {
4243
cmd.Flags().StringVarP(&p.address, "port", "p", "", tr("Upload port address, e.g.: COM3 or /dev/ttyACM2"))
4344
cmd.RegisterFlagCompletionFunc("port", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
44-
return GetConnectedBoards(), cobra.ShellCompDirectiveDefault
45+
return f.Map(GetAvailablePorts(), (*rpc.Port).GetAddress), cobra.ShellCompDirectiveDefault
4546
})
4647
cmd.Flags().StringVarP(&p.protocol, "protocol", "l", "", tr("Upload port protocol, e.g: serial"))
4748
cmd.RegisterFlagCompletionFunc("protocol", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
48-
return GetInstalledProtocols(), cobra.ShellCompDirectiveDefault
49+
return f.Map(GetAvailablePorts(), (*rpc.Port).GetProtocol), cobra.ShellCompDirectiveDefault
4950
})
5051
p.timeout.AddToCommand(cmd)
5152
}

0 commit comments

Comments
 (0)