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

142 lines
3.2 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"
2020-03-26 21:07:15 +00:00
"github.com/urfave/cli/v2"
2019-02-08 04:04:22 +00:00
)
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.
2020-03-26 21:07:15 +00:00
subcommands = append(subcommands, "--"+flag.Names()[0])
2019-07-10 03:51:02 +00:00
}
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.
2020-03-26 21:07:15 +00:00
opts = append(opts, "--"+flag.Names()[0])
2019-07-10 03:51:02 +00:00
}
fmt.Fprintln(c.App.Writer, fmt.Sprintf(zshCompletionTemplate, strings.Join(subcommands, "' '"), strings.Join(opts, "' '")))
return nil
}
2020-03-26 21:07:15 +00:00
var completionCommand = &cli.Command{
2019-07-10 03:51:02 +00:00
Name: "completion",
Usage: "Output shell completion code",
ArgsUsage: "SHELL",
2020-01-21 18:14:47 +00:00
Description: `Output shell completion code for bash, zsh or fish.
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)
2020-01-21 18:14:47 +00:00
# Installing fish completion on Linux
crictl completion fish | source
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)
2020-01-21 18:14:47 +00:00
case "fish":
return fishCompletion(c)
2019-07-10 03:51:02 +00:00
case "zsh":
return zshCompletion(c)
default:
2020-01-21 18:14:47 +00:00
return fmt.Errorf("only bash, zsh or fish are supported")
2019-07-10 03:51:02 +00:00
}
2019-02-08 04:04:22 +00:00
},
}
2020-01-21 18:14:47 +00:00
func fishCompletion(c *cli.Context) error {
completion, err := c.App.ToFishCompletion()
if err != nil {
return err
}
fmt.Fprintln(c.App.Writer, completion)
return nil
}