Skip to content

Commit

Permalink
Add PowerShell code completion
Browse files Browse the repository at this point in the history
Signed-off-by: Oldřich Jedlička <oldium.pro@gmail.com>
  • Loading branch information
oldium committed Apr 5, 2023
1 parent 08896fd commit c221588
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
42 changes: 40 additions & 2 deletions cmd/minikube/cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"k8s.io/minikube/pkg/minikube/reason"
)

const longDescription = `Outputs minikube shell completion for the given shell (bash, zsh or fish)
const longDescription = `Outputs minikube shell completion for the given shell (bash, zsh, fish or powershell)
This depends on the bash-completion binary. Example installation instructions:
OS X:
Expand All @@ -47,6 +47,16 @@ const longDescription = `Outputs minikube shell completion for the given shell (
Additionally, you may want to output the completion to a file and source in your .bashrc
Windows:
## Save completion code to a script and execute in the profile
PS> minikube completion powershell > $HOME\.minikube-completion.ps1
PS> Add-Content $PROFILE ". ` + "`" + `$HOME\.minikube-completion.ps1"
## Execute completion code in the profile
PS> Add-Content $PROFILE "if (Get-Command minikube -ErrorAction SilentlyContinue) {
minikube completion powershell | Out-String | Invoke-Expression
}"
Note for zsh users: [1] zsh completions are only supported in versions of zsh >= 5.2
Note for fish users: [2] please refer to this docs for more details https://fishshell.com/docs/current/#tab-completion
`
Expand Down Expand Up @@ -75,7 +85,7 @@ var completionCmd = &cobra.Command{
if len(args) != 1 {
exit.Message(reason.Usage, "Usage: minikube completion SHELL")
}
if args[0] != "bash" && args[0] != "zsh" && args[0] != "fish" {
if args[0] != "bash" && args[0] != "zsh" && args[0] != "fish" && args[0] != "powershell" {
exit.Message(reason.Usage, "Sorry, completion support is not yet implemented for {{.name}}", out.V{"name": args[0]})
}
},
Expand Down Expand Up @@ -117,10 +127,23 @@ var fishCmd = &cobra.Command{
},
}

var powershellCmd = &cobra.Command{
Use: "powershell",
Short: "powershell completion.",
Long: "Generate command completion for PowerShell.",
Run: func(cmd *cobra.Command, args []string) {
err := GeneratePowerShellCompletion(os.Stdout, cmd.Root())
if err != nil {
exit.Error(reason.InternalCompletion, "powershell completion failed", err)
}
},
}

func init() {
completionCmd.AddCommand(bashCmd)
completionCmd.AddCommand(zshCmd)
completionCmd.AddCommand(fishCmd)
completionCmd.AddCommand(powershellCmd)
}

// GenerateBashCompletion generates the completion for the bash shell
Expand Down Expand Up @@ -329,3 +352,18 @@ func GenerateFishCompletion(w io.Writer, cmd *cobra.Command) error {

return nil
}

// GeneratePowerShellCompletion generates the completion for the PowerShell
func GeneratePowerShellCompletion(w io.Writer, cmd *cobra.Command) error {
_, err := w.Write([]byte(boilerPlate))
if err != nil {
return err
}

err = cmd.GenPowerShellCompletionWithDesc(w)
if err != nil {
return errors.Wrap(err, "Error generating powershell completion")
}

return nil
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ require (
github.com/pmezard/go-difflib v1.0.0
github.com/samalba/dockerclient v0.0.0-20160414174713-91d7393ff859 // indirect
github.com/shirou/gopsutil/v3 v3.23.2
github.com/spf13/cobra v1.6.1
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.15.0
github.com/zchee/go-vmnet v0.0.0-20161021174912-97ebf9174097
Expand Down Expand Up @@ -157,7 +157,7 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1005,8 +1005,8 @@ github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/intel-go/cpuid v0.0.0-20181003105527-1a4a6f06a1c6 h1:XboatR7lasl05yel5hNXF7kQBw2oFUGdMztcgisfhNU=
github.com/intel-go/cpuid v0.0.0-20181003105527-1a4a6f06a1c6/go.mod h1:RmeVYf9XrPRbRc3XIx0gLYA8qOFvNoPOfaEZduRlEp4=
github.com/intel/goresctrl v0.2.0/go.mod h1:+CZdzouYFn5EsxgqAQTEzMfwKwuc0fVdMrT9FCCAVRQ=
Expand Down Expand Up @@ -1463,8 +1463,8 @@ github.com/spf13/cobra v0.0.2-0.20171109065643-2da4a54c5cee/go.mod h1:1l0Ry5zgKv
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
github.com/spf13/cobra v1.1.3/go.mod h1:pGADOWyqRD/YMrPZigI/zbliZ2wVD/23d+is3pSWzOo=
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk=
github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo=
Expand Down

0 comments on commit c221588

Please sign in to comment.