Skip to content

Commit

Permalink
cli/command/container: add completion for --restart
Browse files Browse the repository at this point in the history
With this patch:

    docker run --restart <TAB>
    always  no  on-failure  unless-stopped

Signed-off-by: Sebastiaan van Stijn <[email protected]>
  • Loading branch information
thaJeztah committed Jul 5, 2024
1 parent c96ca62 commit 683bc01
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
15 changes: 15 additions & 0 deletions cli/command/container/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package container

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

Expand Down Expand Up @@ -64,6 +65,20 @@ var allLinuxCapabilities = []string{
"CAP_CHECKPOINT_RESTORE",
}

// restartPolicies is a list of all valid restart-policies..
//
// TODO(thaJeztah): add descriptions, and enable descriptions for our completion scripts (cobra.CompletionOptions.DisableDescriptions is currently set to "true")
var restartPolicies = []string{
string(container.RestartPolicyDisabled),
string(container.RestartPolicyAlways),
string(container.RestartPolicyOnFailure),
string(container.RestartPolicyUnlessStopped),
}

func completeLinuxCapabilityNames(cmd *cobra.Command, args []string, toComplete string) (names []string, _ cobra.ShellCompDirective) {
return completion.FromList(allLinuxCapabilities...)(cmd, args, toComplete)
}

func completeRestartPolicies(cmd *cobra.Command, args []string, toComplete string) (names []string, _ cobra.ShellCompDirective) {
return completion.FromList(restartPolicies...)(cmd, args, toComplete)
}
1 change: 1 addition & 0 deletions cli/command/container/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func NewCreateCommand(dockerCli command.Cli) *cobra.Command {
_ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames)
_ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli))
_ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever))
_ = cmd.RegisterFlagCompletionFunc("restart", completeRestartPolicies)
return cmd
}

Expand Down
1 change: 1 addition & 0 deletions cli/command/container/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func NewRunCommand(dockerCli command.Cli) *cobra.Command {
_ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames)
_ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli))
_ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever))
_ = cmd.RegisterFlagCompletionFunc("restart", completeRestartPolicies)
return cmd
}

Expand Down
2 changes: 2 additions & 0 deletions cli/command/container/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ func NewUpdateCommand(dockerCli command.Cli) *cobra.Command {
flags.Var(&options.cpus, "cpus", "Number of CPUs")
flags.SetAnnotation("cpus", "version", []string{"1.29"})

_ = cmd.RegisterFlagCompletionFunc("restart", completeRestartPolicies)

return cmd
}

Expand Down

0 comments on commit 683bc01

Please sign in to comment.