Adds the PowerShell completion generation (#103758)

* Adds the powershell completion generation

* Fixes formatting based on verification script

* Changes generation to include descriptions

* Adjusts formatting and Adds startup information

* Fix build
This commit is contained in:
Zovin Khanmohammed 2021-08-25 22:29:23 -05:00 committed by GitHub
parent cbd0611d49
commit dec8528aba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -44,7 +44,7 @@ const defaultBoilerPlate = `
var (
completionLong = templates.LongDesc(i18n.T(`
Output shell completion code for the specified shell (bash, zsh or fish).
Output shell completion code for the specified shell (bash, zsh, fish, or powershell).
The shell code must be evaluated to provide interactive
completion of kubectl commands. This can be done by sourcing it from
the .bash_profile.
@ -95,14 +95,28 @@ var (
# Load the kubectl completion code for fish[2] into the current shell
kubectl completion fish | source
# To load completions for each session, execute once:
kubectl completion fish > ~/.config/fish/completions/kubectl.fish`))
kubectl completion fish > ~/.config/fish/completions/kubectl.fish
# Load the kubectl completion code for powershell into the current shell
kubectl completion powershell | Out-String | Invoke-Expression
# Set kubectl completion code for powershell to run on startup
## Save completion code to a script and execute in the profile
kubectl completion powershell > $HOME\.kube\completion.ps1
Add-Content $PROFILE "$HOME\.kube\completion.ps1"
## Execute completion code in the profile
Add-Content $PROFILE "if (Get-Command kubectl -ErrorAction SilentlyContinue) {
kubectl completion powershell | Out-String | Invoke-Expression
}"
## Add completion code directly to the $PROFILE script
kubectl completion powershell >> $PROFILE`))
)
var (
completionShells = map[string]func(out io.Writer, boilerPlate string, cmd *cobra.Command) error{
"bash": runCompletionBash,
"zsh": runCompletionZsh,
"fish": runCompletionFish,
"bash": runCompletionBash,
"zsh": runCompletionZsh,
"fish": runCompletionFish,
"powershell": runCompletionPwsh,
}
)
@ -179,3 +193,15 @@ func runCompletionFish(out io.Writer, boilerPlate string, kubectl *cobra.Command
return kubectl.GenFishCompletion(out, true)
}
func runCompletionPwsh(out io.Writer, boilerPlate string, kubectl *cobra.Command) error {
if len(boilerPlate) == 0 {
boilerPlate = defaultBoilerPlate
}
if _, err := out.Write([]byte(boilerPlate)); err != nil {
return err
}
return kubectl.GenPowerShellCompletionWithDesc(out)
}