Handle null completions with a default callback

Credits to thaJeztah

Signed-off-by: Harald Albers <github@albersweb.de>
This commit is contained in:
Harald Albers 2024-10-30 20:10:37 +00:00
parent ef6cca9699
commit 4378ab4d92
3 changed files with 14 additions and 56 deletions

View File

@ -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 {

View File

@ -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
}

View File

@ -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
}