k3s/vendor/github.com/kubernetes-sigs/cri-tools/cmd/crictl/completion.go

128 lines
2.9 KiB
Go
Raw Normal View History

2019-02-08 04:04:22 +00:00
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package crictl
import (
"fmt"
"strings"
"github.com/urfave/cli"
)
var bashCompletionTemplate = `_cli_bash_autocomplete() {
local cur opts base
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
opts="%s"
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
}
complete -F _cli_bash_autocomplete crictl`
2019-07-10 03:51:02 +00:00
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
}
2019-02-08 04:04:22 +00:00
var completionCommand = cli.Command{
2019-07-10 03:51:02 +00:00
Name: "completion",
Usage: "Output shell completion code",
ArgsUsage: "SHELL",
Description: `Output shell completion code for bash or zsh.
2019-02-08 04:04:22 +00:00
Examples:
# Installing bash completion on Linux
2019-07-10 03:51:02 +00:00
source <(crictl completion bash)
# Installing zsh completion on Linux
source <(crictl completion zsh)
2019-02-08 04:04:22 +00:00
`,
Action: func(c *cli.Context) error {
2019-07-10 03:51:02 +00:00
// select bash by default for backwards compatibility
if c.NArg() == 0 {
return bashCompletion(c)
2019-02-08 04:04:22 +00:00
}
2019-07-10 03:51:02 +00:00
if c.NArg() != 1 {
return cli.ShowSubcommandHelp(c)
2019-02-08 04:04:22 +00:00
}
2019-07-10 03:51:02 +00:00
switch c.Args().First() {
case "bash":
return bashCompletion(c)
case "zsh":
return zshCompletion(c)
default:
return fmt.Errorf("only bash and zsh supported")
}
2019-02-08 04:04:22 +00:00
},
}