Skip to content

Commit

Permalink
Adds the PowerShell completion generation (#103758)
Browse files Browse the repository at this point in the history
* Adds the powershell completion generation

* Fixes formatting based on verification script

* Changes generation to include descriptions

* Adjusts formatting and Adds startup information

* Fix build
  • Loading branch information
zikhan committed Aug 26, 2021
1 parent cbd0611 commit dec8528
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions staging/src/k8s.io/kubectl/pkg/cmd/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
}
)

Expand Down Expand Up @@ -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)
}

0 comments on commit dec8528

Please sign in to comment.