Skip to content

Auto-completion framework for plugins - #7468

Merged
marckhouzam merged 2 commits into
helm:masterfrom
VilledeMontreal:feat/pluginCompletion
Feb 21, 2020
Merged

marckhouzam merged 2 commits into
helm:masterfrom
VilledeMontreal:feat/pluginCompletion

Conversation

@marckhouzam

Copy link
Copy Markdown
Member

Closes #7043

This PR adds a framework allowing plugins to provide both static auto-completion and dynamic auto-completion, as described in #7043

In summary, with this PR plugins could optionally provide:

  1. a completion.yaml file to specify all flags and sub-commands to be completed
  2. a plugin.complete executable to be called when dynamic completions are required (i.e., when the list of flags and sub-commands does not provide the required completion)

I've prepared examples of how to use this framework in the fullstatus plugin and the 2to3 plugin.
The new completion.yaml file providing static completion can be seen at
https://github.com/marckhouzam/helm-fullstatus/blob/master/completion.yaml and https://github.com/marckhouzam/helm-2to3/blob/master/completion.yaml

For the fullstatus plugin, the new plugin.complete file providing dynamic completion can be seen at
https://github.com/marckhouzam/helm-fullstatus/blob/master/plugin.complete

For the 2to3 plugin, I've used the actual go binary to provide simple dynamic completion, as can be seen in the following commit: marckhouzam/helm-2to3@0e22f70

To test:

cd helm; make
bin/helm plugin install https://github.com/marckhouzam/helm-fullstatus
bin/helm plugin install https://github.com/marckhouzam/helm-2to3

source <(bin/helm completion bash)

bin/helm fullstatus --ou<TAB>
bin/helm fullstatus --output <TAB>
bin/helm fullstatus <TAB>

bin/helm 2to3 <TAB>
bin/helm 2to3 con<TAB>
bin/helm 2to3 convert <TAB>
# etc

This PR includes go tests for the new feature.
A first pass at some user documentation can be see in the description of #7043

Of note:

  • This PR provides a contract between plugin developers and Helm, which will require backwards-compatibility support.

Questions for maintainers:

  • Should the existing plugin.yaml file be enhanced to allow plugin developers to specify which files should be used to provide completions? I've gone an easier route and hard-coded the files to be <pluginRootDir>/completion.yaml and <pluginRootDir>/plugin.complete for every plugin.

@helm-bot helm-bot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Jan 27, 2020
@marckhouzam

Copy link
Copy Markdown
Member Author

The tests pass on my machine. I'll have to investigate.

Allow plugins to optionally specify their sub-cmds and flags through a
simple yaml file.

When generating the completion script with the command
'helm completion <bash|zsh>' (and only then), helm will look for that
yaml file in the plugin's directory.  If the file exists, helm will
create cobra commands and flags so that the completion script will
handle them.

Signed-off-by: Marc Khouzam <[email protected]>
@marckhouzam
marckhouzam force-pushed the feat/pluginCompletion branch 3 times, most recently from 5d4658f to 89ee0f8 Compare January 31, 2020 03:33
For each plugin, helm registers a ValidArgsFunction which the completion
script will call when it needs dynamic completion.  This
ValidArgsFunction will setup the plugin environment and then call the
executable `plugin.complete`, if it is provided by the plugin.

The output of the call to `plugin.complete` will be used as completion
choices.  The last line of the output can optionally be used by the
plugin to specify a completion directive.

Signed-off-by: Marc Khouzam <[email protected]>
@marckhouzam
marckhouzam force-pushed the feat/pluginCompletion branch from 89ee0f8 to 82bfe37 Compare January 31, 2020 15:15
@marckhouzam

Copy link
Copy Markdown
Member Author

The test failures were caused by shell script differences in the tests between my machine and the CI image.

(This is one of the reasons why we now have helm completions coded in Go instead of shell. However, for plugins, which may not be go programs, shell scripts are a possibility, as I implemented in the tests).

@marckhouzam

Copy link
Copy Markdown
Member Author

There are acceptance-tests written for this feature: helm/acceptance-testing#63

@bacongobbler bacongobbler left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code LGTM!

@bacongobbler

bacongobbler commented Feb 20, 2020 •

Copy link
Copy Markdown
Member
  • Should the existing plugin.yaml file be enhanced to allow plugin developers to specify which files should be used to provide completions? I've gone an easier route and hard-coded the files to be <pluginRootDir>/completion.yaml and <pluginRootDir>/plugin.complete for every plugin.

I think that would be great, but we can start here and see how things shake out as users play with this new functionality. I like to err on the side of "less is more". :)

@marckhouzam

Copy link
Copy Markdown
Member Author

Thanks @bacongobbler !

As the user-facing API is approved, I'll post a PR to helm-www to document this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Proposal: Auto-completion framework for plugins

3 participants