Skip to content

fix(comp): Prepare plugin completion for Cobra 1.0 - #7906

Merged
marckhouzam merged 1 commit into
helm:masterfrom
VilledeMontreal:fix/compPluginUpdate
May 15, 2020
Merged

marckhouzam merged 1 commit into
helm:masterfrom
VilledeMontreal:fix/compPluginUpdate

Conversation

@marckhouzam

@marckhouzam marckhouzam commented Apr 12, 2020 •

Copy link
Copy Markdown
Member

What this PR does / why we need it

Prepares Helm plugin completion for Cobra 1.0.

See Cobra v1.0.0 changelog for more details, but the changes we aim for are:

However, supporting these changes will break helm's current plugin completion. This PR fixes that (see below for details).

Special notes for your reviewer

This PR seems larger than it really is as it is mostly a refactoring to extract the logic to call a plugin's executable script. The behavioural change is small.

The completion part was verified with the acceptance-testing repo.


Cobra 1.0.0 introduces support for Custom Go Completions. However, there is a small difference compared to helm's existing solution. The difference is that Cobra will not complete sub-commands if there are any dynamic completion function registered.

The way helm implemented plugin completions had the dynamic completion function associated with the root plugin command (e.g. 2to3); with Cobra, this prevents any of the sub-commands of the plugin to be completed (i.e., 2to3 will not show the static sub-commands).

The solution is to only register the dynamic completion function for a plugin command that doesn't have any further sub-commands. This allows all sub-commands to be completed properly, and when there are no more levels of sub-commands, the dynamic completion is called.

This commit had to make two changes to achieve this:

  1. refactor the load_plugins.go file to extract the logic to call the plugin.complete executable (for dynamic completions), so that it could be added to each final sub-command.
  2. load the static completion not only for the completion command like before, but also for the __complete command, so that when the __complete command is called for dynamic completion of a sub-command, it is aware of the sub-command in question.

If applicable

  • this PR has been tested for backwards compatibility

@helm-bot helm-bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Apr 12, 2020
@marckhouzam
marckhouzam force-pushed the fix/compPluginUpdate branch from d7771c0 to 29af2c0 Compare May 14, 2020 12:13
@marckhouzam

Copy link
Copy Markdown
Member Author

This PR is a blocker for contribution #8111, but also for 3 or 4 improvements I have tucked away. Basically the following stuff is blocked:

  • Fish completion
  • Dropping the internal package “completion”
  • Improved custom completion (adding descriptions)
  • Upcoming improved Zsh completion (with descriptions)

Cobra 1.0.0 introduces support for Custom Go Completions.  However,
there is a small difference compared to helm's existing solution.
The difference is that Cobra will not complete sub-commands if there
are any dynamic completion functions registered.

The way helm implemented plugin completions had the dynamic completion
function associated with the root plugin command (e.g. 2to3); with
Cobra, this prevents any of the sub-commands of the plugin to be
completed (i.e., 2to3 <TAB> will not show the static sub-commands).

The solution is to only register the dynamic completion function for a
plugin command that doesn't have any further sub-commands.  This allows
all sub-commands to be completed properly, and when there are no more
levels of sub-commands, the dynamic completion is called.

This commit had to make two changes to achieve this:
1- refactor the load_plugins.go file to extract the logic to call
   the plugin.complete executable (for dynamic completions), so that
   it could be added to each final sub-command.
2- load the static completion not only for the "completion" command like
   before, but also for the "__complete" command, so that when the
   __complete command is called for dynamic completion of a sub-command,
   it is aware of the sub-command in question.

The second change is also valuable for the future support of completion
for the Fish shell.  Completion for the fish shell uses the __complete
command not only for dynamic completions but also for basic command and
flag completion. This implies that the __complete command must be aware
of the plugins sub-commands, and therefore that the plugin's static
completion be loaded.

Signed-off-by: Marc Khouzam <[email protected]>
@marckhouzam
marckhouzam force-pushed the fix/compPluginUpdate branch from 29af2c0 to cdb416c Compare May 14, 2020 16:32

@scottrigby scottrigby 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.

Thanks for the detailed chat. Updated description a bit per our convo. No change to end users or plugin maintainers. Also congrats on those changes to spf13/cobra. Looks good! 👍

@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

@marckhouzam

Copy link
Copy Markdown
Member Author

Thanks @scottrigby and @bacongobbler for the review!

@marckhouzam
marckhouzam merged commit 0366f99 into helm:master May 15, 2020
@marckhouzam
marckhouzam deleted the fix/compPluginUpdate branch May 15, 2020 00:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants