Skip to content

Commit 768d107

Browse files
committed
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 <github@gone.nl>
1 parent d5e6e2e commit 768d107

3 files changed

Lines changed: 25 additions & 0 deletions

File tree

cli/command/node/list.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/docker/docker/api/types/system"
1515
"github.com/fvbommel/sortorder"
1616
"github.com/spf13/cobra"
17+
"github.com/spf13/pflag"
1718
)
1819

1920
type listOptions struct {
@@ -40,6 +41,12 @@ func newListCommand(dockerCli command.Cli) *cobra.Command {
4041
flags.StringVar(&options.format, "format", "", flagsHelper.FormatHelp)
4142
flags.VarP(&options.filter, "filter", "f", "Filter output based on conditions provided")
4243

44+
flags.VisitAll(func(flag *pflag.Flag) {
45+
// Set a default completion function if none was set. We don't look
46+
// up if it does already have one set, because Cobra does this for
47+
// us, and returns an error (which we ignore for this reason).
48+
_ = cmd.RegisterFlagCompletionFunc(flag.Name, completion.NoComplete)
49+
})
4350
return cmd
4451
}
4552

cli/command/node/ps.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import (
66

77
"github.com/docker/cli/cli"
88
"github.com/docker/cli/cli/command"
9+
"github.com/docker/cli/cli/command/completion"
910
"github.com/docker/cli/cli/command/idresolver"
1011
"github.com/docker/cli/cli/command/task"
1112
"github.com/docker/cli/opts"
1213
"github.com/docker/docker/api/types"
1314
"github.com/docker/docker/api/types/swarm"
1415
"github.com/pkg/errors"
1516
"github.com/spf13/cobra"
17+
"github.com/spf13/pflag"
1618
)
1719

1820
type psOptions struct {
@@ -49,6 +51,12 @@ func newPsCommand(dockerCli command.Cli) *cobra.Command {
4951
flags.StringVar(&options.format, "format", "", "Pretty-print tasks using a Go template")
5052
flags.BoolVarP(&options.quiet, "quiet", "q", false, "Only display task IDs")
5153

54+
flags.VisitAll(func(flag *pflag.Flag) {
55+
// Set a default completion function if none was set. We don't look
56+
// up if it does already have one set, because Cobra does this for
57+
// us, and returns an error (which we ignore for this reason).
58+
_ = cmd.RegisterFlagCompletionFunc(flag.Name, completion.NoComplete)
59+
})
5260
return cmd
5361
}
5462

cli/command/node/update.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/docker/cli/cli"
88
"github.com/docker/cli/cli/command"
9+
"github.com/docker/cli/cli/command/completion"
910
"github.com/docker/cli/opts"
1011
"github.com/docker/docker/api/types/swarm"
1112
"github.com/pkg/errors"
@@ -34,6 +35,15 @@ func newUpdateCommand(dockerCli command.Cli) *cobra.Command {
3435
flags.Var(&options.annotations.labels, flagLabelAdd, `Add or update a node label ("key=value")`)
3536
labelKeys := opts.NewListOpts(nil)
3637
flags.Var(&labelKeys, flagLabelRemove, "Remove a node label if exists")
38+
39+
_ = cmd.RegisterFlagCompletionFunc(flagRole, completion.FromList("worker", "manager"))
40+
_ = cmd.RegisterFlagCompletionFunc(flagAvailability, completion.FromList("active", "pause", "drain"))
41+
flags.VisitAll(func(flag *pflag.Flag) {
42+
// Set a default completion function if none was set. We don't look
43+
// up if it does already have one set, because Cobra does this for
44+
// us, and returns an error (which we ignore for this reason).
45+
_ = cmd.RegisterFlagCompletionFunc(flag.Name, completion.NoComplete)
46+
})
3747
return cmd
3848
}
3949

0 commit comments

Comments
 (0)