diff --git a/cli/command/completion/functions.go b/cli/command/completion/functions.go index dc17619237b4..b217ec54b05d 100644 --- a/cli/command/completion/functions.go +++ b/cli/command/completion/functions.go @@ -91,30 +91,21 @@ func VolumeNames(dockerCLI APIClientProvider) ValidArgsFn { } } -// CompleteNetworkNames offers completion for networks -func CompleteNetworkNames(dockerCLI APIClientProvider) ValidArgsFn { +// NetworkNames offers completion for networks +func NetworkNames(dockerCLI APIClientProvider) ValidArgsFn { return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - names, err := NetworkNames(dockerCLI, cmd) + list, err := dockerCLI.Client().NetworkList(cmd.Context(), network.ListOptions{}) if err != nil { return nil, cobra.ShellCompDirectiveError } + var names []string + for _, nw := range list { + names = append(names, nw.Name) + } return names, cobra.ShellCompDirectiveNoFileComp } } -// NetworkNames returns a list of networks -func NetworkNames(dockerCLI APIClientProvider, cmd *cobra.Command) ([]string, error) { - summaries, err := dockerCLI.Client().NetworkList(cmd.Context(), network.ListOptions{}) - if err != nil { - return nil, err - } - var names []string - for _, nws := range summaries { - names = append(names, nws.Name) - } - return names, nil -} - // EnvVarNames offers completion for environment-variable names. This // completion can be used for "--env" and "--build-arg" flags, which // allow obtaining the value of the given environment-variable if present diff --git a/cli/command/container/create.go b/cli/command/container/create.go index 63a5fec7f581..31ddeaad0167 100644 --- a/cli/command/container/create.go +++ b/cli/command/container/create.go @@ -82,7 +82,7 @@ func NewCreateCommand(dockerCli command.Cli) *cobra.Command { _ = cmd.RegisterFlagCompletionFunc("cap-drop", completeLinuxCapabilityNames) _ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames) _ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames) - _ = cmd.RegisterFlagCompletionFunc("network", completion.CompleteNetworkNames(dockerCli)) + _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) _ = cmd.RegisterFlagCompletionFunc("restart", completeRestartPolicies) diff --git a/cli/command/container/run.go b/cli/command/container/run.go index 11e4907784e1..a3fc5f983a3d 100644 --- a/cli/command/container/run.go +++ b/cli/command/container/run.go @@ -73,7 +73,7 @@ func NewRunCommand(dockerCli command.Cli) *cobra.Command { _ = cmd.RegisterFlagCompletionFunc("cap-drop", completeLinuxCapabilityNames) _ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames) _ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames) - _ = cmd.RegisterFlagCompletionFunc("network", completion.CompleteNetworkNames(dockerCli)) + _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) _ = cmd.RegisterFlagCompletionFunc("restart", completeRestartPolicies) diff --git a/cli/command/network/connect.go b/cli/command/network/connect.go index b2eced5766b2..0d9e87125975 100644 --- a/cli/command/network/connect.go +++ b/cli/command/network/connect.go @@ -41,7 +41,7 @@ func newConnectCommand(dockerCLI command.Cli) *cobra.Command { }, ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { if len(args) == 0 { - return completion.CompleteNetworkNames(dockerCLI)(cmd, args, toComplete) + return completion.NetworkNames(dockerCLI)(cmd, args, toComplete) } nw := args[0] return completion.ContainerNames(dockerCLI, true, not(isConnected(nw)))(cmd, args, toComplete) diff --git a/cli/command/network/disconnect.go b/cli/command/network/disconnect.go index 37e13acb8bcf..521aee33f509 100644 --- a/cli/command/network/disconnect.go +++ b/cli/command/network/disconnect.go @@ -31,7 +31,7 @@ func newDisconnectCommand(dockerCli command.Cli) *cobra.Command { }, ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { if len(args) == 0 { - return completion.CompleteNetworkNames(dockerCli)(cmd, args, toComplete) + return completion.NetworkNames(dockerCli)(cmd, args, toComplete) } network := args[0] return completion.ContainerNames(dockerCli, true, isConnected(network))(cmd, args, toComplete) diff --git a/cli/command/network/inspect.go b/cli/command/network/inspect.go index 1cb58c9189b1..72f66382fadc 100644 --- a/cli/command/network/inspect.go +++ b/cli/command/network/inspect.go @@ -34,7 +34,7 @@ func newInspectCommand(dockerCLI command.Cli) *cobra.Command { opts.names = args return runInspect(cmd.Context(), dockerCLI.Client(), dockerCLI.Out(), opts) }, - ValidArgsFunction: completion.CompleteNetworkNames(dockerCLI), + ValidArgsFunction: completion.NetworkNames(dockerCLI), } cmd.Flags().StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp) diff --git a/cli/command/network/remove.go b/cli/command/network/remove.go index e46b7dd71642..105e629f614c 100644 --- a/cli/command/network/remove.go +++ b/cli/command/network/remove.go @@ -27,7 +27,7 @@ func newRemoveCommand(dockerCli command.Cli) *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { return runRemove(cmd.Context(), dockerCli, args, &opts) }, - ValidArgsFunction: completion.CompleteNetworkNames(dockerCli), + ValidArgsFunction: completion.NetworkNames(dockerCli), } flags := cmd.Flags() diff --git a/cli/command/system/completion.go b/cli/command/system/completion.go index 4cde18736342..21e0623f986f 100644 --- a/cli/command/system/completion.go +++ b/cli/command/system/completion.go @@ -69,10 +69,8 @@ func completeFilters(dockerCLI completion.APIClientProvider) completion.ValidArg return nil, cobra.ShellCompDirectiveNoFileComp } if strings.HasPrefix(toComplete, "network=") { - names, err := completion.NetworkNames(dockerCLI, cmd) - if err != nil { - return nil, cobra.ShellCompDirectiveError - } + // the pure network name list should be pulled out from NetworkNames. + names, _ := completion.NetworkNames(dockerCLI)(cmd, args, toComplete) return prefixWith("network=", names), cobra.ShellCompDirectiveDefault } if strings.HasPrefix(toComplete, "type=") {