New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds the PowerShell completion generation #103758
Adds the PowerShell completion generation #103758
Conversation
Welcome @zikhan! |
Hi @zikhan. Thanks for your PR. I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/ok-to-test
The integration failure looks to be a flaky test. I'm thinking it's related to #103512. Someone should correct me if I'm wrong. Just gonna rerun and see if it magically doesn't timeout. /retest |
350e65e
to
5e474b6
Compare
/retest |
return err | ||
} | ||
|
||
return kubectl.GenPowerShellCompletion(out) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want to have descriptions for the completion choices, likes zsh has, you can instead use GenPowerShellCompletionWithDesc()
.
See https://github.com/spf13/cobra/blob/master/shell_completions.md#powershell-completions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the comment. I don't typically use the "Menu Complete" completion to see those descriptions, but I'm sure other people will appreciate it. I went ahead and updated the method.
/retest |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/lgtm
Thanks!
kubectl completion zsh > "${fpath[1]}/_kubectl" | ||
|
||
# Load the kubectl completion code for powershell into the current shell | ||
kubectl completion powershell | Out-String | Invoke-Expression`)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Could you make the indentation depth consistent with the above text?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just out of curiosity, could we save the completion code to a dedicated file so that powershell could load the code on startup?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This just came from cobra's example here: https://github.com/spf13/cobra/blob/master/shell_completions.md#creating-your-own-completion-command.
You can save the completion string to a .ps1 file, but you still need to run it from the $PROFILE. I've typically just shoved the command into my $PROFILE to make the mobilty a bit easier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected the indentation.
I also added some more options for auto-loading the powershell completion code on startup.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great!
/priority important-longterm |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: eddiezane, knight42, zikhan The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
e925e74
to
00f9042
Compare
/label tide/merge-method-squash |
/retest |
/lgtm |
/hold cancel |
* Adds the powershell completion generation * Fixes formatting based on verification script * Changes generation to include descriptions * Adjusts formatting and Adds startup information * Fix build
Hi all, Maybe this would be better served as a new issue, but I was wondering how this can be used with PowerShell aliases. I've downloaded sal k kubectl I see on this documentation page that you can add autocomplete aliases in Bash with P.S. This third-party PowerShell module does handle aliases correctly: https://github.com/mziyabo/PSKubectlCompletion Thanks for any help! |
@micahmo Looks like the cobra library is not registering the alias's for the command in the generated script, that can be found here: https://github.com/mziyabo/PSKubectlCompletion/blob/f6f28153a616e14789a3a7fbcb493ff7f1f1ae85/PSKubectlCompletion.psm1#L16-L20 $aliases = (get-alias).Where( { $_.Definition -in $rootCommands }).Name;
if ($aliases) {
$rootCommands += $aliases
}
Register-ArgumentCompleter -CommandName $rootCommands -ScriptBlock $ArgumentCompleter -Native There is a bit of a workaround that I tried. If you take the output from the I was able to get the workaround to a single line command that you can use. (kubectl completion powershell | Out-String) -replace "CommandName 'kubectl'","CommandName 'kubectl','k'" | iex |
Thanks for the response @zikhan! Your workaround works perfectly for me. Do you think it would be worth adding it to the documentation? There's already precedent for documenting additional alias-related commands, and this isn't that much more hacky than in Bash. :-) |
@zikhan Should supporting aliases for powershell take a similar approach to what
If that is the right solution for powershell, then Cobra needs to put the script block in a variable, like you have done in your scripts. I can take care of that if that is the proper solution (I'm not familiar with powershell much). I like how you automated detecting aliases. However I worry it may not always work and that it would be simpler to teach the user what to do. For example, |
I haven't had much time to play around get-alias to clean up the scripts, but I did notice it allowed for wildcard ala |
What type of PR is this?
/kind feature
What this PR does / why we need it:
This PR adds PowerShell completion generation via the Cobra package. This is a quality-of-life improvement for users of kubectl in PowerShell.
Which issue(s) this PR fixes:
Fixes kubernetes/kubectl#613
Special notes for your reviewer:
#92989 modifies the same file. If that PR merges first, I'll rebase my changes.
Does this PR introduce a user-facing change?
Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:
No Docs yet, however, I will open a separate PR on the kubernetes/website repo to update the Install and Set Up kubectl on Windows docs to reflect the new PowerShell completion feature.