Skip to content

Commit

Permalink
Merge pull request #5848 from thaJeztah/improve_swarm_completion
Browse files Browse the repository at this point in the history
completion: fix / add completion for service names and node-names
  • Loading branch information
neersighted authored Feb 20, 2025
2 parents 2493a96 + 8f55738 commit eb48cad
Show file tree
Hide file tree
Showing 16 changed files with 174 additions and 9 deletions.
37 changes: 37 additions & 0 deletions cli/command/node/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package node

import (
"os"

"github.com/docker/cli/cli/command/completion"
"github.com/docker/docker/api/types"
"github.com/spf13/cobra"
)

// completeNodeNames offers completion for swarm node (host)names and optional IDs.
// By default, only names are returned.
// Set DOCKER_COMPLETION_SHOW_NODE_IDS=yes to also complete IDs.
//
// TODO(thaJeztah): add support for filters.
func completeNodeNames(dockerCLI completion.APIClientProvider) completion.ValidArgsFn {
// https://github.com/docker/cli/blob/f9ced58158d5e0b358052432244b483774a1983d/contrib/completion/bash/docker#L41-L43
showIDs := os.Getenv("DOCKER_COMPLETION_SHOW_NODE_IDS") == "yes"
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
list, err := dockerCLI.Client().NodeList(cmd.Context(), types.NodeListOptions{})
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

names := make([]string, 0, len(list)+1)
for _, node := range list {
if showIDs {
names = append(names, node.Description.Hostname, node.ID)
} else {
names = append(names, node.Description.Hostname)
}
}
// Nodes allow "self" as magic word for the current node.
names = append(names, "self")
return names, cobra.ShellCompDirectiveNoFileComp
}
}
1 change: 1 addition & 0 deletions cli/command/node/demote.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func newDemoteCommand(dockerCli command.Cli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runDemote(cmd.Context(), dockerCli, args)
},
ValidArgsFunction: completeNodeNames(dockerCli),
}
}

Expand Down
1 change: 1 addition & 0 deletions cli/command/node/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
opts.nodeIds = args
return runInspect(cmd.Context(), dockerCli, opts)
},
ValidArgsFunction: completeNodeNames(dockerCli),
}

flags := cmd.Flags()
Expand Down
7 changes: 7 additions & 0 deletions cli/command/node/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions cli/command/node/promote.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func newPromoteCommand(dockerCli command.Cli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runPromote(cmd.Context(), dockerCli, args)
},
ValidArgsFunction: completeNodeNames(dockerCli),
}
}

Expand Down
9 changes: 8 additions & 1 deletion cli/command/node/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,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 {
Expand Down Expand Up @@ -41,7 +42,7 @@ func newPsCommand(dockerCli command.Cli) *cobra.Command {

return runPs(cmd.Context(), dockerCli, options)
},
ValidArgsFunction: completion.NoComplete,
ValidArgsFunction: completeNodeNames(dockerCli),
}
flags := cmd.Flags()
flags.BoolVar(&options.noTrunc, "no-trunc", false, "Do not truncate output")
Expand All @@ -50,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
}

Expand Down
1 change: 1 addition & 0 deletions cli/command/node/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func newRemoveCommand(dockerCli command.Cli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runRemove(cmd.Context(), dockerCli, args, opts)
},
ValidArgsFunction: completeNodeNames(dockerCli),
}
flags := cmd.Flags()
flags.BoolVarP(&opts.force, "force", "f", false, "Force remove a node from the swarm")
Expand Down
11 changes: 11 additions & 0 deletions cli/command/node/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -25,6 +26,7 @@ func newUpdateCommand(dockerCli command.Cli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runUpdate(cmd.Context(), dockerCli, cmd.Flags(), args[0])
},
ValidArgsFunction: completeNodeNames(dockerCli),
}

flags := cmd.Flags()
Expand All @@ -33,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
}

Expand Down
17 changes: 14 additions & 3 deletions cli/command/service/cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package service

import (
"os"

"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/completion"
Expand Down Expand Up @@ -34,16 +36,25 @@ func NewServiceCommand(dockerCli command.Cli) *cobra.Command {
return cmd
}

// CompletionFn offers completion for swarm services
// CompletionFn offers completion for swarm service names and optional IDs.
// By default, only names are returned.
// Set DOCKER_COMPLETION_SHOW_SERVICE_IDS=yes to also complete IDs.
func CompletionFn(dockerCLI completion.APIClientProvider) completion.ValidArgsFn {
// https://github.com/docker/cli/blob/f9ced58158d5e0b358052432244b483774a1983d/contrib/completion/bash/docker#L41-L43
showIDs := os.Getenv("DOCKER_COMPLETION_SHOW_SERVICE_IDS") == "yes"
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
list, err := dockerCLI.Client().ServiceList(cmd.Context(), types.ServiceListOptions{})
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
var names []string

names := make([]string, 0, len(list))
for _, service := range list {
names = append(names, service.ID)
if showIDs {
names = append(names, service.Spec.Name, service.ID)
} else {
names = append(names, service.Spec.Name)
}
}
return names, cobra.ShellCompDirectiveNoFileComp
}
Expand Down
26 changes: 24 additions & 2 deletions cli/command/service/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/spf13/pflag"
)

func newCreateCommand(dockerCli command.Cli) *cobra.Command {
func newCreateCommand(dockerCLI command.Cli) *cobra.Command {
opts := newServiceOptions()

cmd := &cobra.Command{
Expand All @@ -28,7 +28,7 @@ func newCreateCommand(dockerCli command.Cli) *cobra.Command {
if len(args) > 1 {
opts.args = args[1:]
}
return runCreate(cmd.Context(), dockerCli, cmd.Flags(), opts)
return runCreate(cmd.Context(), dockerCLI, cmd.Flags(), opts)
},
ValidArgsFunction: completion.NoComplete,
}
Expand Down Expand Up @@ -75,6 +75,28 @@ func newCreateCommand(dockerCli command.Cli) *cobra.Command {
flags.SetAnnotation(flagHostAdd, "version", []string{"1.32"})

flags.SetInterspersed(false)

// TODO(thaJeztah): add completion for capabilities, stop-signal (currently non-exported in container package)
// _ = cmd.RegisterFlagCompletionFunc(flagCapAdd, completeLinuxCapabilityNames)
// _ = cmd.RegisterFlagCompletionFunc(flagCapDrop, completeLinuxCapabilityNames)
// _ = cmd.RegisterFlagCompletionFunc(flagStopSignal, completeSignals)

_ = cmd.RegisterFlagCompletionFunc(flagMode, completion.FromList("replicated", "global", "replicated-job", "global-job"))
_ = cmd.RegisterFlagCompletionFunc(flagEnv, completion.EnvVarNames) // TODO(thaJeztah): flagEnvRemove (needs to read current env-vars on the service)
_ = cmd.RegisterFlagCompletionFunc(flagEnvFile, completion.FileNames)
_ = cmd.RegisterFlagCompletionFunc(flagNetwork, completion.NetworkNames(dockerCLI))
_ = cmd.RegisterFlagCompletionFunc(flagRestartCondition, completion.FromList("none", "on-failure", "any"))
_ = cmd.RegisterFlagCompletionFunc(flagRollbackOrder, completion.FromList("start-first", "stop-first"))
_ = cmd.RegisterFlagCompletionFunc(flagRollbackFailureAction, completion.FromList("pause", "continue"))
_ = cmd.RegisterFlagCompletionFunc(flagUpdateOrder, completion.FromList("start-first", "stop-first"))
_ = cmd.RegisterFlagCompletionFunc(flagUpdateFailureAction, completion.FromList("pause", "continue", "rollback"))

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
}

Expand Down
9 changes: 9 additions & 0 deletions cli/command/service/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ 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/formatter"
flagsHelper "github.com/docker/cli/cli/flags"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/errdefs"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

type inspectOptions struct {
Expand Down Expand Up @@ -47,6 +49,13 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
flags := cmd.Flags()
flags.StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp)
flags.BoolVar(&opts.pretty, "pretty", false, "Print the information in a human friendly format")

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
}

Expand Down
7 changes: 7 additions & 0 deletions cli/command/service/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

type listOptions struct {
Expand Down Expand Up @@ -41,6 +42,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
}

Expand Down
9 changes: 9 additions & 0 deletions cli/command/service/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,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/service/logs"
"github.com/docker/docker/api/types"
Expand All @@ -22,6 +23,7 @@ import (
"github.com/docker/docker/pkg/stringid"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

type logsOptions struct {
Expand Down Expand Up @@ -69,6 +71,13 @@ func newLogsCommand(dockerCli command.Cli) *cobra.Command {
flags.BoolVar(&opts.details, "details", false, "Show extra details provided to logs")
flags.SetAnnotation("details", "version", []string{"1.30"})
flags.StringVarP(&opts.tail, "tail", "n", "all", "Number of lines to show from the end of the logs")

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
}

Expand Down
8 changes: 8 additions & 0 deletions cli/command/service/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/node"
"github.com/docker/cli/cli/command/task"
Expand All @@ -15,6 +16,7 @@ import (
"github.com/docker/docker/client"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

type psOptions struct {
Expand Down Expand Up @@ -48,6 +50,12 @@ func newPsCommand(dockerCli command.Cli) *cobra.Command {
flags.StringVar(&options.format, "format", "", "Pretty-print tasks using a Go template")
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
}

Expand Down
8 changes: 8 additions & 0 deletions cli/command/service/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (

"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/completion"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/versions"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

func newRollbackCommand(dockerCli command.Cli) *cobra.Command {
Expand All @@ -31,6 +33,12 @@ func newRollbackCommand(dockerCli command.Cli) *cobra.Command {
flags.BoolVarP(&options.quiet, flagQuiet, "q", false, "Suppress progress output")
addDetachFlag(flags, &options.detach)

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
}

Expand Down
Loading

0 comments on commit eb48cad

Please sign in to comment.