From 9f19820f883f1091256090279a94874d2fecba9d Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 17 Feb 2025 10:58:43 +0100 Subject: [PATCH] cli/command/completion: deprecate ValidArgsFn Cobra now defines a CompletionFunc for the same, so we can alias it to that, and stop using our own definition. Signed-off-by: Sebastiaan van Stijn --- cli/command/completion/functions.go | 16 +++++++++------- cli/command/config/cmd.go | 2 +- cli/command/container/completion.go | 12 ++++++------ cli/command/node/completion.go | 2 +- cli/command/secret/cmd.go | 2 +- cli/command/service/cmd.go | 2 +- cli/command/stack/cmd.go | 2 +- cli/command/system/completion.go | 2 +- 8 files changed, 21 insertions(+), 19 deletions(-) diff --git a/cli/command/completion/functions.go b/cli/command/completion/functions.go index ddb8442211..919528b06f 100644 --- a/cli/command/completion/functions.go +++ b/cli/command/completion/functions.go @@ -13,8 +13,10 @@ import ( "github.com/spf13/cobra" ) -// ValidArgsFn a function to be used by cobra command as `ValidArgsFunction` to offer command line completion -type ValidArgsFn func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) +// ValidArgsFn a function to be used by cobra command as `ValidArgsFunction` to offer command line completion. +// +// Deprecated: use [cobra.CompletionFunc]. +type ValidArgsFn = cobra.CompletionFunc // APIClientProvider provides a method to get an [client.APIClient], initializing // it if needed. @@ -27,7 +29,7 @@ type APIClientProvider interface { } // ImageNames offers completion for images present within the local store -func ImageNames(dockerCLI APIClientProvider, limit int) ValidArgsFn { +func ImageNames(dockerCLI APIClientProvider, limit int) cobra.CompletionFunc { return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { if limit > 0 && len(args) >= limit { return nil, cobra.ShellCompDirectiveNoFileComp @@ -47,7 +49,7 @@ func ImageNames(dockerCLI APIClientProvider, limit int) ValidArgsFn { // ContainerNames offers completion for container names and IDs // By default, only names are returned. // Set DOCKER_COMPLETION_SHOW_CONTAINER_IDS=yes to also complete IDs. -func ContainerNames(dockerCLI APIClientProvider, all bool, filters ...func(container.Summary) bool) ValidArgsFn { +func ContainerNames(dockerCLI APIClientProvider, all bool, filters ...func(container.Summary) bool) cobra.CompletionFunc { return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { list, err := dockerCLI.Client().ContainerList(cmd.Context(), container.ListOptions{ All: all, @@ -80,7 +82,7 @@ func ContainerNames(dockerCLI APIClientProvider, all bool, filters ...func(conta } // VolumeNames offers completion for volumes -func VolumeNames(dockerCLI APIClientProvider) ValidArgsFn { +func VolumeNames(dockerCLI APIClientProvider) cobra.CompletionFunc { return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { list, err := dockerCLI.Client().VolumeList(cmd.Context(), volume.ListOptions{}) if err != nil { @@ -95,7 +97,7 @@ func VolumeNames(dockerCLI APIClientProvider) ValidArgsFn { } // NetworkNames offers completion for networks -func NetworkNames(dockerCLI APIClientProvider) ValidArgsFn { +func NetworkNames(dockerCLI APIClientProvider) cobra.CompletionFunc { return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { list, err := dockerCLI.Client().NetworkList(cmd.Context(), network.ListOptions{}) if err != nil { @@ -133,7 +135,7 @@ func EnvVarNames(_ *cobra.Command, _ []string, _ string) (names []string, _ cobr } // FromList offers completion for the given list of options. -func FromList(options ...string) ValidArgsFn { +func FromList(options ...string) cobra.CompletionFunc { return cobra.FixedCompletions(options, cobra.ShellCompDirectiveNoFileComp) } diff --git a/cli/command/config/cmd.go b/cli/command/config/cmd.go index b8e84037cb..6c1969dff7 100644 --- a/cli/command/config/cmd.go +++ b/cli/command/config/cmd.go @@ -30,7 +30,7 @@ func NewConfigCommand(dockerCli command.Cli) *cobra.Command { } // completeNames offers completion for swarm configs -func completeNames(dockerCLI completion.APIClientProvider) completion.ValidArgsFn { +func completeNames(dockerCLI completion.APIClientProvider) cobra.CompletionFunc { return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { list, err := dockerCLI.Client().ConfigList(cmd.Context(), types.ConfigListOptions{}) if err != nil { diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 76129a950a..e3a511899d 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -144,7 +144,7 @@ func addCompletions(cmd *cobra.Command, dockerCLI completion.APIClientProvider) } // completeCgroupns implements shell completion for the `--cgroupns` option of `run` and `create`. -func completeCgroupns() completion.ValidArgsFn { +func completeCgroupns() cobra.CompletionFunc { return completion.FromList(string(container.CgroupnsModeHost), string(container.CgroupnsModePrivate)) } @@ -155,7 +155,7 @@ func completeDetachKeys(_ *cobra.Command, _ []string, _ string) ([]string, cobra // completeIpc implements shell completion for the `--ipc` option of `run` and `create`. // The completion is partly composite. -func completeIpc(dockerCLI completion.APIClientProvider) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { +func completeIpc(dockerCLI completion.APIClientProvider) cobra.CompletionFunc { return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { if len(toComplete) > 0 && strings.HasPrefix("container", toComplete) { //nolint:gocritic // not swapped, matches partly typed "container" return []string{"container:"}, cobra.ShellCompDirectiveNoSpace @@ -175,7 +175,7 @@ func completeIpc(dockerCLI completion.APIClientProvider) func(cmd *cobra.Command } // completeLink implements shell completion for the `--link` option of `run` and `create`. -func completeLink(dockerCLI completion.APIClientProvider) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { +func completeLink(dockerCLI completion.APIClientProvider) cobra.CompletionFunc { return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { return postfixWith(":", containerNames(dockerCLI, cmd, args, toComplete)), cobra.ShellCompDirectiveNoSpace } @@ -184,7 +184,7 @@ func completeLink(dockerCLI completion.APIClientProvider) func(cmd *cobra.Comman // completeLogDriver implements shell completion for the `--log-driver` option of `run` and `create`. // The log drivers are collected from a call to the Info endpoint with a fallback to a hard-coded list // of the build-in log drivers. -func completeLogDriver(dockerCLI completion.APIClientProvider) completion.ValidArgsFn { +func completeLogDriver(dockerCLI completion.APIClientProvider) cobra.CompletionFunc { return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { info, err := dockerCLI.Client().Info(cmd.Context()) if err != nil { @@ -206,7 +206,7 @@ func completeLogOpt(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.S } // completePid implements shell completion for the `--pid` option of `run` and `create`. -func completePid(dockerCLI completion.APIClientProvider) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { +func completePid(dockerCLI completion.APIClientProvider) cobra.CompletionFunc { return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { if len(toComplete) > 0 && strings.HasPrefix("container", toComplete) { //nolint:gocritic // not swapped, matches partly typed "container" return []string{"container:"}, cobra.ShellCompDirectiveNoSpace @@ -277,7 +277,7 @@ func completeUlimit(_ *cobra.Command, _ []string, _ string) ([]string, cobra.She } // completeVolumeDriver contacts the API to get the built-in and installed volume drivers. -func completeVolumeDriver(dockerCLI completion.APIClientProvider) completion.ValidArgsFn { +func completeVolumeDriver(dockerCLI completion.APIClientProvider) cobra.CompletionFunc { return func(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { info, err := dockerCLI.Client().Info(cmd.Context()) if err != nil { diff --git a/cli/command/node/completion.go b/cli/command/node/completion.go index 3b07b801e7..8b64861ac6 100644 --- a/cli/command/node/completion.go +++ b/cli/command/node/completion.go @@ -13,7 +13,7 @@ import ( // Set DOCKER_COMPLETION_SHOW_NODE_IDS=yes to also complete IDs. // // TODO(thaJeztah): add support for filters. -func completeNodeNames(dockerCLI completion.APIClientProvider) completion.ValidArgsFn { +func completeNodeNames(dockerCLI completion.APIClientProvider) cobra.CompletionFunc { // https://github.com/docker/cli/blob/f9ced58158d5e0b358052432244b483774a1983d/contrib/completion/bash/docker#L41-L43 showIDs := os.Getenv("DOCKER_COMPLETION_SHOW_NODE_IDS") == "yes" return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { diff --git a/cli/command/secret/cmd.go b/cli/command/secret/cmd.go index 4b5dc26332..1d3a8304e8 100644 --- a/cli/command/secret/cmd.go +++ b/cli/command/secret/cmd.go @@ -30,7 +30,7 @@ func NewSecretCommand(dockerCli command.Cli) *cobra.Command { } // completeNames offers completion for swarm secrets -func completeNames(dockerCLI completion.APIClientProvider) completion.ValidArgsFn { +func completeNames(dockerCLI completion.APIClientProvider) cobra.CompletionFunc { return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { list, err := dockerCLI.Client().SecretList(cmd.Context(), types.SecretListOptions{}) if err != nil { diff --git a/cli/command/service/cmd.go b/cli/command/service/cmd.go index 66214d99c2..0596d3d995 100644 --- a/cli/command/service/cmd.go +++ b/cli/command/service/cmd.go @@ -39,7 +39,7 @@ func NewServiceCommand(dockerCli command.Cli) *cobra.Command { // CompletionFn offers completion for swarm service names and optional IDs. // By default, only names are returned. // Set DOCKER_COMPLETION_SHOW_SERVICE_IDS=yes to also complete IDs. -func CompletionFn(dockerCLI completion.APIClientProvider) completion.ValidArgsFn { +func CompletionFn(dockerCLI completion.APIClientProvider) cobra.CompletionFunc { // https://github.com/docker/cli/blob/f9ced58158d5e0b358052432244b483774a1983d/contrib/completion/bash/docker#L41-L43 showIDs := os.Getenv("DOCKER_COMPLETION_SHOW_SERVICE_IDS") == "yes" return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { diff --git a/cli/command/stack/cmd.go b/cli/command/stack/cmd.go index dba4e2929d..e46f49ad97 100644 --- a/cli/command/stack/cmd.go +++ b/cli/command/stack/cmd.go @@ -46,7 +46,7 @@ func NewStackCommand(dockerCli command.Cli) *cobra.Command { } // completeNames offers completion for swarm stacks -func completeNames(dockerCLI completion.APIClientProvider) completion.ValidArgsFn { +func completeNames(dockerCLI completion.APIClientProvider) cobra.CompletionFunc { return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { list, err := swarm.GetStacks(cmd.Context(), dockerCLI.Client()) if err != nil { diff --git a/cli/command/system/completion.go b/cli/command/system/completion.go index 0f70d1a8bf..fad57b67f2 100644 --- a/cli/command/system/completion.go +++ b/cli/command/system/completion.go @@ -85,7 +85,7 @@ var ( ) // completeEventFilters provides completion for the filters that can be used with `--filter`. -func completeEventFilters(dockerCLI completion.APIClientProvider) completion.ValidArgsFn { +func completeEventFilters(dockerCLI completion.APIClientProvider) cobra.CompletionFunc { return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { key, _, ok := strings.Cut(toComplete, "=") if !ok {