Skip to content

Commit

Permalink
Add completion for --volume-driver
Browse files Browse the repository at this point in the history
Signed-off-by: Harald Albers <[email protected]>
  • Loading branch information
albers committed Nov 8, 2024
1 parent db0ed1e commit 4525fe3
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cli/command/container/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func addCompletions(cmd *cobra.Command, dockerCLI completion.APIClientProvider)
_ = cmd.RegisterFlagCompletionFunc("ulimit", completeUlimit)
_ = cmd.RegisterFlagCompletionFunc("userns", completion.FromList("host"))
_ = cmd.RegisterFlagCompletionFunc("uts", completion.FromList("host"))
_ = cmd.RegisterFlagCompletionFunc("volume-driver", completeVolumeDriver(dockerCLI))
_ = cmd.RegisterFlagCompletionFunc("volumes-from", completion.ContainerNames(dockerCLI, true))
}

Expand Down Expand Up @@ -272,6 +273,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 {
Expand Down

0 comments on commit 4525fe3

Please sign in to comment.