From e5230fdbbe455e1d02b1cf42a7bf8e0f1bc273af Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Mon, 11 Mar 2019 17:32:03 +0100 Subject: [PATCH 1/2] add simple completion support for zsh --- cmd/crictl/completion.go | 96 +++++++++++++++++++++++++++++++++------- 1 file changed, 79 insertions(+), 17 deletions(-) diff --git a/cmd/crictl/completion.go b/cmd/crictl/completion.go index 2b9c2cb7c0..1c8482b800 100644 --- a/cmd/crictl/completion.go +++ b/cmd/crictl/completion.go @@ -34,33 +34,95 @@ var bashCompletionTemplate = `_cli_bash_autocomplete() { complete -F _cli_bash_autocomplete crictl` +func bashCompletion(c *cli.Context) error { + subcommands := []string{} + for _, command := range c.App.Commands { + if command.Hidden { + continue + } + for _, name := range command.Names() { + subcommands = append(subcommands, name) + } + } + + for _, flag := range c.App.Flags { + // only includes full flag name. + subcommands = append(subcommands, "--"+strings.Split(flag.GetName(), ",")[0]) + } + + fmt.Fprintln(c.App.Writer, fmt.Sprintf(bashCompletionTemplate, strings.Join(subcommands, "\n"))) + return nil +} + +var zshCompletionTemplate = `_cli_zsh_autocomplete() { + + local -a cmds + cmds=('%s') + _describe 'commands' cmds + + local -a opts + opts=('%s') + _describe 'global options' opts + + return +} + +compdef _cli_zsh_autocomplete crictl` + +func zshCompletion(c *cli.Context) error { + subcommands := []string{} + for _, command := range c.App.Commands { + if command.Hidden { + continue + } + for _, name := range command.Names() { + subcommands = append(subcommands, name+":"+command.Usage) + } + } + + opts := []string{} + for _, flag := range c.App.Flags { + // only includes full flag name. + opts = append(opts, "--"+strings.Split(flag.GetName(), ",")[0]) + } + + fmt.Fprintln(c.App.Writer, fmt.Sprintf(zshCompletionTemplate, strings.Join(subcommands, "' '"), strings.Join(opts, "' '"))) + return nil + +} + var completionCommand = cli.Command{ - Name: "completion", - Usage: "Output bash shell completion code", - Description: `Output bash shell completion code. + Name: "completion", + Usage: "Output shell completion code", + ArgsUsage: "SHELL", + Description: `Output shell completion code for bash or zsh. Examples: # Installing bash completion on Linux - source <(crictl completion) + source <(crictl completion bash) + + # Installing zsh completion on Linux + source <(crictl completion zsh) `, Action: func(c *cli.Context) error { - subcommands := []string{} - for _, command := range c.App.Commands { - if command.Hidden { - continue - } - for _, name := range command.Names() { - subcommands = append(subcommands, name) - } + // select bash by default for backwards compatibility + if c.NArg() == 0 { + return bashCompletion(c) } - for _, flag := range c.App.Flags { - // only includes full flag name. - subcommands = append(subcommands, "--"+strings.Split(flag.GetName(), ",")[0]) + if c.NArg() != 1 { + return cli.ShowSubcommandHelp(c) } - fmt.Fprintln(c.App.Writer, fmt.Sprintf(bashCompletionTemplate, strings.Join(subcommands, "\n"))) - return nil + shell := c.Args().First() + switch shell { + case "bash": + return bashCompletion(c) + case "zsh": + return zshCompletion(c) + default: + return fmt.Errorf("unknown shell: %q", shell) + } }, } From 0277b92b08f90d95ba6d75fc76b8421822dc0cf1 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Thu, 14 Mar 2019 10:29:03 +0100 Subject: [PATCH 2/2] completion: better log message for unsupported shells --- cmd/crictl/completion.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cmd/crictl/completion.go b/cmd/crictl/completion.go index 1c8482b800..d062cb779c 100644 --- a/cmd/crictl/completion.go +++ b/cmd/crictl/completion.go @@ -115,14 +115,13 @@ Examples: return cli.ShowSubcommandHelp(c) } - shell := c.Args().First() - switch shell { + switch c.Args().First() { case "bash": return bashCompletion(c) case "zsh": return zshCompletion(c) default: - return fmt.Errorf("unknown shell: %q", shell) + return fmt.Errorf("only bash and zsh supported") } }, }