Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2.2k
Cobra completion v2 with CLI plugin support#3429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package completion | ||
| import ( | ||
| "os" | ||
| "github.com/docker/cli/cli/command" | ||
| "github.com/docker/cli/cli/command/formatter" | ||
| "github.com/docker/docker/api/types" | ||
| "github.com/docker/docker/api/types/filters" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
| // ValidArgsFn a function to be used by cobra command as `ValidArgsFunction` to offer command line completion | ||
| type ValidArgsFn func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) | ||
| // ImageNames offers completion for images present within the local store | ||
| func ImageNames(dockerCli command.Cli) ValidArgsFn { | ||
| return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
| list, err := dockerCli.Client().ImageList(cmd.Context(), types.ImageListOptions{}) | ||
| if err != nil { | ||
| return nil, cobra.ShellCompDirectiveError | ||
| } | ||
| var names []string | ||
| for _, image := range list { | ||
| names = append(names, image.RepoTags...) | ||
| } | ||
| return names, cobra.ShellCompDirectiveNoFileComp | ||
| } | ||
| } | ||
| // ContainerNames offers completion for container names and IDs | ||
| // By default, only names are returned. | ||
| // Set DOCKER_COMPLETION_SHOW_CONTAINER_IDS=yes to also complete IDs. | ||
| func ContainerNames(dockerCli command.Cli, all bool, filters ...func(types.Container) bool) ValidArgsFn { | ||
| return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
| list, err := dockerCli.Client().ContainerList(cmd.Context(), types.ContainerListOptions{ | ||
| All: all, | ||
| }) | ||
| if err != nil { | ||
| return nil, cobra.ShellCompDirectiveError | ||
| } | ||
| showContainerIDs := os.Getenv("DOCKER_COMPLETION_SHOW_CONTAINER_IDS") == "yes" | ||
| var names []string | ||
| for _, container := range list { | ||
| skip := false | ||
| for _, fn := range filters { | ||
| if !fn(container) { | ||
| skip = true | ||
| break | ||
| } | ||
| } | ||
| if skip { | ||
| continue | ||
| } | ||
| if showContainerIDs { | ||
| names = append(names, container.ID) | ||
| } | ||
| names = append(names, formatter.StripNamePrefix(container.Names)...) | ||
| } | ||
| return names, cobra.ShellCompDirectiveNoFileComp | ||
| } | ||
| } | ||
| // VolumeNames offers completion for volumes | ||
| func VolumeNames(dockerCli command.Cli) ValidArgsFn { | ||
| return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
| list, err := dockerCli.Client().VolumeList(cmd.Context(), filters.Args{}) | ||
| if err != nil { | ||
| return nil, cobra.ShellCompDirectiveError | ||
| } | ||
| var names []string | ||
| for _, volume := range list.Volumes { | ||
| names = append(names, volume.Name) | ||
| } | ||
| return names, cobra.ShellCompDirectiveNoFileComp | ||
| } | ||
| } | ||
| // NetworkNames offers completion for networks | ||
| func NetworkNames(dockerCli command.Cli) ValidArgsFn { | ||
| return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
| list, err := dockerCli.Client().NetworkList(cmd.Context(), types.NetworkListOptions{}) | ||
| if err != nil { | ||
| return nil, cobra.ShellCompDirectiveError | ||
| } | ||
| var names []string | ||
| for _, network := range list { | ||
| names = append(names, network.Name) | ||
| } | ||
| return names, cobra.ShellCompDirectiveNoFileComp | ||
| } | ||
| } | ||
| // NoComplete is used for commands where there's no relevant completion | ||
| func NoComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
| return nil, cobra.ShellCompDirectiveNoFileComp | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -4,9 +4,11 @@ import ( | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "github.com/docker/cli/cli" | ||
| "github.com/docker/cli/cli/command" | ||
| "github.com/docker/cli/cli/command/completion" | ||
| "github.com/docker/cli/cli/config/configfile" | ||
| "github.com/docker/cli/opts" | ||
| "github.com/docker/docker/api/types" | ||
| @@ -52,6 +54,7 @@ func NewExecCommand(dockerCli command.Cli) *cobra.Command { | ||
| options.Command = args[1:] | ||
| return RunExec(dockerCli, options) | ||
| }, | ||
| ValidArgsFunction: completion.ContainerNames(dockerCli, false), | ||
| Annotations: map[string]string{ | ||
| "category-top": "2", | ||
| }, | ||
| @@ -73,6 +76,19 @@ func NewExecCommand(dockerCli command.Cli) *cobra.Command { | ||
| flags.StringVarP(&options.Workdir, "workdir", "w", "", "Working directory inside the container") | ||
| flags.SetAnnotation("workdir", "version", []string{"1.35"}) | ||
| cmd.RegisterFlagCompletionFunc( | ||
| "env", | ||
| func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
| return os.Environ(), cobra.ShellCompDirectiveNoFileComp | ||
| }, | ||
| ) | ||
| cmd.RegisterFlagCompletionFunc( | ||
| "env-file", | ||
| func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
| return nil, cobra.ShellCompDirectiveDefault // _filedir | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Default directive will list files in current dir as completion option, which is relevant for this flag | ||
| }, | ||
| ) | ||
| return cmd | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.