diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/completion/completion.go b/staging/src/k8s.io/kubectl/pkg/cmd/completion/completion.go index 9036effe3e5..5a2839c662b 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/completion/completion.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/completion/completion.go @@ -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) +}