equal
deleted
inserted
replaced
1 # Generating Bash Completions For Your Own cobra.Command |
1 # Generating Bash Completions For Your Own cobra.Command |
|
2 |
|
3 If you are using the generator you can create a completion command by running |
|
4 |
|
5 ```bash |
|
6 cobra add completion |
|
7 ``` |
|
8 |
|
9 Update the help text show how to install the bash_completion Linux show here [Kubectl docs show mac options](https://kubernetes.io/docs/tasks/tools/install-kubectl/#enabling-shell-autocompletion) |
|
10 |
|
11 Writing the shell script to stdout allows the most flexible use. |
|
12 |
|
13 ```go |
|
14 // completionCmd represents the completion command |
|
15 var completionCmd = &cobra.Command{ |
|
16 Use: "completion", |
|
17 Short: "Generates bash completion scripts", |
|
18 Long: `To load completion run |
|
19 |
|
20 . <(bitbucket completion) |
|
21 |
|
22 To configure your bash shell to load completions for each session add to your bashrc |
|
23 |
|
24 # ~/.bashrc or ~/.profile |
|
25 . <(bitbucket completion) |
|
26 `, |
|
27 Run: func(cmd *cobra.Command, args []string) { |
|
28 rootCmd.GenBashCompletion(os.Stdout); |
|
29 }, |
|
30 } |
|
31 ``` |
|
32 |
|
33 **Note:** The cobra generator may include messages printed to stdout for example if the config file is loaded, this will break the auto complete script |
|
34 |
|
35 |
|
36 ## Example from kubectl |
2 |
37 |
3 Generating bash completions from a cobra command is incredibly easy. An actual program which does so for the kubernetes kubectl binary is as follows: |
38 Generating bash completions from a cobra command is incredibly easy. An actual program which does so for the kubernetes kubectl binary is as follows: |
4 |
39 |
5 ```go |
40 ```go |
6 package main |
41 package main |
45 if [[ $? -eq 0 ]]; then |
80 if [[ $? -eq 0 ]]; then |
46 return 0 |
81 return 0 |
47 fi |
82 fi |
48 } |
83 } |
49 |
84 |
50 __custom_func() { |
85 __kubectl_custom_func() { |
51 case ${last_command} in |
86 case ${last_command} in |
52 kubectl_get | kubectl_describe | kubectl_delete | kubectl_stop) |
87 kubectl_get | kubectl_describe | kubectl_delete | kubectl_stop) |
53 __kubectl_get_resource |
88 __kubectl_get_resource |
54 return |
89 return |
55 ;; |
90 ;; |
72 Run: runHelp, |
107 Run: runHelp, |
73 BashCompletionFunction: bash_completion_func, |
108 BashCompletionFunction: bash_completion_func, |
74 } |
109 } |
75 ``` |
110 ``` |
76 |
111 |
77 The `BashCompletionFunction` option is really only valid/useful on the root command. Doing the above will cause `__custom_func()` to be called when the built in processor was unable to find a solution. In the case of kubernetes a valid command might look something like `kubectl get pod [mypod]`. If you type `kubectl get pod [tab][tab]` the `__customc_func()` will run because the cobra.Command only understood "kubectl" and "get." `__custom_func()` will see that the cobra.Command is "kubectl_get" and will thus call another helper `__kubectl_get_resource()`. `__kubectl_get_resource` will look at the 'nouns' collected. In our example the only noun will be `pod`. So it will call `__kubectl_parse_get pod`. `__kubectl_parse_get` will actually call out to kubernetes and get any pods. It will then set `COMPREPLY` to valid pods! |
112 The `BashCompletionFunction` option is really only valid/useful on the root command. Doing the above will cause `__kubectl_custom_func()` (`__<command-use>_custom_func()`) to be called when the built in processor was unable to find a solution. In the case of kubernetes a valid command might look something like `kubectl get pod [mypod]`. If you type `kubectl get pod [tab][tab]` the `__kubectl_customc_func()` will run because the cobra.Command only understood "kubectl" and "get." `__kubectl_custom_func()` will see that the cobra.Command is "kubectl_get" and will thus call another helper `__kubectl_get_resource()`. `__kubectl_get_resource` will look at the 'nouns' collected. In our example the only noun will be `pod`. So it will call `__kubectl_parse_get pod`. `__kubectl_parse_get` will actually call out to kubernetes and get any pods. It will then set `COMPREPLY` to valid pods! |
78 |
113 |
79 ## Have the completions code complete your 'nouns' |
114 ## Have the completions code complete your 'nouns' |
80 |
115 |
81 In the above example "pod" was assumed to already be typed. But if you want `kubectl get [tab][tab]` to show a list of valid "nouns" you have to set them. Simplified code from `kubectl get` looks like: |
116 In the above example "pod" was assumed to already be typed. But if you want `kubectl get [tab][tab]` to show a list of valid "nouns" you have to set them. Simplified code from `kubectl get` looks like: |
82 |
117 |