From adcce2b4ef5358ab57473e076bd12ada012ae42e Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Wed, 30 Oct 2024 16:24:35 +0000 Subject: [PATCH] Improve completion for "--volume-driver" Signed-off-by: Harald Albers --- cli/command/container/completion.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index c4e093c210..c1f5c1eccc 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -183,7 +183,7 @@ func addCompletions(cmd *cobra.Command, dockerCLI completion.APIClientProvider) _ = cmd.RegisterFlagCompletionFunc("user", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("userns", completion.FromList("host")) _ = cmd.RegisterFlagCompletionFunc("uts", completion.FromList("host")) - _ = cmd.RegisterFlagCompletionFunc("volume-driver", completion.FromList("local")) + _ = cmd.RegisterFlagCompletionFunc("volume-driver", completeVolumeDriver(dockerCLI)) _ = cmd.RegisterFlagCompletionFunc("volumes-from", completion.ContainerNames(dockerCLI, true)) _ = cmd.RegisterFlagCompletionFunc("workdir", completion.NoComplete) } @@ -309,6 +309,19 @@ func completeUlimit(_ *cobra.Command, _ []string, _ string) ([]string, cobra.She return postfixWith("=", limits), cobra.ShellCompDirectiveNoSpace } +// completeVolumeDriver contacts the API to get the built-in and installed volume drivers. +func completeVolumeDriver(dockerCLI completion.APIClientProvider) completion.ValidArgsFn { + return func(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { + info, err := dockerCLI.Client().Info(cmd.Context()) + if err != nil { + // fallback: the built-in drivers + return []string{"local"}, cobra.ShellCompDirectiveNoFileComp + } + drivers := info.Plugins.Volume + return drivers, cobra.ShellCompDirectiveNoFileComp + } +} + // containerNames contacts the API to get names and optionally IDs of containers. // In case of an error, an empty list is returned. func containerNames(dockerCLI completion.APIClientProvider, cmd *cobra.Command, args []string, toComplete string) []string {