From 6c6db14a07114fbf9d8604436be0cb48e679c2fe Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Wed, 18 Oct 2023 17:32:45 +0200 Subject: [PATCH] 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. --- internal/cli/arguments/completion.go | 40 +++---------------- internal/cli/arguments/port.go | 5 ++- .../completion/completion_test.go | 14 ++----- 3 files changed, 13 insertions(+), 46 deletions(-) diff --git a/internal/cli/arguments/completion.go b/internal/cli/arguments/completion.go index 7b38e1e7d11..c97ac72f064 100644 --- a/internal/cli/arguments/completion.go +++ b/internal/cli/arguments/completion.go @@ -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 { @@ -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 } diff --git a/internal/cli/arguments/port.go b/internal/cli/arguments/port.go index f0cb0a4a2e5..14f5846d80d 100644 --- a/internal/cli/arguments/port.go +++ b/internal/cli/arguments/port.go @@ -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" @@ -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) } diff --git a/internal/integrationtest/completion/completion_test.go b/internal/integrationtest/completion/completion_test.go index 0fcdbc32913..75da997a357 100644 --- a/internal/integrationtest/completion/completion_test.go +++ b/internal/integrationtest/completion/completion_test.go @@ -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", "")