From c28aec2308a24f589814cf4ac55a225d144f557d Mon Sep 17 00:00:00 2001 From: Ulysses Souza Date: Tue, 22 Jun 2021 18:18:25 -0300 Subject: [PATCH] Filter completions by toComplete variable Signed-off-by: Ulysses Souza --- cmd/compose/completion.go | 74 +++++---------------------------------- cmd/compose/compose.go | 1 - cmd/compose/ps.go | 10 ------ 3 files changed, 8 insertions(+), 77 deletions(-) diff --git a/cmd/compose/completion.go b/cmd/compose/completion.go index e3bd75ef8..891539b6b 100644 --- a/cmd/compose/completion.go +++ b/cmd/compose/completion.go @@ -17,75 +17,11 @@ package compose import ( - "os" + "strings" "github.com/spf13/cobra" ) -func completionCommand() *cobra.Command { - return &cobra.Command{ - Use: "completion [bash|zsh|fish|powershell]", - Short: "Generate completion script", - Long: `To load completions: - -Bash: - - $ source <(docker compose completion bash) - - # To load completions for each session, execute once: - # Linux: - $ docker compose completion bash > /etc/bash_completion.d/docker_compose - # macOS: - $ docker compose completion bash > /usr/local/etc/bash_completion.d/docker_compose - -Zsh: - - # If shell completion is not already enabled in your environment, - # you will need to enable it. You can execute the following once: - - $ echo "autoload -U compinit; compinit" >> ~/.zshrc - - # To load completions for each session, execute once: - $ docker compose completion zsh > "${fpath[1]}/_docker_compose" - - # You will need to start a new shell for this setup to take effect. - -fish: - - $ docker compose completion fish | source - - # To load completions for each session, execute once: - $ docker compose completion fish > ~/.config/fish/completions/docker_compose.fish - -PowerShell: - - PS> docker compose completion powershell | Out-String | Invoke-Expression - - # To load completions for every new session, run: - PS> docker compose completion powershell > docker_compose.ps1 - # and source this file from your PowerShell profile. -`, - DisableFlagsInUseLine: true, - ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, - Args: cobra.ExactValidArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { - var err error - switch args[0] { - case "bash": - err = cmd.Root().GenBashCompletion(os.Stdout) - case "zsh": - err = cmd.Root().GenZshCompletion(os.Stdout) - case "fish": - err = cmd.Root().GenFishCompletion(os.Stdout, true) - case "powershell": - err = cmd.Root().GenPowerShellCompletion(os.Stdout) - } - return err - }, - Hidden: true, - } -} - // validArgsFn defines a completion func to be returned to fetch completion options type validArgsFn func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) @@ -101,6 +37,12 @@ func serviceCompletion(p *projectOptions) validArgsFn { if err != nil { return nil, cobra.ShellCompDirectiveNoFileComp } - return project.ServiceNames(), cobra.ShellCompDirectiveNoFileComp + var serviceNames []string + for _, s := range project.ServiceNames() { + if toComplete == "" || strings.HasPrefix(s, toComplete) { + serviceNames = append(serviceNames, s) + } + } + return serviceNames, cobra.ShellCompDirectiveNoFileComp } } diff --git a/cmd/compose/compose.go b/cmd/compose/compose.go index 6d292b81d..85fa4f7ab 100644 --- a/cmd/compose/compose.go +++ b/cmd/compose/compose.go @@ -276,7 +276,6 @@ func RootCommand(contextType string, backend api.Service) *cobra.Command { portCommand(&opts, backend), imagesCommand(&opts, backend), versionCommand(), - completionCommand(), ) if contextType == store.LocalContextType || contextType == store.DefaultContextType { diff --git a/cmd/compose/ps.go b/cmd/compose/ps.go index 70a99789b..9bb9a6e5a 100644 --- a/cmd/compose/ps.go +++ b/cmd/compose/ps.go @@ -185,13 +185,3 @@ func filterByStatus(containers []api.ContainerSummary, status string) []api.Cont } return filtered } - -func psCompletion(p *projectOptions) validArgsFn { - return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - project, err := p.toProject(nil) - if err != nil { - return nil, cobra.ShellCompDirectiveNoFileComp - } - return project.ServiceNames(), cobra.ShellCompDirectiveNoFileComp - } -}