From 761d76750c8eb9edfc7e13cae8e3db6e3f4db573 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Tue, 22 Oct 2024 19:40:31 +0000 Subject: [PATCH] Share the container completions Signed-off-by: Harald Albers --- cli/command/container/completion.go | 14 ++++++++++++++ cli/command/container/create.go | 12 ++---------- cli/command/container/run.go | 12 ++---------- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index e6ca17308a..fb8193b4ca 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -54,6 +54,20 @@ var restartPolicies = []string{ string(container.RestartPolicyUnlessStopped), } +// addCompletions adds the completions that `run` and `create` have in common. +func addCompletions(cmd *cobra.Command, dockerCLI completion.APIClientProvider) { + _ = cmd.RegisterFlagCompletionFunc("cap-add", completeLinuxCapabilityNames) + _ = cmd.RegisterFlagCompletionFunc("cap-drop", completeLinuxCapabilityNames) + _ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames) + _ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames) + _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCLI)) + _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) + _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) + _ = cmd.RegisterFlagCompletionFunc("restart", completeRestartPolicies) + _ = cmd.RegisterFlagCompletionFunc("stop-signal", completeSignals) + _ = cmd.RegisterFlagCompletionFunc("volumes-from", completion.ContainerNames(dockerCLI, true)) +} + func completeLinuxCapabilityNames(cmd *cobra.Command, args []string, toComplete string) (names []string, _ cobra.ShellCompDirective) { return completion.FromList(allLinuxCapabilities()...)(cmd, args, toComplete) } diff --git a/cli/command/container/create.go b/cli/command/container/create.go index 31ddeaad01..00b7436a47 100644 --- a/cli/command/container/create.go +++ b/cli/command/container/create.go @@ -78,16 +78,8 @@ func NewCreateCommand(dockerCli command.Cli) *cobra.Command { command.AddTrustVerificationFlags(flags, &options.untrusted, dockerCli.ContentTrustEnabled()) copts = addFlags(flags) - _ = cmd.RegisterFlagCompletionFunc("cap-add", completeLinuxCapabilityNames) - _ = cmd.RegisterFlagCompletionFunc("cap-drop", completeLinuxCapabilityNames) - _ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames) - _ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames) - _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) - _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) - _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) - _ = cmd.RegisterFlagCompletionFunc("restart", completeRestartPolicies) - _ = cmd.RegisterFlagCompletionFunc("stop-signal", completeSignals) - _ = cmd.RegisterFlagCompletionFunc("volumes-from", completion.ContainerNames(dockerCli, true)) + addCompletions(cmd, dockerCli) + return cmd } diff --git a/cli/command/container/run.go b/cli/command/container/run.go index a3fc5f983a..ad8afcab82 100644 --- a/cli/command/container/run.go +++ b/cli/command/container/run.go @@ -69,16 +69,8 @@ func NewRunCommand(dockerCli command.Cli) *cobra.Command { command.AddTrustVerificationFlags(flags, &options.untrusted, dockerCli.ContentTrustEnabled()) copts = addFlags(flags) - _ = cmd.RegisterFlagCompletionFunc("cap-add", completeLinuxCapabilityNames) - _ = cmd.RegisterFlagCompletionFunc("cap-drop", completeLinuxCapabilityNames) - _ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames) - _ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames) - _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) - _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) - _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) - _ = cmd.RegisterFlagCompletionFunc("restart", completeRestartPolicies) - _ = cmd.RegisterFlagCompletionFunc("stop-signal", completeSignals) - _ = cmd.RegisterFlagCompletionFunc("volumes-from", completion.ContainerNames(dockerCli, true)) + addCompletions(cmd, dockerCli) + return cmd }