Repository navigation
fix(comp): Prepare plugin completion for Cobra 1.0 - #7906
Merged
Merged
Conversation
marckhouzam
force-pushed
the
fix/compPluginUpdate
branch
from
May 14, 2020 12:13
d7771c0 to
29af2c0
Compare
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:
|
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
force-pushed
the
fix/compPluginUpdate
branch
from
May 14, 2020 16:32
29af2c0 to
cdb416c
Compare
scottrigby
approved these changes
May 14, 2020
scottrigby
left a comment
Member
There was a problem hiding this comment.
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! 👍
Member
Author
|
Thanks @scottrigby and @bacongobbler for the review! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
completioncommand like before, but also for the__completecommand, so that when the__completecommand is called for dynamic completion of a sub-command, it is aware of the sub-command in question.If applicable