Add completion to "compose ps"

Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
This commit is contained in:
Ulysses Souza 2021-06-09 16:35:33 -03:00
parent 3174f49dc2
commit 37961c51ca
4 changed files with 25 additions and 10 deletions

View File

@ -68,6 +68,9 @@ var (
"serve": {},
"version": {},
"backend-metadata": {},
// Special hidden commands used by cobra for completion
"__complete": {},
"__completeNoDesc": {},
}
unknownCommandRegexp = regexp.MustCompile(`unknown docker command: "([^"]*)"`)
)
@ -167,7 +170,7 @@ func main() {
})
// populate the opts with the global flags
flags.Parse(os.Args[1:]) //nolint: errcheck
flags.Parse(os.Args[1:]) // nolint: errcheck
level, err := logrus.ParseLevel(opts.LogLevel)
if err != nil {
@ -223,18 +226,16 @@ func main() {
volume.Command(ctype),
)
if ctype != store.DefaultContextType {
// On default context, "compose" is implemented by CLI Plugin
proxy := api.NewServiceProxy().WithService(service.ComposeService())
command := compose2.RootCommand(ctype, proxy)
// On default context, "compose" is implemented by CLI Plugin
proxy := api.NewServiceProxy().WithService(service.ComposeService())
command := compose2.RootCommand(ctype, proxy)
if ctype == store.AciContextType {
customizeCliForACI(command, proxy)
}
root.AddCommand(command)
if ctype == store.AciContextType {
customizeCliForACI(command, proxy)
}
root.AddCommand(command)
if err = root.ExecuteContext(ctx); err != nil {
handleError(ctx, err, ctype, currentContext, cc, root)
}

View File

@ -44,6 +44,9 @@ import (
// Command defines a compose CLI command as a func with args
type Command func(context.Context, []string) error
// 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)
// Adapt a Command func to cobra library
func Adapt(fn Command) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {

View File

@ -76,6 +76,7 @@ func psCommand(p *projectOptions, backend api.Service) *cobra.Command {
RunE: Adapt(func(ctx context.Context, args []string) error {
return runPs(ctx, backend, args, opts)
}),
ValidArgsFunction: psCompletion(p),
}
flags := cmd.Flags()
flags.StringVar(&opts.Format, "format", "pretty", "Format the output. Values: [pretty | json]")
@ -184,3 +185,13 @@ 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
}
}