Browse code

daemon/command: remove `__dummy_command` workaround for completion

This workaround was added in 3bb40af036d4f2211a5c1c3f73b86bd2050b16a7 to
work around a limitation of Cobra, which didn't support completion if there
are no subcommands, because [Command.InitDefaultCompletionCmd] disabled
completion, as implicitly adding the hidden `completion` and `__complete`
subcommands would break binaries without subcommands that accepted positional
args.

This problem was fixed in Cobra v1.9.0 through [cobra@24ada7f], which now
dynamically adds the `completion` subcommand when invoked.

This patch removes the hidden `__dummy_command`, which was used to work around
the logic in Cobra < v1.9.0, and to make it enable completion regardless.

Completion should still work as usual:

dockerd completion --help

Usage: dockerd completion [flags]
...

source <(dockerd completion bash)
dockerd --default-<tab>
--default-address-pool (Default address pools for node specific local networks)
--default-cgroupns-mode (Default mode for containers cgroup namespace ("host" | "private"))
--default-gateway (Default gateway IPv4 address for the default bridge network)
--default-gateway-v6 (Default gateway IPv6 address for the default bridge network)
--default-ipc-mode (Default mode for containers ipc ("shareable" | "private"))
--default-network-opt (Default network options)
--default-runtime (Default OCI runtime for containers)
--default-shm-size (Default shm size for containers)
--default-ulimit (Default ulimits for containers)

[Command.InitDefaultCompletionCmd]: https://github.com/spf13/cobra/blob/v1.8.1/completions.go#L685-L698
[cobra@24ada7f]: https://github.com/spf13/cobra/commit/24ada7fe71e3a3a8741dd52e0a7fc3b97450535a

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2025/11/06 17:38:56
Showing 1 changed files
... ...
@@ -53,23 +53,6 @@ func newDaemonCommand(stderr io.Writer) (*cobra.Command, error) {
53 53
 		},
54 54
 	}
55 55
 
56
-	// Cobra's [Command.InitDefaultCompletionCmd] has a special-case for
57
-	// binaries/commands that don't have subcommands, and does not set up
58
-	// the default completion command in that case.
59
-	//
60
-	// Unfortunately, the definition of the default completion commands
61
-	// is not exported, and we don't want to replicate them. As a workaround,
62
-	// we're adding a hidden dummy-command to trick Cobra into applying
63
-	// the default.
64
-	//
65
-	// TODO(thaJeztah): consider contributing to Cobra to either allow explicitly enabling, or to export the default completion commands
66
-	//
67
-	// [Command.InitDefaultCompletionCmd]: https://github.com/spf13/cobra/blob/v1.8.1/completions.go#L685-L698
68
-	cmd.AddCommand(&cobra.Command{
69
-		Use:    "__dummy_command",
70
-		Hidden: true,
71
-	})
72
-
73 56
 	SetupRootCommand(cmd)
74 57
 
75 58
 	flags := cmd.Flags()