|
14 | 14 | - [Scenarios](#scenarios)
|
15 | 15 | - [Implementation Details/Design](#implementation-detailsdesign)
|
16 | 16 | - [Naming Conventions](#naming-conventions)
|
| 17 | + - [Shell Completion Support](#shell-completion-support) |
17 | 18 | - [Implementation Notes/Constraints](#implementation-notesconstraints)
|
18 | 19 | - [Risks and Mitigations](#risks-and-mitigations)
|
19 | 20 | - [Graduation Criteria](#graduation-criteria)
|
@@ -156,6 +157,40 @@ Under this proposal, `kubectl` would identify plugins by looking for filenames b
|
156 | 157 | A search for these names would occur on a user's `PATH`. Only files that are executable and begin with this prefix
|
157 | 158 | would be identified.
|
158 | 159 |
|
| 160 | +#### Shell Completion Support |
| 161 | + |
| 162 | +As an extension to this proposal, this section describes how shell completion support can be added to the implementation |
| 163 | +of a `kubectl` plugin. |
| 164 | + |
| 165 | +For a plugin to support shell completion, it will need to provide an independent executable file using the prefix |
| 166 | +`kubectl_complete-` followed by the plugin name. For example, a plugin named `kubectl-foo` would |
| 167 | +provide a completion executable named `kubectl_complete-foo`. |
| 168 | + |
| 169 | +Similarly to the plugin executable, the completion executable will need to be found on `PATH`. When |
| 170 | +`kubectl` requires shell completions for the plugin, it will look for and execute the |
| 171 | +`kubectl_complete-<plugin>` file, passing it the command-line that needs to be completed as multiple |
| 172 | +arguments. The `kubectl_complete-<plugin>` executable will need to have the logic to determine what the |
| 173 | +proper completion choices are and output them to standard output to be consumed by `kubectl`. |
| 174 | + |
| 175 | +For a richer completion experience, `kubectl` will support Cobra's shell completion directive system which will |
| 176 | +allow plugins to optionally control certain behaviors of the shell when it performs shell completion. Please refer to |
| 177 | +[the Cobra documentation](https://github.com/spf13/cobra/blob/master/shell_completions.md#dynamic-completion-of-nouns) for details. |
| 178 | + |
| 179 | +Supporting shell completion and therefore providing a `kubectl_complete-<plugin>` executable is entirely |
| 180 | +optional. If it is not provided, `kubectl` will simply not perform any special shell completion for the |
| 181 | +plugin and will instead default to file completion. Note that providing a `kubectl_complete-<plugin>` |
| 182 | +executable is backwards-compatible and will simply be ignored when using older versions of `kubectl`. |
| 183 | + |
| 184 | +It is worth mentioning that if a `kubectl` plugin is implemented using [the Cobra project](https://github.com/spf13/cobra), |
| 185 | +it can use Cobra's built-in shell completion support directly. As an example, if we have a plugin named `myplugin` |
| 186 | +which uses Cobra, its `kubectl_complete-myplugin` executable can simply be: |
| 187 | +```sh |
| 188 | +#!/usr/bin/env sh |
| 189 | + |
| 190 | +kubectl myplugin __complete "$@" |
| 191 | +``` |
| 192 | +Then all that is needed is to name this file `kubectl_complete-myplugin`, put it somewhere on `$PATH` and give it executable permissions. |
| 193 | + |
159 | 194 | ### Implementation Notes/Constraints
|
160 | 195 |
|
161 | 196 | The current implementation details for the proposed design rely on using a plugin executable's name to determine what
|
|
0 commit comments