From cc981e30ba2628946568a987b6bcb6908a958c35 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Tue, 22 Oct 2024 19:40:31 +0000 Subject: [PATCH 01/59] Share the container completions Signed-off-by: Harald Albers --- cli/command/container/completion.go | 15 +++++++++++++++ cli/command/container/create.go | 12 ++---------- cli/command/container/run.go | 12 ++---------- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index e6ca17308aee..23fb0817e019 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -4,6 +4,7 @@ import ( "strings" "sync" + "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command/completion" "github.com/docker/docker/api/types/container" "github.com/moby/sys/capability" @@ -54,6 +55,20 @@ var restartPolicies = []string{ string(container.RestartPolicyUnlessStopped), } +// addCompletions adds the completions that `run` and `create` have in common. +func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { + _ = 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 31ddeaad0167..00b7436a471a 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 a3fc5f983a3d..ad8afcab8200 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 } From d9766a3c65af66a13fea2d90b112d15cdd975fea Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Wed, 23 Oct 2024 13:04:58 +0000 Subject: [PATCH 02/59] Disable file completion for `--add-host` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 23fb0817e019..351d26f265a7 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -57,6 +57,7 @@ var restartPolicies = []string{ // addCompletions adds the completions that `run` and `create` have in common. func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { + _ = cmd.RegisterFlagCompletionFunc("add-host", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("cap-add", completeLinuxCapabilityNames) _ = cmd.RegisterFlagCompletionFunc("cap-drop", completeLinuxCapabilityNames) _ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames) From 98d5b0389db9a3819d8960dba5883d394d90ff61 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Wed, 23 Oct 2024 13:05:09 +0000 Subject: [PATCH 03/59] Disable file completion for `--annotation` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 351d26f265a7..65818af97a50 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -58,6 +58,7 @@ var restartPolicies = []string{ // addCompletions adds the completions that `run` and `create` have in common. func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("add-host", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("annotation", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("cap-add", completeLinuxCapabilityNames) _ = cmd.RegisterFlagCompletionFunc("cap-drop", completeLinuxCapabilityNames) _ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames) From 55d4192c3aa1f7733205c5fb8133b3c7bc108eb1 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Wed, 23 Oct 2024 13:09:13 +0000 Subject: [PATCH 04/59] Add completion for `--attach` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 65818af97a50..3d7902b0632c 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -59,6 +59,7 @@ var restartPolicies = []string{ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("add-host", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("annotation", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("attach", completion.FromList("stderr", "stdin", "stdout")) _ = cmd.RegisterFlagCompletionFunc("cap-add", completeLinuxCapabilityNames) _ = cmd.RegisterFlagCompletionFunc("cap-drop", completeLinuxCapabilityNames) _ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames) From 75d80775cef2d4197203ccd0a380cb9549911bdf Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Wed, 23 Oct 2024 13:14:07 +0000 Subject: [PATCH 05/59] Disable file completion for `--blkio-weight*` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 3d7902b0632c..e7169a6e5da2 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -60,6 +60,8 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("add-host", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("annotation", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("attach", completion.FromList("stderr", "stdin", "stdout")) + _ = cmd.RegisterFlagCompletionFunc("blkio-weight", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("blkio-weight-device", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("cap-add", completeLinuxCapabilityNames) _ = cmd.RegisterFlagCompletionFunc("cap-drop", completeLinuxCapabilityNames) _ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames) From fe1db01af176cbe4f975c6ea0b1ef512d40e259b Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Wed, 23 Oct 2024 13:16:12 +0000 Subject: [PATCH 06/59] Disable file completion for `--cgroup-parent` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index e7169a6e5da2..b57791dfdbf7 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -64,6 +64,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("blkio-weight-device", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("cap-add", completeLinuxCapabilityNames) _ = cmd.RegisterFlagCompletionFunc("cap-drop", completeLinuxCapabilityNames) + _ = cmd.RegisterFlagCompletionFunc("cgroup-parent", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames) _ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames) _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) From 00e3b7cdcc11e1490d32a62ba5155c98a7599f6a Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Wed, 23 Oct 2024 13:18:13 +0000 Subject: [PATCH 07/59] Add completion for `--cgroupns` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index b57791dfdbf7..9af903e80abc 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -65,6 +65,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("cap-add", completeLinuxCapabilityNames) _ = cmd.RegisterFlagCompletionFunc("cap-drop", completeLinuxCapabilityNames) _ = cmd.RegisterFlagCompletionFunc("cgroup-parent", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("cgroupns", completion.FromList("host", "private")) _ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames) _ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames) _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) From 4450f48f07a8a0139e0d24f99c2afd91f14a1761 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Wed, 23 Oct 2024 13:39:28 +0000 Subject: [PATCH 08/59] Disable file completion for `--cpu*` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 9af903e80abc..932cb0f412bc 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -66,6 +66,14 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("cap-drop", completeLinuxCapabilityNames) _ = cmd.RegisterFlagCompletionFunc("cgroup-parent", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("cgroupns", completion.FromList("host", "private")) + _ = cmd.RegisterFlagCompletionFunc("cpu-period", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("cpu-quota", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("cpu-rt-period", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("cpu-rt-runtime", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("cpu-shares", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("cpus", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("cpuset-cpus", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("cpuset-mems", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames) _ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames) _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) From 285d0e32be53b5d024b623ebc3483d0ea25d46cf Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Wed, 23 Oct 2024 17:17:13 +0000 Subject: [PATCH 09/59] Disable file completion for `--device-*` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 932cb0f412bc..de83afc0541b 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -74,6 +74,11 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("cpus", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("cpuset-cpus", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("cpuset-mems", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("device-cgroup-rule", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("device-read-bps", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("device-read-iops", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("device-write-bps", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("device-write-iops", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames) _ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames) _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) From 14038a915db9fd844ea49add07c675e9f4a18909 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 18:34:31 +0000 Subject: [PATCH 10/59] Disable file completion for `--dns*` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index de83afc0541b..377072d01c51 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -79,6 +79,9 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("device-read-iops", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("device-write-bps", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("device-write-iops", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("dns", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("dns-option", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("dns-search", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames) _ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames) _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) From 85ab1210ab019786417c6f15bf9119cbc88ddf23 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 18:38:18 +0000 Subject: [PATCH 11/59] Disable file completion for `--domainname` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 377072d01c51..e990f98b9dab 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -82,6 +82,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("dns", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("dns-option", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("dns-search", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("domainname", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames) _ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames) _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) From 9906847f5461bed001f72374f94a3aa6d23b9c6c Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Wed, 23 Oct 2024 17:19:47 +0000 Subject: [PATCH 12/59] Disable file completion for `--entrypoint` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index e990f98b9dab..8f9600e8718e 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -83,6 +83,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("dns-option", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("dns-search", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("domainname", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("entrypoint", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames) _ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames) _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) From 6aba94e6dd72ea025e06b95e2762e5403d62bc6c Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 18:23:09 +0000 Subject: [PATCH 13/59] Disable file completion for `--expose` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 8f9600e8718e..6a7377e369ae 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -84,6 +84,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("dns-search", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("domainname", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("entrypoint", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("expose", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames) _ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames) _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) From a5aa7c903779861e733ee4b47b6a6cbd8c668183 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 18:23:22 +0000 Subject: [PATCH 14/59] Disable file completion for `--gpus` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 6a7377e369ae..8ad3e742ec04 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -87,6 +87,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("expose", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames) _ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames) + _ = cmd.RegisterFlagCompletionFunc("gpus", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) From d963cfc964de75459d49bff31cdae5c5d0d07879 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Wed, 23 Oct 2024 17:24:14 +0000 Subject: [PATCH 15/59] Disable file completion for `--group-add` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 8ad3e742ec04..3e7fae39262c 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -88,6 +88,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames) _ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames) _ = cmd.RegisterFlagCompletionFunc("gpus", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("group-add", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) From 3ab5af735abb0372451e3bd38578a2c07f72a887 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Wed, 23 Oct 2024 17:26:56 +0000 Subject: [PATCH 16/59] Disable file completion for `--health-*` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 3e7fae39262c..7f45162678a3 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -89,6 +89,12 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames) _ = cmd.RegisterFlagCompletionFunc("gpus", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("group-add", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("health-cmd", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("health-interval", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("health-retries", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("health-start-interval", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("health-start-period", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("health-timeout", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) From e08a00701b1753784a2ad586dab343f3fba64458 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Wed, 23 Oct 2024 17:27:22 +0000 Subject: [PATCH 17/59] Disable file completion for `--hostname` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 7f45162678a3..6e5645f910bb 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -95,6 +95,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("health-start-interval", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("health-start-period", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("health-timeout", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("hostname", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) From 784ad51c67097df3008f90a3acf2cd79c7f8ce8d Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Wed, 23 Oct 2024 17:32:13 +0000 Subject: [PATCH 18/59] Disable file completion for `--ip6*` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 6e5645f910bb..571044de4292 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -96,6 +96,8 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("health-start-period", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("health-timeout", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("hostname", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("ip", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("ip6", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) From b9c7f6c67e3e92f5e3c90fe89df870cbc4ab4e45 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 11:08:41 +0000 Subject: [PATCH 19/59] 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 571044de4292..16f6b547e3e5 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) } From 77fd7d7a16e212ef7526c0a8c949b707b20841a0 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 18:47:14 +0000 Subject: [PATCH 20/59] Disable file completion for `--isolation` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 16f6b547e3e5..498d738f5bd0 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -99,6 +99,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("ip", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("ip6", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("ipc", completeIpc(dockerCli)) + _ = cmd.RegisterFlagCompletionFunc("isolation", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) From 33d870adb79f9365e4fb8b07b0cf5d4d2f96d782 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 11:12:45 +0000 Subject: [PATCH 21/59] Disable file completion for `--kernel-memory` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 498d738f5bd0..df17e49d994a 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -100,6 +100,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("ip6", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("ipc", completeIpc(dockerCli)) _ = cmd.RegisterFlagCompletionFunc("isolation", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("kernel-memory", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) From 846439cdc47a1dcfaddd83852e6e4fbe62a49624 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 11:14:11 +0000 Subject: [PATCH 22/59] Disable file completion for `--label` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index df17e49d994a..d27d1880453c 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -101,6 +101,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("ipc", completeIpc(dockerCli)) _ = cmd.RegisterFlagCompletionFunc("isolation", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("kernel-memory", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("label", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) From 0e4b312b90a6de8aa854629474ff87741ebc4f44 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 11:31:25 +0000 Subject: [PATCH 23/59] Add completion for `--link` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index d27d1880453c..861076f985cc 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -102,6 +102,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("isolation", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("kernel-memory", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("label", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("link", completeLink(dockerCli)) _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) @@ -125,6 +126,23 @@ func completeIpc(cli command.Cli) func(cmd *cobra.Command, args []string, toComp } } +// completeLink implements shell completion for the `--link` option of `run` and `create`. +func completeLink(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) { + return postfixWith(":", containerNames(cli, cmd, args, toComplete)), cobra.ShellCompDirectiveNoSpace + } +} + +// 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 { + names, _ := completion.ContainerNames(dockerCLI, true)(cmd, args, toComplete) + if names == nil { + return []string{} + } + return names +} + // prefixWith prefixes every element in the slice with the given prefix. func prefixWith(prefix string, values []string) []string { result := make([]string, len(values)) @@ -134,6 +152,15 @@ func prefixWith(prefix string, values []string) []string { return result } +// postfixWith appends postfix to every element in the slice. +func postfixWith(postfix string, values []string) []string { + result := make([]string, len(values)) + for i, v := range values { + result[i] = v + postfix + } + return result +} + func completeLinuxCapabilityNames(cmd *cobra.Command, args []string, toComplete string) (names []string, _ cobra.ShellCompDirective) { return completion.FromList(allLinuxCapabilities()...)(cmd, args, toComplete) } From 93af729f5341c7c530392062e6d378eeb4b9f9dd Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 11:36:15 +0000 Subject: [PATCH 24/59] Disable file completion for `--link-local-ip` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 861076f985cc..c75bf1d20b2d 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -103,6 +103,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("kernel-memory", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("label", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("link", completeLink(dockerCli)) + _ = cmd.RegisterFlagCompletionFunc("link-local-ip", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) From 6a801bcd408258e5c283777663d251e73aef3c6d Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 12:28:07 +0000 Subject: [PATCH 25/59] Disable file completion for `--log-*` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index c75bf1d20b2d..4f877adc33d3 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -104,6 +104,8 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("label", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("link", completeLink(dockerCli)) _ = cmd.RegisterFlagCompletionFunc("link-local-ip", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("log-driver", completion.NoComplete) // TODO complete drivers + _ = cmd.RegisterFlagCompletionFunc("log-opt", completion.NoComplete) // TODO complete driver options _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) From e27ff3b20225e1c71e2aca72c02793ebd9c515f4 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 12:30:07 +0000 Subject: [PATCH 26/59] Disable file completion for `--mac-address` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 4f877adc33d3..81e3a1c65959 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -106,6 +106,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("link-local-ip", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("log-driver", completion.NoComplete) // TODO complete drivers _ = cmd.RegisterFlagCompletionFunc("log-opt", completion.NoComplete) // TODO complete driver options + _ = cmd.RegisterFlagCompletionFunc("mac-address", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) From 0135d7ed2bcfaf323d08603ebbe6d79a9e71d908 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 12:32:46 +0000 Subject: [PATCH 27/59] Disable file completion for `--memory*` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 81e3a1c65959..0b580a54e0f7 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -107,6 +107,10 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("log-driver", completion.NoComplete) // TODO complete drivers _ = cmd.RegisterFlagCompletionFunc("log-opt", completion.NoComplete) // TODO complete driver options _ = cmd.RegisterFlagCompletionFunc("mac-address", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("memory", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("memory-reservation", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("memory-swap", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("memory-swappiness", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) From 31f1f43b1b202e3d37ae0c2baf5aa87bebc38351 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 12:33:37 +0000 Subject: [PATCH 28/59] Disable file completion for `--mount` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 0b580a54e0f7..1010fd5f3384 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -111,6 +111,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("memory-reservation", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("memory-swap", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("memory-swappiness", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("mount", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) From b3703fa10922c1d1674d18f9ed3b9ea467f04f5a Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 13:01:11 +0000 Subject: [PATCH 29/59] Disable file completion for `--name` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 1010fd5f3384..31b952a98da6 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -112,6 +112,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("memory-swap", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("memory-swappiness", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("mount", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("name", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) From 39cd60b6eb4ebc2965bf2030e07aecf6602798b2 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 13:02:11 +0000 Subject: [PATCH 30/59] Disable file completion for `--network-alias` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 31b952a98da6..b05bcadd361e 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -114,6 +114,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("mount", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("name", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) + _ = cmd.RegisterFlagCompletionFunc("network-alias", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) _ = cmd.RegisterFlagCompletionFunc("restart", completeRestartPolicies) From 9817e322d996ed1a8bbd016c178ee424928198b6 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 13:04:19 +0000 Subject: [PATCH 31/59] Disable file completion for `--oom-score-adj` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index b05bcadd361e..7494213f1f57 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -115,6 +115,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("name", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) _ = cmd.RegisterFlagCompletionFunc("network-alias", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("oom-score-adj", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) _ = cmd.RegisterFlagCompletionFunc("restart", completeRestartPolicies) From 5c963884f5d6f97a633e277dee0a9100c5deaf29 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 13:12:51 +0000 Subject: [PATCH 32/59] Add completion for `--pid` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 15 ++++++++ cli/command/container/completion_test.go | 46 ++++++++++++++++++++++++ cli/command/container/opts_test.go | 2 +- 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 7494213f1f57..43e189a66dac 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -116,6 +116,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) _ = cmd.RegisterFlagCompletionFunc("network-alias", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("oom-score-adj", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("pid", completePid(dockerCli)) _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) _ = cmd.RegisterFlagCompletionFunc("restart", completeRestartPolicies) @@ -145,6 +146,20 @@ func completeLink(cli command.Cli) func(cmd *cobra.Command, args []string, toCom } } +// completePid implements shell completion for the `--pid` option of `run` and `create`. +func completePid(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"}, 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 { diff --git a/cli/command/container/completion_test.go b/cli/command/container/completion_test.go index b85aae32d5dc..4315dd1c7e98 100644 --- a/cli/command/container/completion_test.go +++ b/cli/command/container/completion_test.go @@ -1,9 +1,13 @@ package container import ( + "fmt" "strings" "testing" + "github.com/docker/cli/internal/test" + "github.com/docker/cli/internal/test/builders" + "github.com/docker/docker/api/types/container" "github.com/moby/sys/signal" "github.com/spf13/cobra" "gotest.tools/v3/assert" @@ -21,6 +25,48 @@ func TestCompleteLinuxCapabilityNames(t *testing.T) { } } +func TestCompletePid(t *testing.T) { + tests := []struct { + containerListFunc func(container.ListOptions) ([]container.Summary, error) + toComplete string + expectedCompletions []string + expectedDirective cobra.ShellCompDirective + }{ + { + toComplete: "", + expectedCompletions: []string{"container:", "host"}, + expectedDirective: cobra.ShellCompDirectiveNoFileComp, + }, + { + toComplete: "c", + expectedCompletions: []string{"container:"}, + expectedDirective: cobra.ShellCompDirectiveNoSpace, + }, + { + containerListFunc: func(container.ListOptions) ([]container.Summary, error) { + return []container.Summary{ + *builders.Container("c1"), + *builders.Container("c2"), + }, nil + }, + toComplete: "container:", + expectedCompletions: []string{"container:c1", "container:c2"}, + expectedDirective: cobra.ShellCompDirectiveNoFileComp, + }, + } + + for _, tc := range tests { + cli := test.NewFakeCli(&fakeClient{ + containerListFunc: tc.containerListFunc, + }) + + completions, directive := completePid(cli)(NewRunCommand(cli), nil, tc.toComplete) + + assert.DeepEqual(t, completions, tc.expectedCompletions) + assert.Equal(t, directive, tc.expectedDirective, fmt.Sprintf("wrong directive in completion for '%s'", tc.toComplete)) + } +} + func TestCompleteRestartPolicies(t *testing.T) { values, directives := completeRestartPolicies(nil, nil, "") assert.Check(t, is.Equal(directives&cobra.ShellCompDirectiveNoFileComp, cobra.ShellCompDirectiveNoFileComp), "Should not perform file completion") diff --git a/cli/command/container/opts_test.go b/cli/command/container/opts_test.go index e0ff746bc5db..9a4f3ce04b0d 100644 --- a/cli/command/container/opts_test.go +++ b/cli/command/container/opts_test.go @@ -79,7 +79,7 @@ func TestParseRunLinks(t *testing.T) { t.Fatalf("Error parsing links. Expected []string{\"a:b\", \"c:d\"}, received: %v", hostConfig.Links) } if _, hostConfig, _ := mustParse(t, ""); len(hostConfig.Links) != 0 { - t.Fatalf("Error parsing links. No link expected, received: %v", hostConfig.Links) + t.Fatalf("Error parsing links. No link expectedCompletions, received: %v", hostConfig.Links) } } From 968d7589883a6ed66b46eb9ad8e05589e309bcd9 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 13:16:41 +0000 Subject: [PATCH 33/59] Disable file completion for `--pids-limit` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 43e189a66dac..1f69db1ee4bf 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -117,6 +117,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("network-alias", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("oom-score-adj", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("pid", completePid(dockerCli)) + _ = cmd.RegisterFlagCompletionFunc("pids-limit", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) _ = cmd.RegisterFlagCompletionFunc("restart", completeRestartPolicies) From 39028ced904205281e166b3a7488ecdc7783664d Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 13:20:07 +0000 Subject: [PATCH 34/59] Disable file completion for `--publish` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 1f69db1ee4bf..9c59c3fe5905 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -119,6 +119,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("pid", completePid(dockerCli)) _ = cmd.RegisterFlagCompletionFunc("pids-limit", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) + _ = cmd.RegisterFlagCompletionFunc("publish", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) _ = cmd.RegisterFlagCompletionFunc("restart", completeRestartPolicies) _ = cmd.RegisterFlagCompletionFunc("stop-signal", completeSignals) From a40ed99d5ad25ee68f7fc41762d7c8672a51251f Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 13:29:27 +0000 Subject: [PATCH 35/59] Disable file completion for `--runtime` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 9c59c3fe5905..20285212decc 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -122,6 +122,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("publish", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) _ = cmd.RegisterFlagCompletionFunc("restart", completeRestartPolicies) + _ = cmd.RegisterFlagCompletionFunc("runtime", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("stop-signal", completeSignals) _ = cmd.RegisterFlagCompletionFunc("volumes-from", completion.ContainerNames(dockerCli, true)) } From ff9c6846b10ab7a169af9e5a876a5437d8692e3f Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 13:34:38 +0000 Subject: [PATCH 36/59] Disable file completion for `--security-opt` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 20285212decc..3b3f07739af7 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -123,6 +123,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) _ = cmd.RegisterFlagCompletionFunc("restart", completeRestartPolicies) _ = cmd.RegisterFlagCompletionFunc("runtime", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("security-opt", completion.NoComplete) // TODO refine _ = cmd.RegisterFlagCompletionFunc("stop-signal", completeSignals) _ = cmd.RegisterFlagCompletionFunc("volumes-from", completion.ContainerNames(dockerCli, true)) } From 396d9e02cfeb4ea1b525ec2dd41888cffe447eda Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 13:35:52 +0000 Subject: [PATCH 37/59] Disable file completion for `--shm-size` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 3b3f07739af7..0daf6c4193bc 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -124,6 +124,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("restart", completeRestartPolicies) _ = cmd.RegisterFlagCompletionFunc("runtime", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("security-opt", completion.NoComplete) // TODO refine + _ = cmd.RegisterFlagCompletionFunc("shm-size", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("stop-signal", completeSignals) _ = cmd.RegisterFlagCompletionFunc("volumes-from", completion.ContainerNames(dockerCli, true)) } From 09701feb71a78a6195a762e6798b8aff9f89e94c Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 13:39:12 +0000 Subject: [PATCH 38/59] Disable file completion for `--stop-timeout` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 0daf6c4193bc..eb48b52e7b44 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -126,6 +126,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("security-opt", completion.NoComplete) // TODO refine _ = cmd.RegisterFlagCompletionFunc("shm-size", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("stop-signal", completeSignals) + _ = cmd.RegisterFlagCompletionFunc("stop-timeout", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("volumes-from", completion.ContainerNames(dockerCli, true)) } From a490538d0d6b20d01e2da8e17da3ab96f48b99e2 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 13:45:23 +0000 Subject: [PATCH 39/59] Add completion for `--storage-opt` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index eb48b52e7b44..69a4dc08f29f 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -127,6 +127,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("shm-size", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("stop-signal", completeSignals) _ = cmd.RegisterFlagCompletionFunc("stop-timeout", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("storage-opt", completeStorageOpt) _ = cmd.RegisterFlagCompletionFunc("volumes-from", completion.ContainerNames(dockerCli, true)) } @@ -166,6 +167,11 @@ func completePid(cli command.Cli) func(cmd *cobra.Command, args []string, toComp } } +// completeStorageOpt implements shell completion for the `--storage-opt` option of `run` and `create`. +func completeStorageOpt(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { + return []string{"size="}, cobra.ShellCompDirectiveNoSpace +} + // 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 { From 0d65aca68e21760c89b131d4fca9c658ba964506 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 13:46:52 +0000 Subject: [PATCH 40/59] Disable file completion for `--sysctl` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 69a4dc08f29f..7e95c01169e1 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -128,6 +128,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("stop-signal", completeSignals) _ = cmd.RegisterFlagCompletionFunc("stop-timeout", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("storage-opt", completeStorageOpt) + _ = cmd.RegisterFlagCompletionFunc("sysctl", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("volumes-from", completion.ContainerNames(dockerCli, true)) } From fd1d55a2b4fb095c17d266db8be8fe422624db36 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 14:04:15 +0000 Subject: [PATCH 41/59] Add completion for `--ulimit` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 7e95c01169e1..a760491794d4 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -129,6 +129,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("stop-timeout", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("storage-opt", completeStorageOpt) _ = cmd.RegisterFlagCompletionFunc("sysctl", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("ulimit", completeUlimit) _ = cmd.RegisterFlagCompletionFunc("volumes-from", completion.ContainerNames(dockerCli, true)) } @@ -173,6 +174,32 @@ func completeStorageOpt(_ *cobra.Command, _ []string, _ string) ([]string, cobra return []string{"size="}, cobra.ShellCompDirectiveNoSpace } +// completeUlimit implements shell completion for the `--ulimit` option of `run` and `create`. +func completeUlimit(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { + limits := []string{ + "as", + "chroot", + "core", + "cpu", + "data", + "fsize", + "locks", + "maxlogins", + "maxsyslogins", + "memlock", + "msgqueue", + "nice", + "nofile", + "nproc", + "priority", + "rss", + "rtprio", + "sigpending", + "stack", + } + return postfixWith("=", limits), cobra.ShellCompDirectiveNoSpace +} + // 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 { From 20538ac1c76254743d68ae8bb4bfea07513c83ea Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 14:08:50 +0000 Subject: [PATCH 42/59] Disable file completion for `--user` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index a760491794d4..cc2cd5eed3cd 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -130,6 +130,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("storage-opt", completeStorageOpt) _ = cmd.RegisterFlagCompletionFunc("sysctl", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("ulimit", completeUlimit) + _ = cmd.RegisterFlagCompletionFunc("user", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("volumes-from", completion.ContainerNames(dockerCli, true)) } From b878f5e0574cd8636cd4221870efab4a299a4a5d Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 14:09:32 +0000 Subject: [PATCH 43/59] Add completion for `--userns` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index cc2cd5eed3cd..dfb1ef031f3f 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -131,6 +131,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("sysctl", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("ulimit", completeUlimit) _ = cmd.RegisterFlagCompletionFunc("user", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("userns", completion.FromList("host")) _ = cmd.RegisterFlagCompletionFunc("volumes-from", completion.ContainerNames(dockerCli, true)) } From 46db7413e2cbc57cbdedb129c50bfe75b2bf8a90 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 14:11:20 +0000 Subject: [PATCH 44/59] Disable file completion for `--uts` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index dfb1ef031f3f..8c08ab164381 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -132,6 +132,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("ulimit", completeUlimit) _ = cmd.RegisterFlagCompletionFunc("user", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("userns", completion.FromList("host")) + _ = cmd.RegisterFlagCompletionFunc("uts", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("volumes-from", completion.ContainerNames(dockerCli, true)) } From 8afd8dd21a5e362cf9157bf054c12238f3a37d2f Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 14:15:29 +0000 Subject: [PATCH 45/59] Add completion for `--volume-driver` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 8c08ab164381..8cdd3f9b5dd4 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -133,6 +133,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("user", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("userns", completion.FromList("host")) _ = cmd.RegisterFlagCompletionFunc("uts", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("volume-driver", completion.FromList("local")) _ = cmd.RegisterFlagCompletionFunc("volumes-from", completion.ContainerNames(dockerCli, true)) } From 61e803c2aa94c88a875092cf8e8f70860364fdde Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 14:16:46 +0000 Subject: [PATCH 46/59] Disable file completion for `--workdir` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 8cdd3f9b5dd4..c0cc0fb19e2d 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -135,6 +135,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("uts", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("volume-driver", completion.FromList("local")) _ = cmd.RegisterFlagCompletionFunc("volumes-from", completion.ContainerNames(dockerCli, true)) + _ = cmd.RegisterFlagCompletionFunc("workdir", completion.NoComplete) } // completeIpc implements shell completion for the `--ipc` option of `run` and `create`. From fc12d091fadc5f0e7381489943987c8b625b5fe5 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 20:14:49 +0000 Subject: [PATCH 47/59] Add completion for `--detach-keys` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 5 +++++ cli/command/container/run.go | 1 + 2 files changed, 6 insertions(+) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index c0cc0fb19e2d..d8c5802ddad5 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -138,6 +138,11 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("workdir", completion.NoComplete) } +// completeDetachKeys implements shell completion for the `--detach-keys` option of `run` and `create`. +func completeDetachKeys(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { + return []string{"ctrl-"}, cobra.ShellCompDirectiveNoSpace +} + // 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) { diff --git a/cli/command/container/run.go b/cli/command/container/run.go index ad8afcab8200..1077a9994a27 100644 --- a/cli/command/container/run.go +++ b/cli/command/container/run.go @@ -69,6 +69,7 @@ func NewRunCommand(dockerCli command.Cli) *cobra.Command { command.AddTrustVerificationFlags(flags, &options.untrusted, dockerCli.ContentTrustEnabled()) copts = addFlags(flags) + _ = cmd.RegisterFlagCompletionFunc("detach-keys", completeDetachKeys) addCompletions(cmd, dockerCli) return cmd From eca980c6d72285c9bf910c908f36ee62254106a2 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Thu, 24 Oct 2024 21:41:05 +0000 Subject: [PATCH 48/59] Improve completion for `--security-opt` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 28 ++++++++++++- cli/command/container/completion_test.go | 51 ++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index d8c5802ddad5..91cc1d1efb1e 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -123,7 +123,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) _ = cmd.RegisterFlagCompletionFunc("restart", completeRestartPolicies) _ = cmd.RegisterFlagCompletionFunc("runtime", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("security-opt", completion.NoComplete) // TODO refine + _ = cmd.RegisterFlagCompletionFunc("security-opt", completeSecurityOpt) _ = cmd.RegisterFlagCompletionFunc("shm-size", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("stop-signal", completeSignals) _ = cmd.RegisterFlagCompletionFunc("stop-timeout", completion.NoComplete) @@ -179,6 +179,32 @@ func completePid(cli command.Cli) func(cmd *cobra.Command, args []string, toComp } } +// completeSecurityOpt implements shell completion for the `--security-opt` option of `run` and `create`. +// The completion is partly composite. +func completeSecurityOpt(_ *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(toComplete) > 0 && strings.HasPrefix("apparmor=", toComplete) { //nolint:gocritic + return []string{"apparmor="}, cobra.ShellCompDirectiveNoSpace + } + if len(toComplete) > 0 && strings.HasPrefix("label", toComplete) { //nolint:gocritic + return []string{"label="}, cobra.ShellCompDirectiveNoSpace + } + if strings.HasPrefix(toComplete, "label=") { + if strings.HasPrefix(toComplete, "label=d") { + return []string{"label=disable"}, cobra.ShellCompDirectiveNoFileComp + } + labels := []string{"disable", "level:", "role:", "type:", "user:"} + return prefixWith("label=", labels), cobra.ShellCompDirectiveNoSpace | cobra.ShellCompDirectiveNoFileComp + } + // length must be > 1 here so that completion of "s" falls through. + if len(toComplete) > 1 && strings.HasPrefix("seccomp", toComplete) { //nolint:gocritic + return []string{"seccomp="}, cobra.ShellCompDirectiveNoSpace + } + if strings.HasPrefix(toComplete, "seccomp=") { + return []string{"seccomp=unconfined"}, cobra.ShellCompDirectiveNoFileComp + } + return []string{"apparmor=", "label=", "no-new-privileges", "seccomp=", "systempaths=unconfined"}, cobra.ShellCompDirectiveNoFileComp +} + // completeStorageOpt implements shell completion for the `--storage-opt` option of `run` and `create`. func completeStorageOpt(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { return []string{"size="}, cobra.ShellCompDirectiveNoSpace diff --git a/cli/command/container/completion_test.go b/cli/command/container/completion_test.go index 4315dd1c7e98..dce3237092c5 100644 --- a/cli/command/container/completion_test.go +++ b/cli/command/container/completion_test.go @@ -74,6 +74,57 @@ func TestCompleteRestartPolicies(t *testing.T) { assert.Check(t, is.DeepEqual(values, expected)) } +func TestCompleteSecurityOpts(t *testing.T) { + tests := []struct { + toComplete string + expectedCompletions []string + expectedDirective cobra.ShellCompDirective + }{ + { + toComplete: "", + expectedCompletions: []string{"apparmor=", "label=", "no-new-privileges", "seccomp=", "systempaths=unconfined"}, + expectedDirective: cobra.ShellCompDirectiveNoFileComp, + }, + { + toComplete: "apparmor=", + expectedCompletions: []string{"apparmor="}, + expectedDirective: cobra.ShellCompDirectiveNoSpace, + }, + { + toComplete: "label=", + expectedCompletions: []string{"label=disable", "label=level:", "label=role:", "label=type:", "label=user:"}, + expectedDirective: cobra.ShellCompDirectiveNoSpace | cobra.ShellCompDirectiveNoFileComp, + }, + { + toComplete: "s", + // We do not filter matching completions but delegate this task to the shell script. + expectedCompletions: []string{"apparmor=", "label=", "no-new-privileges", "seccomp=", "systempaths=unconfined"}, + expectedDirective: cobra.ShellCompDirectiveNoFileComp, + }, + { + toComplete: "se", + expectedCompletions: []string{"seccomp="}, + expectedDirective: cobra.ShellCompDirectiveNoSpace, + }, + { + toComplete: "seccomp=", + expectedCompletions: []string{"seccomp=unconfined"}, + expectedDirective: cobra.ShellCompDirectiveNoFileComp, + }, + { + toComplete: "sy", + expectedCompletions: []string{"apparmor=", "label=", "no-new-privileges", "seccomp=", "systempaths=unconfined"}, + expectedDirective: cobra.ShellCompDirectiveNoFileComp, + }, + } + + for _, tc := range tests { + completions, directive := completeSecurityOpt(nil, nil, tc.toComplete) + assert.DeepEqual(t, completions, tc.expectedCompletions) + assert.Equal(t, directive, tc.expectedDirective, fmt.Sprintf("wrong directive in completion for '%s'", tc.toComplete)) + } +} + func TestCompleteSignals(t *testing.T) { values, directives := completeSignals(nil, nil, "") assert.Check(t, is.Equal(directives&cobra.ShellCompDirectiveNoFileComp, cobra.ShellCompDirectiveNoFileComp), "Should not perform file completion") From bdae758447a2178fbeff86e7826bc40033de7c4f Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Sat, 26 Oct 2024 19:46:23 +0000 Subject: [PATCH 49/59] Improve completion for `--log-driver` and `--log-opt` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 74 ++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 91cc1d1efb1e..e3c0ae970824 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -45,6 +45,57 @@ var allLinuxCapabilities = sync.OnceValue(func() []string { return out }) +// logDriverOptions provides the options for each built-in logging driver. +var logDriverOptions = map[string][]string{ + "awslogs": { + "max-buffer-size", "mode", "awslogs-create-group", "awslogs-credentials-endpoint", "awslogs-datetime-format", + "awslogs-group", "awslogs-multiline-pattern", "awslogs-region", "awslogs-stream", "tag", + }, + "etwlogs": {}, + "fluentd": { + "max-buffer-size", "mode", "env", "env-regex", "labels", "fluentd-address", "fluentd-async", + "fluentd-buffer-limit", "fluentd-request-ack", "fluentd-retry-wait", "fluentd-max-retries", + "fluentd-sub-second-precision", "tag", + }, + "gcplogs": { + "max-buffer-size", "mode", "env", "env-regex", "labels", "gcp-log-cmd", "gcp-meta-id", "gcp-meta-name", + "gcp-meta-zone", "gcp-project", + }, + "gelf": { + "max-buffer-size", "mode", "env", "env-regex", "labels", "gelf-address", "gelf-compression-level", + "gelf-compression-type", "gelf-tcp-max-reconnect", "gelf-tcp-reconnect-delay", "tag", + }, + "journald": {"max-buffer-size", "mode", "env", "env-regex", "labels", "tag"}, + "json-file": {"max-buffer-size", "mode", "env", "env-regex", "labels", "compress", "max-file", "max-size"}, + "local": {"max-buffer-size", "mode", "compress", "max-file", "max-size"}, + "none": {}, + "splunk": { + "max-buffer-size", "mode", "env", "env-regex", "labels", "splunk-caname", "splunk-capath", "splunk-format", + "splunk-gzip", "splunk-gzip-level", "splunk-index", "splunk-insecureskipverify", "splunk-source", + "splunk-sourcetype", "splunk-token", "splunk-url", "splunk-verify-connection", "tag", + }, + "syslog": { + "max-buffer-size", "mode", "env", "env-regex", "labels", "syslog-address", "syslog-facility", "syslog-format", + "syslog-tls-ca-cert", "syslog-tls-cert", "syslog-tls-key", "syslog-tls-skip-verify", "tag", + }, +} + +// allLogDriverOptions provides all options of the built-in logging drivers. +// The list does not contain duplicates. +var allLogDriverOptions = sync.OnceValue(func() []string { + var result []string + seen := make(map[string]bool) + for driver := range logDriverOptions { + for _, opt := range logDriverOptions[driver] { + if !seen[opt] { + seen[opt] = true + result = append(result, opt) + } + } + } + return result +}) + // restartPolicies is a list of all valid restart-policies.. // // TODO(thaJeztah): add descriptions, and enable descriptions for our completion scripts (cobra.CompletionOptions.DisableDescriptions is currently set to "true") @@ -104,8 +155,8 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("label", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("link", completeLink(dockerCli)) _ = cmd.RegisterFlagCompletionFunc("link-local-ip", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("log-driver", completion.NoComplete) // TODO complete drivers - _ = cmd.RegisterFlagCompletionFunc("log-opt", completion.NoComplete) // TODO complete driver options + _ = cmd.RegisterFlagCompletionFunc("log-driver", completeLogDriver) + _ = cmd.RegisterFlagCompletionFunc("log-opt", completeLogOpt) _ = cmd.RegisterFlagCompletionFunc("mac-address", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("memory", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("memory-reservation", completion.NoComplete) @@ -165,6 +216,25 @@ func completeLink(cli command.Cli) func(cmd *cobra.Command, args []string, toCom } } +// completeLogDriver implements shell completion for the `--log-driver` option of `run` and `create`. +func completeLogDriver(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { + drivers := make([]string, 0, len(logDriverOptions)) + for driver := range logDriverOptions { + drivers = append(drivers, driver) + } + return drivers, cobra.ShellCompDirectiveNoFileComp +} + +// completeLogOpt implements shell completion for the `--log-opt` option of `run` and `create`. +// If the user supplied a log-driver, only options for that driver are returned. +func completeLogOpt(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { + driver, _ := cmd.Flags().GetString("log-driver") + if options, exists := logDriverOptions[driver]; exists { + return postfixWith("=", options), cobra.ShellCompDirectiveNoSpace | cobra.ShellCompDirectiveNoFileComp + } + return postfixWith("=", allLogDriverOptions()), cobra.ShellCompDirectiveNoSpace +} + // completePid implements shell completion for the `--pid` option of `run` and `create`. func completePid(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) { From 32d7ff88831cf1044caaed2f658159bb76f71264 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Wed, 30 Oct 2024 16:38:38 +0100 Subject: [PATCH 50/59] Fix function signatures (dockerCLI completion.APIClientProvider) Signed-off-by: Harald Albers --- cli/command/container/completion.go | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index e3c0ae970824..cc2607c19b51 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -4,7 +4,6 @@ import ( "strings" "sync" - "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command/completion" "github.com/docker/docker/api/types/container" "github.com/moby/sys/capability" @@ -107,7 +106,7 @@ var restartPolicies = []string{ } // addCompletions adds the completions that `run` and `create` have in common. -func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { +func addCompletions(cmd *cobra.Command, dockerCLI completion.APIClientProvider) { _ = cmd.RegisterFlagCompletionFunc("add-host", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("annotation", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("attach", completion.FromList("stderr", "stdin", "stdout")) @@ -149,11 +148,11 @@ 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("ipc", completeIpc(dockerCLI)) _ = cmd.RegisterFlagCompletionFunc("isolation", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("kernel-memory", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("label", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("link", completeLink(dockerCli)) + _ = cmd.RegisterFlagCompletionFunc("link", completeLink(dockerCLI)) _ = cmd.RegisterFlagCompletionFunc("link-local-ip", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("log-driver", completeLogDriver) _ = cmd.RegisterFlagCompletionFunc("log-opt", completeLogOpt) @@ -164,10 +163,10 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("memory-swappiness", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("mount", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("name", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) + _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCLI)) _ = cmd.RegisterFlagCompletionFunc("network-alias", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("oom-score-adj", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("pid", completePid(dockerCli)) + _ = cmd.RegisterFlagCompletionFunc("pid", completePid(dockerCLI)) _ = cmd.RegisterFlagCompletionFunc("pids-limit", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) _ = cmd.RegisterFlagCompletionFunc("publish", completion.NoComplete) @@ -185,7 +184,7 @@ func addCompletions(cmd *cobra.Command, dockerCli command.Cli) { _ = cmd.RegisterFlagCompletionFunc("userns", completion.FromList("host")) _ = cmd.RegisterFlagCompletionFunc("uts", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("volume-driver", completion.FromList("local")) - _ = cmd.RegisterFlagCompletionFunc("volumes-from", completion.ContainerNames(dockerCli, true)) + _ = cmd.RegisterFlagCompletionFunc("volumes-from", completion.ContainerNames(dockerCLI, true)) _ = cmd.RegisterFlagCompletionFunc("workdir", completion.NoComplete) } @@ -196,13 +195,13 @@ func completeDetachKeys(_ *cobra.Command, _ []string, _ string) ([]string, cobra // 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) { +func completeIpc(dockerCLI completion.APIClientProvider) 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) + names, _ := completion.ContainerNames(dockerCLI, true)(cmd, args, toComplete) return prefixWith("container:", names), cobra.ShellCompDirectiveNoFileComp } return []string{"container:", "host", "none", "private", "shareable"}, cobra.ShellCompDirectiveNoFileComp @@ -210,9 +209,9 @@ func completeIpc(cli command.Cli) func(cmd *cobra.Command, args []string, toComp } // completeLink implements shell completion for the `--link` option of `run` and `create`. -func completeLink(cli command.Cli) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { +func completeLink(dockerCLI completion.APIClientProvider) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return postfixWith(":", containerNames(cli, cmd, args, toComplete)), cobra.ShellCompDirectiveNoSpace + return postfixWith(":", containerNames(dockerCLI, cmd, args, toComplete)), cobra.ShellCompDirectiveNoSpace } } @@ -236,13 +235,13 @@ func completeLogOpt(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.S } // completePid implements shell completion for the `--pid` option of `run` and `create`. -func completePid(cli command.Cli) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { +func completePid(dockerCLI completion.APIClientProvider) 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) + names, _ := completion.ContainerNames(dockerCLI, true)(cmd, args, toComplete) return prefixWith("container:", names), cobra.ShellCompDirectiveNoFileComp } return []string{"container:", "host"}, cobra.ShellCompDirectiveNoFileComp From afda9f3b970683136b8dc574a072f24cbc26c847 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Wed, 30 Oct 2024 14:55:19 +0000 Subject: [PATCH 51/59] Improve completion for "--uts" Signed-off-by: Harald Albers --- cli/command/container/completion.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index cc2607c19b51..9103c05c8ba4 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -182,7 +182,7 @@ func addCompletions(cmd *cobra.Command, dockerCLI completion.APIClientProvider) _ = cmd.RegisterFlagCompletionFunc("ulimit", completeUlimit) _ = cmd.RegisterFlagCompletionFunc("user", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("userns", completion.FromList("host")) - _ = cmd.RegisterFlagCompletionFunc("uts", completion.NoComplete) + _ = cmd.RegisterFlagCompletionFunc("uts", completion.FromList("host")) _ = cmd.RegisterFlagCompletionFunc("volume-driver", completion.FromList("local")) _ = cmd.RegisterFlagCompletionFunc("volumes-from", completion.ContainerNames(dockerCLI, true)) _ = cmd.RegisterFlagCompletionFunc("workdir", completion.NoComplete) From 5624d2cbfda596545f5398ac99d5bb931ab186dc Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Wed, 30 Oct 2024 15:19:39 +0000 Subject: [PATCH 52/59] Add comments to linter exceptions Signed-off-by: Harald Albers --- cli/command/container/completion.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 9103c05c8ba4..914de4e84d9b 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -197,7 +197,7 @@ func completeDetachKeys(_ *cobra.Command, _ []string, _ string) ([]string, cobra // The completion is partly composite. func completeIpc(dockerCLI completion.APIClientProvider) 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 + if len(toComplete) > 0 && strings.HasPrefix("container", toComplete) { //nolint:gocritic // not swapped, matches partly typed "container" return []string{"container:"}, cobra.ShellCompDirectiveNoSpace } if strings.HasPrefix(toComplete, "container:") { @@ -237,7 +237,7 @@ func completeLogOpt(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.S // completePid implements shell completion for the `--pid` option of `run` and `create`. func completePid(dockerCLI completion.APIClientProvider) 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 + if len(toComplete) > 0 && strings.HasPrefix("container", toComplete) { //nolint:gocritic // not swapped, matches partly typed "container" return []string{"container:"}, cobra.ShellCompDirectiveNoSpace } if strings.HasPrefix(toComplete, "container:") { @@ -251,10 +251,10 @@ func completePid(dockerCLI completion.APIClientProvider) func(cmd *cobra.Command // completeSecurityOpt implements shell completion for the `--security-opt` option of `run` and `create`. // The completion is partly composite. func completeSecurityOpt(_ *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) { - if len(toComplete) > 0 && strings.HasPrefix("apparmor=", toComplete) { //nolint:gocritic + if len(toComplete) > 0 && strings.HasPrefix("apparmor=", toComplete) { //nolint:gocritic // not swapped, matches partly typed "apparmor=" return []string{"apparmor="}, cobra.ShellCompDirectiveNoSpace } - if len(toComplete) > 0 && strings.HasPrefix("label", toComplete) { //nolint:gocritic + if len(toComplete) > 0 && strings.HasPrefix("label", toComplete) { //nolint:gocritic // not swapped, matches partly typed "label" return []string{"label="}, cobra.ShellCompDirectiveNoSpace } if strings.HasPrefix(toComplete, "label=") { @@ -265,7 +265,7 @@ func completeSecurityOpt(_ *cobra.Command, _ []string, toComplete string) ([]str return prefixWith("label=", labels), cobra.ShellCompDirectiveNoSpace | cobra.ShellCompDirectiveNoFileComp } // length must be > 1 here so that completion of "s" falls through. - if len(toComplete) > 1 && strings.HasPrefix("seccomp", toComplete) { //nolint:gocritic + if len(toComplete) > 1 && strings.HasPrefix("seccomp", toComplete) { //nolint:gocritic // not swapped, matches partly typed "seccomp" return []string{"seccomp="}, cobra.ShellCompDirectiveNoSpace } if strings.HasPrefix(toComplete, "seccomp=") { From 8362a23b67ed54e963b7c562878ffd6139cb6de4 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Wed, 30 Oct 2024 15:55:19 +0000 Subject: [PATCH 53/59] Use constants in completion of `--cgroupns` Signed-off-by: Harald Albers --- cli/command/container/completion.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 914de4e84d9b..c4e093c210e9 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -115,7 +115,7 @@ func addCompletions(cmd *cobra.Command, dockerCLI completion.APIClientProvider) _ = cmd.RegisterFlagCompletionFunc("cap-add", completeLinuxCapabilityNames) _ = cmd.RegisterFlagCompletionFunc("cap-drop", completeLinuxCapabilityNames) _ = cmd.RegisterFlagCompletionFunc("cgroup-parent", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("cgroupns", completion.FromList("host", "private")) + _ = cmd.RegisterFlagCompletionFunc("cgroupns", completeCgroupns()) _ = cmd.RegisterFlagCompletionFunc("cpu-period", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("cpu-quota", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("cpu-rt-period", completion.NoComplete) @@ -188,6 +188,10 @@ func addCompletions(cmd *cobra.Command, dockerCLI completion.APIClientProvider) _ = cmd.RegisterFlagCompletionFunc("workdir", completion.NoComplete) } +func completeCgroupns() completion.ValidArgsFn { + return completion.FromList(string(container.CgroupnsModeHost), string(container.CgroupnsModePrivate)) +} + // completeDetachKeys implements shell completion for the `--detach-keys` option of `run` and `create`. func completeDetachKeys(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { return []string{"ctrl-"}, cobra.ShellCompDirectiveNoSpace From adcce2b4ef5358ab57473e076bd12ada012ae42e Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Wed, 30 Oct 2024 16:24:35 +0000 Subject: [PATCH 54/59] 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 c4e093c210e9..c1f5c1eccc34 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 { From a40ea785792094067000725afad13b6bd5e6d965 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Wed, 30 Oct 2024 17:08:33 +0000 Subject: [PATCH 55/59] Improve completion for "--log-driver" Signed-off-by: Harald Albers --- cli/command/container/completion.go | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index c1f5c1eccc34..94e9b5046129 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -50,7 +50,6 @@ var logDriverOptions = map[string][]string{ "max-buffer-size", "mode", "awslogs-create-group", "awslogs-credentials-endpoint", "awslogs-datetime-format", "awslogs-group", "awslogs-multiline-pattern", "awslogs-region", "awslogs-stream", "tag", }, - "etwlogs": {}, "fluentd": { "max-buffer-size", "mode", "env", "env-regex", "labels", "fluentd-address", "fluentd-async", "fluentd-buffer-limit", "fluentd-request-ack", "fluentd-retry-wait", "fluentd-max-retries", @@ -79,6 +78,15 @@ var logDriverOptions = map[string][]string{ }, } +// builtInLogDrivers provides a list of the built-in logging drivers. +var builtInLogDrivers = sync.OnceValue(func() []string { + drivers := make([]string, 0, len(logDriverOptions)) + for driver := range logDriverOptions { + drivers = append(drivers, driver) + } + return drivers +}) + // allLogDriverOptions provides all options of the built-in logging drivers. // The list does not contain duplicates. var allLogDriverOptions = sync.OnceValue(func() []string { @@ -154,7 +162,7 @@ func addCompletions(cmd *cobra.Command, dockerCLI completion.APIClientProvider) _ = cmd.RegisterFlagCompletionFunc("label", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("link", completeLink(dockerCLI)) _ = cmd.RegisterFlagCompletionFunc("link-local-ip", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("log-driver", completeLogDriver) + _ = cmd.RegisterFlagCompletionFunc("log-driver", completeLogDriver(dockerCLI)) _ = cmd.RegisterFlagCompletionFunc("log-opt", completeLogOpt) _ = cmd.RegisterFlagCompletionFunc("mac-address", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("memory", completion.NoComplete) @@ -220,12 +228,17 @@ func completeLink(dockerCLI completion.APIClientProvider) func(cmd *cobra.Comman } // completeLogDriver implements shell completion for the `--log-driver` option of `run` and `create`. -func completeLogDriver(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { - drivers := make([]string, 0, len(logDriverOptions)) - for driver := range logDriverOptions { - drivers = append(drivers, driver) +// The log drivers are collected from a call to the Info endpoint with a fallback to a hard-coded list +// of the build-in log drivers. +func completeLogDriver(dockerCLI completion.APIClientProvider) completion.ValidArgsFn { + return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + info, err := dockerCLI.Client().Info(cmd.Context()) + if err != nil { + return builtInLogDrivers(), cobra.ShellCompDirectiveNoFileComp + } + drivers := info.Plugins.Log + return drivers, cobra.ShellCompDirectiveNoFileComp } - return drivers, cobra.ShellCompDirectiveNoFileComp } // completeLogOpt implements shell completion for the `--log-opt` option of `run` and `create`. From 3bc177b8ac1c9eeb0ff3147af5b7754f00792e03 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Wed, 30 Oct 2024 17:18:10 +0000 Subject: [PATCH 56/59] Use constants in completion of IPCModes Signed-off-by: Harald Albers --- cli/command/container/completion.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 94e9b5046129..34456d40ee2a 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -216,7 +216,13 @@ func completeIpc(dockerCLI completion.APIClientProvider) func(cmd *cobra.Command names, _ := completion.ContainerNames(dockerCLI, true)(cmd, args, toComplete) return prefixWith("container:", names), cobra.ShellCompDirectiveNoFileComp } - return []string{"container:", "host", "none", "private", "shareable"}, cobra.ShellCompDirectiveNoFileComp + return []string{ + string(container.IPCModeContainer + ":"), + string(container.IPCModeHost), + string(container.IPCModeNone), + string(container.IPCModePrivate), + string(container.IPCModeShareable), + }, cobra.ShellCompDirectiveNoFileComp } } From 33d3246a37474fa9ac8c7ed9ff0120f16a2d5a0d Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Wed, 30 Oct 2024 19:15:41 +0000 Subject: [PATCH 57/59] fixup! Add completion for `--pid` Signed-off-by: Harald Albers --- cli/command/container/opts_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/command/container/opts_test.go b/cli/command/container/opts_test.go index 9a4f3ce04b0d..e0ff746bc5db 100644 --- a/cli/command/container/opts_test.go +++ b/cli/command/container/opts_test.go @@ -79,7 +79,7 @@ func TestParseRunLinks(t *testing.T) { t.Fatalf("Error parsing links. Expected []string{\"a:b\", \"c:d\"}, received: %v", hostConfig.Links) } if _, hostConfig, _ := mustParse(t, ""); len(hostConfig.Links) != 0 { - t.Fatalf("Error parsing links. No link expectedCompletions, received: %v", hostConfig.Links) + t.Fatalf("Error parsing links. No link expected, received: %v", hostConfig.Links) } } From ef6cca9699016004b53003c4dab2f52f2f534052 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Wed, 30 Oct 2024 19:40:55 +0000 Subject: [PATCH 58/59] Use subtests Signed-off-by: Harald Albers --- cli/command/container/completion_test.go | 25 +++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/cli/command/container/completion_test.go b/cli/command/container/completion_test.go index dce3237092c5..ca75a49b5fe6 100644 --- a/cli/command/container/completion_test.go +++ b/cli/command/container/completion_test.go @@ -1,7 +1,6 @@ package container import ( - "fmt" "strings" "testing" @@ -56,14 +55,15 @@ func TestCompletePid(t *testing.T) { } for _, tc := range tests { - cli := test.NewFakeCli(&fakeClient{ - containerListFunc: tc.containerListFunc, + tc := tc + t.Run(tc.toComplete, func(t *testing.T) { + cli := test.NewFakeCli(&fakeClient{ + containerListFunc: tc.containerListFunc, + }) + completions, directive := completePid(cli)(NewRunCommand(cli), nil, tc.toComplete) + assert.Check(t, is.DeepEqual(completions, tc.expectedCompletions)) + assert.Check(t, is.Equal(directive, tc.expectedDirective)) }) - - completions, directive := completePid(cli)(NewRunCommand(cli), nil, tc.toComplete) - - assert.DeepEqual(t, completions, tc.expectedCompletions) - assert.Equal(t, directive, tc.expectedDirective, fmt.Sprintf("wrong directive in completion for '%s'", tc.toComplete)) } } @@ -119,9 +119,12 @@ func TestCompleteSecurityOpts(t *testing.T) { } for _, tc := range tests { - completions, directive := completeSecurityOpt(nil, nil, tc.toComplete) - assert.DeepEqual(t, completions, tc.expectedCompletions) - assert.Equal(t, directive, tc.expectedDirective, fmt.Sprintf("wrong directive in completion for '%s'", tc.toComplete)) + tc := tc + t.Run(tc.toComplete, func(t *testing.T) { + completions, directive := completeSecurityOpt(nil, nil, tc.toComplete) + assert.Check(t, is.DeepEqual(completions, tc.expectedCompletions)) + assert.Check(t, is.Equal(directive, tc.expectedDirective)) + }) } } From 4378ab4d922e6b5c904775a63cf2f19b1c3d2e4f Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Wed, 30 Oct 2024 20:10:37 +0000 Subject: [PATCH 59/59] Handle null completions with a default callback Credits to thaJeztah Signed-off-by: Harald Albers --- cli/command/container/completion.go | 56 ----------------------------- cli/command/container/create.go | 7 ++++ cli/command/container/run.go | 7 ++++ 3 files changed, 14 insertions(+), 56 deletions(-) diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index 34456d40ee2a..5ff37eca8701 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -115,85 +115,29 @@ var restartPolicies = []string{ // addCompletions adds the completions that `run` and `create` have in common. func addCompletions(cmd *cobra.Command, dockerCLI completion.APIClientProvider) { - _ = cmd.RegisterFlagCompletionFunc("add-host", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("annotation", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("attach", completion.FromList("stderr", "stdin", "stdout")) - _ = cmd.RegisterFlagCompletionFunc("blkio-weight", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("blkio-weight-device", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("cap-add", completeLinuxCapabilityNames) _ = cmd.RegisterFlagCompletionFunc("cap-drop", completeLinuxCapabilityNames) - _ = cmd.RegisterFlagCompletionFunc("cgroup-parent", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("cgroupns", completeCgroupns()) - _ = cmd.RegisterFlagCompletionFunc("cpu-period", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("cpu-quota", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("cpu-rt-period", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("cpu-rt-runtime", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("cpu-shares", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("cpus", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("cpuset-cpus", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("cpuset-mems", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("device-cgroup-rule", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("device-read-bps", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("device-read-iops", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("device-write-bps", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("device-write-iops", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("dns", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("dns-option", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("dns-search", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("domainname", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("entrypoint", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("expose", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames) _ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames) - _ = cmd.RegisterFlagCompletionFunc("gpus", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("group-add", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("health-cmd", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("health-interval", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("health-retries", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("health-start-interval", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("health-start-period", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("health-timeout", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("hostname", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("ip", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("ip6", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("ipc", completeIpc(dockerCLI)) - _ = cmd.RegisterFlagCompletionFunc("isolation", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("kernel-memory", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("label", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("link", completeLink(dockerCLI)) - _ = cmd.RegisterFlagCompletionFunc("link-local-ip", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("log-driver", completeLogDriver(dockerCLI)) _ = cmd.RegisterFlagCompletionFunc("log-opt", completeLogOpt) - _ = cmd.RegisterFlagCompletionFunc("mac-address", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("memory", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("memory-reservation", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("memory-swap", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("memory-swappiness", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("mount", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("name", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCLI)) - _ = cmd.RegisterFlagCompletionFunc("network-alias", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("oom-score-adj", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("pid", completePid(dockerCLI)) - _ = cmd.RegisterFlagCompletionFunc("pids-limit", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) - _ = cmd.RegisterFlagCompletionFunc("publish", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever)) _ = cmd.RegisterFlagCompletionFunc("restart", completeRestartPolicies) - _ = cmd.RegisterFlagCompletionFunc("runtime", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("security-opt", completeSecurityOpt) - _ = cmd.RegisterFlagCompletionFunc("shm-size", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("stop-signal", completeSignals) - _ = cmd.RegisterFlagCompletionFunc("stop-timeout", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("storage-opt", completeStorageOpt) - _ = cmd.RegisterFlagCompletionFunc("sysctl", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("ulimit", completeUlimit) - _ = cmd.RegisterFlagCompletionFunc("user", completion.NoComplete) _ = 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)) - _ = cmd.RegisterFlagCompletionFunc("workdir", completion.NoComplete) } func completeCgroupns() completion.ValidArgsFn { diff --git a/cli/command/container/create.go b/cli/command/container/create.go index 00b7436a471a..9f73b7f0afe3 100644 --- a/cli/command/container/create.go +++ b/cli/command/container/create.go @@ -80,6 +80,13 @@ func NewCreateCommand(dockerCli command.Cli) *cobra.Command { addCompletions(cmd, dockerCli) + flags.VisitAll(func(flag *pflag.Flag) { + // Set a default completion function if none was set. We don't look + // up if it does already have one set, because Cobra does this for + // us, and returns an error (which we ignore for this reason). + _ = cmd.RegisterFlagCompletionFunc(flag.Name, completion.NoComplete) + }) + return cmd } diff --git a/cli/command/container/run.go b/cli/command/container/run.go index 1077a9994a27..feaf59dd786c 100644 --- a/cli/command/container/run.go +++ b/cli/command/container/run.go @@ -72,6 +72,13 @@ func NewRunCommand(dockerCli command.Cli) *cobra.Command { _ = cmd.RegisterFlagCompletionFunc("detach-keys", completeDetachKeys) addCompletions(cmd, dockerCli) + flags.VisitAll(func(flag *pflag.Flag) { + // Set a default completion function if none was set. We don't look + // up if it does already have one set, because Cobra does this for + // us, and returns an error (which we ignore for this reason). + _ = cmd.RegisterFlagCompletionFunc(flag.Name, completion.NoComplete) + }) + return cmd }