From b9c7f6c67e3e92f5e3c90fe89df870cbc4ab4e45 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 11:08:41 +0000 Subject: [PATCH] Add completion for `--ipc` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 571044de42..16f6b547e3 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -98,6 +98,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("hostname", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("ip", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("ip6", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("ipc", completeIpc(dockerCli)) _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) @@ -106,6 +107,30 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("volumes-from", completion.ContainerNames(dockerCli, true)) } +// completeIpc implements shell completion for the `--ipc` option of `run` and `create`. +// The completion is partly composite. +func completeIpc(cli command.Cli) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(toComplete) > 0 && strings.HasPrefix("container", toComplete) { //nolint:gocritic + return []string{"container:"}, cobra.ShellCompDirectiveNoSpace + } + if strings.HasPrefix(toComplete, "container:") { + names, _ := completion.ContainerNames(cli, true)(cmd, args, toComplete) + return prefixWith("container:", names), cobra.ShellCompDirectiveNoFileComp + } + return []string{"container:", "host", "none", "private", "shareable"}, cobra.ShellCompDirectiveNoFileComp + } +} + +// prefixWith prefixes every element in the slice with the given prefix. +func prefixWith(prefix string, values []string) []string { + result := make([]string, len(values)) + for i, v := range values { + result[i] = prefix + v + } + return result +} + func completeLinuxCapabilityNames(cmd *cobra.Command, args []string, toComplete string) (names []string, _ cobra.ShellCompDirective) { return completion.FromList(allLinuxCapabilities()...)(cmd, args, toComplete) }