|
| 1 | +/* |
| 2 | +Copyright 2017 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "github.com/urfave/cli" |
| 24 | +) |
| 25 | + |
| 26 | +var bashCompletionTemplate = `_cli_bash_autocomplete() { |
| 27 | + local cur opts base |
| 28 | + COMPREPLY=() |
| 29 | + cur="${COMP_WORDS[COMP_CWORD]}" |
| 30 | + opts="%s" |
| 31 | + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) |
| 32 | + return 0 |
| 33 | +} |
| 34 | +
|
| 35 | +complete -F _cli_bash_autocomplete crictl` |
| 36 | + |
| 37 | +var completionCommand = cli.Command{ |
| 38 | + Name: "completion", |
| 39 | + Usage: "Output bash shell completion code", |
| 40 | + Description: `Output bash shell completion code. |
| 41 | +
|
| 42 | +Examples: |
| 43 | +
|
| 44 | + # Installing bash completion on Linux |
| 45 | + source <(crictl completion) |
| 46 | + `, |
| 47 | + Action: func(c *cli.Context) error { |
| 48 | + subcommands := []string{} |
| 49 | + for _, command := range c.App.Commands { |
| 50 | + if command.Hidden { |
| 51 | + continue |
| 52 | + } |
| 53 | + for _, name := range command.Names() { |
| 54 | + subcommands = append(subcommands, name) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + for _, flag := range c.App.Flags { |
| 59 | + // only includes full flag name. |
| 60 | + subcommands = append(subcommands, "--"+strings.Split(flag.GetName(), ",")[0]) |
| 61 | + } |
| 62 | + |
| 63 | + fmt.Fprintln(c.App.Writer, fmt.Sprintf(bashCompletionTemplate, strings.Join(subcommands, "\n"))) |
| 64 | + return nil |
| 65 | + }, |
| 66 | +} |
0 commit comments