Skip to content

Commit

Permalink
Replaced direct access to PackageManager to get discovery protocols
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cmaglie committed Oct 18, 2023
1 parent 0ca03b6 commit 6c6db14
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 46 deletions.
40 changes: 6 additions & 34 deletions internal/cli/arguments/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,34 +45,6 @@ func GetInstalledBoards() []string {
return res
}

// GetInstalledProtocols is an helper function useful to autocomplete.
// It returns a list of protocols available based on the installed boards
func GetInstalledProtocols() []string {
inst := instance.CreateAndInit()

boards := pme.InstalledBoards()

installedProtocols := make(map[string]struct{})
for _, board := range boards {
for _, protocol := range board.Properties.SubTree("upload.tool").FirstLevelKeys() {
if protocol == "default" {
// default is used as fallback when trying to upload to a board
// using a protocol not defined for it, it's useless showing it
// in autocompletion
continue
}
installedProtocols[protocol] = struct{}{}
}
}
res := make([]string, len(installedProtocols))
i := 0
for k := range installedProtocols {
res[i] = k
i++
}
return res
}

// GetInstalledProgrammers is an helper function useful to autocomplete.
// It returns a list of programmers available based on the installed boards
func GetInstalledProgrammers() []string {
Expand Down Expand Up @@ -188,19 +160,19 @@ func GetInstallableLibs() []string {
return res
}

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

list, _, _ := board.List(&rpc.BoardListRequest{
Instance: inst,
})
var res []string
var res []*rpc.Port
// transform the data structure for the completion
for _, i := range list {
res = append(res, i.Port.Address)
res = append(res, i.Port)
}
return res
}
5 changes: 3 additions & 2 deletions internal/cli/arguments/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/arduino/arduino-cli/arduino"
"github.com/arduino/arduino-cli/commands/board"
f "github.com/arduino/arduino-cli/internal/algorithms"
"github.com/arduino/arduino-cli/internal/cli/feedback"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/sirupsen/logrus"
Expand All @@ -41,11 +42,11 @@ type Port struct {
func (p *Port) AddToCommand(cmd *cobra.Command) {
cmd.Flags().StringVarP(&p.address, "port", "p", "", tr("Upload port address, e.g.: COM3 or /dev/ttyACM2"))
cmd.RegisterFlagCompletionFunc("port", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return GetConnectedBoards(), cobra.ShellCompDirectiveDefault
return f.Map(GetAvailablePorts(), (*rpc.Port).GetAddress), cobra.ShellCompDirectiveDefault
})
cmd.Flags().StringVarP(&p.protocol, "protocol", "l", "", tr("Upload port protocol, e.g: serial"))
cmd.RegisterFlagCompletionFunc("protocol", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return GetInstalledProtocols(), cobra.ShellCompDirectiveDefault
return f.Map(GetAvailablePorts(), (*rpc.Port).GetProtocol), cobra.ShellCompDirectiveDefault
})
p.timeout.AddToCommand(cmd)
}
Expand Down
14 changes: 4 additions & 10 deletions internal/integrationtest/completion/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,10 @@ func TestCoreCompletion(t *testing.T) {
require.Contains(t, string(stdout), "arduino:avr:uno")
stdout, _, _ = cli.Run("__complete", "monitor", "-b", "")
require.Contains(t, string(stdout), "arduino:avr:uno")
stdout, _, _ = cli.Run("__complete", "burn-bootloader", "-l", "")
require.Contains(t, string(stdout), "network")
stdout, _, _ = cli.Run("__complete", "compile", "-l", "")
require.Contains(t, string(stdout), "network")
stdout, _, _ = cli.Run("__complete", "debug", "-l", "")
require.Contains(t, string(stdout), "network")
stdout, _, _ = cli.Run("__complete", "upload", "-l", "")
require.Contains(t, string(stdout), "network")
stdout, _, _ = cli.Run("__complete", "monitor", "-l", "")
require.Contains(t, string(stdout), "network")

// -l/--protocol and -p/--port cannot be tested because there are
// no board connected.

stdout, _, _ = cli.Run("__complete", "burn-bootloader", "-P", "")
require.Contains(t, string(stdout), "atmel_ice")
stdout, _, _ = cli.Run("__complete", "compile", "-P", "")
Expand Down

0 comments on commit 6c6db14

Please sign in to comment.