From 768d10767fe59670b75ab35897f4f7468b81cecc Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 20 Feb 2025 12:44:43 +0100 Subject: [PATCH] completion: add completion for docker node flags With this patch: docker node update --role manager worker docker node update --availability active drain pause Signed-off-by: Sebastiaan van Stijn --- cli/command/node/list.go | 7 +++++++ cli/command/node/ps.go | 8 ++++++++ cli/command/node/update.go | 10 ++++++++++ 3 files changed, 25 insertions(+) diff --git a/cli/command/node/list.go b/cli/command/node/list.go index f64b8174ff..51b3f4afe1 100644 --- a/cli/command/node/list.go +++ b/cli/command/node/list.go @@ -14,6 +14,7 @@ import ( "github.com/docker/docker/api/types/system" "github.com/fvbommel/sortorder" "github.com/spf13/cobra" + "github.com/spf13/pflag" ) type listOptions struct { @@ -40,6 +41,12 @@ func newListCommand(dockerCli command.Cli) *cobra.Command { flags.StringVar(&options.format, "format", "", flagsHelper.FormatHelp) flags.VarP(&options.filter, "filter", "f", "Filter output based on conditions provided") + 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/node/ps.go b/cli/command/node/ps.go index e999a36c1f..3174c6d5e6 100644 --- a/cli/command/node/ps.go +++ b/cli/command/node/ps.go @@ -6,6 +6,7 @@ import ( "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" + "github.com/docker/cli/cli/command/completion" "github.com/docker/cli/cli/command/idresolver" "github.com/docker/cli/cli/command/task" "github.com/docker/cli/opts" @@ -13,6 +14,7 @@ import ( "github.com/docker/docker/api/types/swarm" "github.com/pkg/errors" "github.com/spf13/cobra" + "github.com/spf13/pflag" ) type psOptions struct { @@ -49,6 +51,12 @@ func newPsCommand(dockerCli command.Cli) *cobra.Command { flags.StringVar(&options.format, "format", "", "Pretty-print tasks using a Go template") flags.BoolVarP(&options.quiet, "quiet", "q", false, "Only display task IDs") + 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/node/update.go b/cli/command/node/update.go index cb46cf37f9..a225dbb1b0 100644 --- a/cli/command/node/update.go +++ b/cli/command/node/update.go @@ -6,6 +6,7 @@ import ( "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" + "github.com/docker/cli/cli/command/completion" "github.com/docker/cli/opts" "github.com/docker/docker/api/types/swarm" "github.com/pkg/errors" @@ -34,6 +35,15 @@ func newUpdateCommand(dockerCli command.Cli) *cobra.Command { flags.Var(&options.annotations.labels, flagLabelAdd, `Add or update a node label ("key=value")`) labelKeys := opts.NewListOpts(nil) flags.Var(&labelKeys, flagLabelRemove, "Remove a node label if exists") + + _ = cmd.RegisterFlagCompletionFunc(flagRole, completion.FromList("worker", "manager")) + _ = cmd.RegisterFlagCompletionFunc(flagAvailability, completion.FromList("active", "pause", "drain")) + 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 }