Skip to content

Commit 9f19820

Browse files
committed
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 <[email protected]>
1 parent 7607c3f commit 9f19820

File tree

8 files changed

+21
-19
lines changed

8 files changed

+21
-19
lines changed

cli/command/completion/functions.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import (
1313
"github.com/spf13/cobra"
1414
)
1515

16-
// ValidArgsFn a function to be used by cobra command as `ValidArgsFunction` to offer command line completion
17-
type ValidArgsFn func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective)
16+
// ValidArgsFn a function to be used by cobra command as `ValidArgsFunction` to offer command line completion.
17+
//
18+
// Deprecated: use [cobra.CompletionFunc].
19+
type ValidArgsFn = cobra.CompletionFunc
1820

1921
// APIClientProvider provides a method to get an [client.APIClient], initializing
2022
// it if needed.
@@ -27,7 +29,7 @@ type APIClientProvider interface {
2729
}
2830

2931
// ImageNames offers completion for images present within the local store
30-
func ImageNames(dockerCLI APIClientProvider, limit int) ValidArgsFn {
32+
func ImageNames(dockerCLI APIClientProvider, limit int) cobra.CompletionFunc {
3133
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
3234
if limit > 0 && len(args) >= limit {
3335
return nil, cobra.ShellCompDirectiveNoFileComp
@@ -47,7 +49,7 @@ func ImageNames(dockerCLI APIClientProvider, limit int) ValidArgsFn {
4749
// ContainerNames offers completion for container names and IDs
4850
// By default, only names are returned.
4951
// Set DOCKER_COMPLETION_SHOW_CONTAINER_IDS=yes to also complete IDs.
50-
func ContainerNames(dockerCLI APIClientProvider, all bool, filters ...func(container.Summary) bool) ValidArgsFn {
52+
func ContainerNames(dockerCLI APIClientProvider, all bool, filters ...func(container.Summary) bool) cobra.CompletionFunc {
5153
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
5254
list, err := dockerCLI.Client().ContainerList(cmd.Context(), container.ListOptions{
5355
All: all,
@@ -80,7 +82,7 @@ func ContainerNames(dockerCLI APIClientProvider, all bool, filters ...func(conta
8082
}
8183

8284
// VolumeNames offers completion for volumes
83-
func VolumeNames(dockerCLI APIClientProvider) ValidArgsFn {
85+
func VolumeNames(dockerCLI APIClientProvider) cobra.CompletionFunc {
8486
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
8587
list, err := dockerCLI.Client().VolumeList(cmd.Context(), volume.ListOptions{})
8688
if err != nil {
@@ -95,7 +97,7 @@ func VolumeNames(dockerCLI APIClientProvider) ValidArgsFn {
9597
}
9698

9799
// NetworkNames offers completion for networks
98-
func NetworkNames(dockerCLI APIClientProvider) ValidArgsFn {
100+
func NetworkNames(dockerCLI APIClientProvider) cobra.CompletionFunc {
99101
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
100102
list, err := dockerCLI.Client().NetworkList(cmd.Context(), network.ListOptions{})
101103
if err != nil {
@@ -133,7 +135,7 @@ func EnvVarNames(_ *cobra.Command, _ []string, _ string) (names []string, _ cobr
133135
}
134136

135137
// FromList offers completion for the given list of options.
136-
func FromList(options ...string) ValidArgsFn {
138+
func FromList(options ...string) cobra.CompletionFunc {
137139
return cobra.FixedCompletions(options, cobra.ShellCompDirectiveNoFileComp)
138140
}
139141

cli/command/config/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func NewConfigCommand(dockerCli command.Cli) *cobra.Command {
3030
}
3131

3232
// completeNames offers completion for swarm configs
33-
func completeNames(dockerCLI completion.APIClientProvider) completion.ValidArgsFn {
33+
func completeNames(dockerCLI completion.APIClientProvider) cobra.CompletionFunc {
3434
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
3535
list, err := dockerCLI.Client().ConfigList(cmd.Context(), types.ConfigListOptions{})
3636
if err != nil {

cli/command/container/completion.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func addCompletions(cmd *cobra.Command, dockerCLI completion.APIClientProvider)
144144
}
145145

146146
// completeCgroupns implements shell completion for the `--cgroupns` option of `run` and `create`.
147-
func completeCgroupns() completion.ValidArgsFn {
147+
func completeCgroupns() cobra.CompletionFunc {
148148
return completion.FromList(string(container.CgroupnsModeHost), string(container.CgroupnsModePrivate))
149149
}
150150

@@ -155,7 +155,7 @@ func completeDetachKeys(_ *cobra.Command, _ []string, _ string) ([]string, cobra
155155

156156
// completeIpc implements shell completion for the `--ipc` option of `run` and `create`.
157157
// The completion is partly composite.
158-
func completeIpc(dockerCLI completion.APIClientProvider) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
158+
func completeIpc(dockerCLI completion.APIClientProvider) cobra.CompletionFunc {
159159
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
160160
if len(toComplete) > 0 && strings.HasPrefix("container", toComplete) { //nolint:gocritic // not swapped, matches partly typed "container"
161161
return []string{"container:"}, cobra.ShellCompDirectiveNoSpace
@@ -175,7 +175,7 @@ func completeIpc(dockerCLI completion.APIClientProvider) func(cmd *cobra.Command
175175
}
176176

177177
// completeLink implements shell completion for the `--link` option of `run` and `create`.
178-
func completeLink(dockerCLI completion.APIClientProvider) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
178+
func completeLink(dockerCLI completion.APIClientProvider) cobra.CompletionFunc {
179179
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
180180
return postfixWith(":", containerNames(dockerCLI, cmd, args, toComplete)), cobra.ShellCompDirectiveNoSpace
181181
}
@@ -184,7 +184,7 @@ func completeLink(dockerCLI completion.APIClientProvider) func(cmd *cobra.Comman
184184
// completeLogDriver implements shell completion for the `--log-driver` option of `run` and `create`.
185185
// The log drivers are collected from a call to the Info endpoint with a fallback to a hard-coded list
186186
// of the build-in log drivers.
187-
func completeLogDriver(dockerCLI completion.APIClientProvider) completion.ValidArgsFn {
187+
func completeLogDriver(dockerCLI completion.APIClientProvider) cobra.CompletionFunc {
188188
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
189189
info, err := dockerCLI.Client().Info(cmd.Context())
190190
if err != nil {
@@ -206,7 +206,7 @@ func completeLogOpt(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.S
206206
}
207207

208208
// completePid implements shell completion for the `--pid` option of `run` and `create`.
209-
func completePid(dockerCLI completion.APIClientProvider) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
209+
func completePid(dockerCLI completion.APIClientProvider) cobra.CompletionFunc {
210210
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
211211
if len(toComplete) > 0 && strings.HasPrefix("container", toComplete) { //nolint:gocritic // not swapped, matches partly typed "container"
212212
return []string{"container:"}, cobra.ShellCompDirectiveNoSpace
@@ -277,7 +277,7 @@ func completeUlimit(_ *cobra.Command, _ []string, _ string) ([]string, cobra.She
277277
}
278278

279279
// completeVolumeDriver contacts the API to get the built-in and installed volume drivers.
280-
func completeVolumeDriver(dockerCLI completion.APIClientProvider) completion.ValidArgsFn {
280+
func completeVolumeDriver(dockerCLI completion.APIClientProvider) cobra.CompletionFunc {
281281
return func(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
282282
info, err := dockerCLI.Client().Info(cmd.Context())
283283
if err != nil {

cli/command/node/completion.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
// Set DOCKER_COMPLETION_SHOW_NODE_IDS=yes to also complete IDs.
1414
//
1515
// TODO(thaJeztah): add support for filters.
16-
func completeNodeNames(dockerCLI completion.APIClientProvider) completion.ValidArgsFn {
16+
func completeNodeNames(dockerCLI completion.APIClientProvider) cobra.CompletionFunc {
1717
// https://github.com/docker/cli/blob/f9ced58158d5e0b358052432244b483774a1983d/contrib/completion/bash/docker#L41-L43
1818
showIDs := os.Getenv("DOCKER_COMPLETION_SHOW_NODE_IDS") == "yes"
1919
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {

cli/command/secret/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func NewSecretCommand(dockerCli command.Cli) *cobra.Command {
3030
}
3131

3232
// completeNames offers completion for swarm secrets
33-
func completeNames(dockerCLI completion.APIClientProvider) completion.ValidArgsFn {
33+
func completeNames(dockerCLI completion.APIClientProvider) cobra.CompletionFunc {
3434
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
3535
list, err := dockerCLI.Client().SecretList(cmd.Context(), types.SecretListOptions{})
3636
if err != nil {

cli/command/service/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func NewServiceCommand(dockerCli command.Cli) *cobra.Command {
3939
// CompletionFn offers completion for swarm service names and optional IDs.
4040
// By default, only names are returned.
4141
// Set DOCKER_COMPLETION_SHOW_SERVICE_IDS=yes to also complete IDs.
42-
func CompletionFn(dockerCLI completion.APIClientProvider) completion.ValidArgsFn {
42+
func CompletionFn(dockerCLI completion.APIClientProvider) cobra.CompletionFunc {
4343
// https://github.com/docker/cli/blob/f9ced58158d5e0b358052432244b483774a1983d/contrib/completion/bash/docker#L41-L43
4444
showIDs := os.Getenv("DOCKER_COMPLETION_SHOW_SERVICE_IDS") == "yes"
4545
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {

cli/command/stack/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func NewStackCommand(dockerCli command.Cli) *cobra.Command {
4646
}
4747

4848
// completeNames offers completion for swarm stacks
49-
func completeNames(dockerCLI completion.APIClientProvider) completion.ValidArgsFn {
49+
func completeNames(dockerCLI completion.APIClientProvider) cobra.CompletionFunc {
5050
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
5151
list, err := swarm.GetStacks(cmd.Context(), dockerCLI.Client())
5252
if err != nil {

cli/command/system/completion.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ var (
8585
)
8686

8787
// completeEventFilters provides completion for the filters that can be used with `--filter`.
88-
func completeEventFilters(dockerCLI completion.APIClientProvider) completion.ValidArgsFn {
88+
func completeEventFilters(dockerCLI completion.APIClientProvider) cobra.CompletionFunc {
8989
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
9090
key, _, ok := strings.Cut(toComplete, "=")
9191
if !ok {

0 commit comments

Comments
 (0)