|
| 1 | +# kubectl-plugin_completion |
| 2 | + |
| 3 | +Auto completion saves a lot of time when typing in commands. The kubectl tool has a way to allow for completion for it's sub-commands, |
| 4 | +however it doesn't not accomodate for when these sub-commands are plugin commands. This repo contains the code for a kubectl plugin that generates scripts for extending the kubectl tool in order to accomodate for completions for kubectl plugin sub-commands. |
| 5 | + |
| 6 | +## Sections |
| 7 | +* [Install plugin](https://github.com/MartinSimango/kubectl-plugin-autocompletion#install-plugin) |
| 8 | +* [How it works](https://github.com/MartinSimango/kubectl-plugin-autocompletion#how-it-works) |
| 9 | +* [How to use](https://github.com/MartinSimango/kubectl-plugin-autocompletion#how-to-use) |
| 10 | +* [Example](https://github.com/MartinSimango/kubectl-plugin-autocompletion#example) |
| 11 | + |
| 12 | + |
| 13 | +## Install plugin |
| 14 | + |
| 15 | +Plugin can be installed in 3 ways: |
| 16 | + |
| 17 | +1. Via krew |
| 18 | + ```sh |
| 19 | +$ kubectl krew install --manifest-url https://github.com/MartinSimango/kubectl-plugin_completion/releases/download/v0.1.0/plugin_completion.yaml |
| 20 | +``` |
| 21 | +2. Zip file containing binary can be download on the [release page](https://github.com/MartinSimango/kubectl-plugin-autocompletion/releases) |
| 22 | + |
| 23 | +3. Manually by cloning the repo and running make install - (This requires you having go installed) |
| 24 | + |
| 25 | + |
| 26 | +## How it works |
| 27 | + |
| 28 | +The plugin works by reading config files for specfic shells (so far only bash and zsh). These config files in a folder located at `$HOME/.kube/plugin-completion-config` and are named `$SHELL_NAME.yaml`. So for example the zsh config file will be located at `$HOME/.kube/plugin-completion-config/zsh.yaml`. |
| 29 | + |
| 30 | +The config files contain information about the available kubectl plugins along with some additional information such as plugin's description and the plugin's completion function name. Below is an example of what a config file for a zsh terminal will look like: |
| 31 | + |
| 32 | +```yaml |
| 33 | +shell: zsh |
| 34 | +shellLocation: /bin/zsh |
| 35 | +plugins: |
| 36 | +- name: krew |
| 37 | + completionFunctionName: _krew |
| 38 | + description: A kubectl plugin called krew |
| 39 | +- name: hello |
| 40 | + completionFunctionName: "" |
| 41 | + description: A kubectl plugin called hello |
| 42 | +- name: plugin_completion |
| 43 | + completionFunctionName: _plugin_completion |
| 44 | + description: A kubectl plugin called plugin_completion |
| 45 | +``` |
| 46 | +
|
| 47 | +The above file can be generated by running: |
| 48 | +``` sh |
| 49 | +$ kubectl plugin_completion config generate |
| 50 | +``` |
| 51 | +This will generate config files for all supported shells (zsh and bash). For plugins that were created using [cobra](https://github.com/spf13/cobra) the `completionFunctionName` field will automatically be field in. The description for each plugin will default to: "A kubectl plugin called $plugin_name". |
| 52 | + |
| 53 | +If a plugin was not created by using [cobra](https://github.com/spf13/cobra) it will be up to you to manually edit the config file and provide the name of the plugin's completion function name. This can also be done by running the `kubectl plugin_completion config edit` command as shown below: |
| 54 | + |
| 55 | +```sh |
| 56 | +$ kubectl plugin_completion config edit zsh --plugin-name=custom_plugin --completion-function="_completion_function_name" |
| 57 | +``` |
| 58 | + |
| 59 | +The plugin_completion uses config files to generate completions specific shells in order to allow for completions for kubectl plugins. The generated completion scripts overwrite the behaviour of the completion function for the kubectl tool and extend it to allow for kubectl plugin completions. |
| 60 | + |
| 61 | +## How to use |
| 62 | + |
| 63 | +To start of you will need to generate your shell plugin config files. This can be done by running the below command: |
| 64 | +``` sh |
| 65 | +$ kubectl plugin_completion config generate |
| 66 | +``` |
| 67 | + |
| 68 | +Once the config files are generated you can source the plugin completion script by running: |
| 69 | + |
| 70 | +```sh |
| 71 | +$ source <(kubectl plugin_completion plugin-completion $SHELL) |
| 72 | +``` |
| 73 | + |
| 74 | +where `$SHELL` is the shell you want to generate the completion script for. |
| 75 | + |
| 76 | +To make the changes permanent you can add `source <(kubectl plugin_completion plugin-completion zsh)` to your `~/.zshrc` file or `source <(kubectl plugin_completion plugin-completion bash)` to your `~/.bashrc (or ~/.bash_profile)` file. |
| 77 | + |
| 78 | +Please note if you change the config files for a shell you will need to rerun the `source <(kubectl plugin_completion plugin-completion $SHELL)` command as the plugin completion script is generated from using information from the config files. |
| 79 | + |
| 80 | +Once the above is done completions for your plugins should work as displayed in the below Example section. |
| 81 | + |
| 82 | + |
| 83 | +## Example |
| 84 | +<p align="center"> |
| 85 | +<img src="https://github.com/MartinSimango/kubectl-plugin-autocompletion/blob/main/kub-plugin.gif" style="width:600px;height=1000px"/> |
| 86 | +</p> |
| 87 | +<!--  --> |
| 88 | + |
| 89 | + |
0 commit comments