Skip to content

Commit 1fa53d0

Browse files
authored
feat: support auto completion (#102)
* feat: support auto completion Signed-off-by: hantmac <hantmac@outlook.com> * add readme Signed-off-by: hantmac <hantmac@outlook.com> --------- Signed-off-by: hantmac <hantmac@outlook.com>
1 parent cfd9de2 commit 1fa53d0

File tree

4 files changed

+73
-3
lines changed

4 files changed

+73
-3
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,27 @@ Same to `install manually`.
4646

4747
## Usage
4848

49+
### completion
50+
```bash
51+
To load auto completions:
52+
53+
Bash:
54+
$ source <(kubectl-kruise completion bash)
55+
56+
Zsh:
57+
# If shell completion is not already enabled in your environment,
58+
# you will need to enable it. You can execute the following once:
59+
$ echo "autoload -U compinit; compinit" >> ~/.zshrc
60+
61+
# To load completions for each session, execute once:
62+
$ kubectl-kruise completion zsh > "${fpath[1]}/_kubectl-kruise"
63+
64+
Fish:
65+
$ kubectl-kruise completion fish | source
66+
67+
PowerShell:
68+
PS> kubectl-kruise completion powershell | Out-String | Invoke-Expression
69+
4970
### expose
5071

5172
Take a workload(e.g. deployment, cloneset), service or pod and expose it as a new Kubernetes Service.

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,6 @@ github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:
294294
github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
295295
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
296296
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
297-
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
298297
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
299298
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
300299
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
@@ -447,7 +446,6 @@ github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTd
447446
github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
448447
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
449448
github.com/spf13/cobra v1.2.1/go.mod h1:ExllRjgxM/piMAM+3tAZvg8fsklGAf3tPfi+i8t68Nk=
450-
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
451449
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
452450
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
453451
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=

pkg/cmd/auto_completion.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/spf13/cobra"
8+
)
9+
10+
func NewAutoCompleteCommand() *cobra.Command {
11+
var completionCmd = &cobra.Command{
12+
Use: "completion [bash|zsh|fish|powershell]",
13+
Short: "Generate completion script",
14+
Long: `To load completions:
15+
16+
Bash:
17+
$ source <(kubectl-kruise completion bash)
18+
19+
Zsh:
20+
# If shell completion is not already enabled in your environment,
21+
# you will need to enable it. You can execute the following once:
22+
$ echo "autoload -U compinit; compinit" >> ~/.zshrc
23+
24+
# To load completions for each session, execute once:
25+
$ kubectl-kruise completion zsh > "${fpath[1]}/_kubectl-kruise"
26+
27+
Fish:
28+
$ kubectl-kruise completion fish | source
29+
30+
PowerShell:
31+
PS> kubectl-kruise completion powershell | Out-String | Invoke-Expression
32+
`,
33+
RunE: func(cmd *cobra.Command, args []string) error {
34+
switch args[0] {
35+
case "bash":
36+
return cmd.Root().GenBashCompletion(os.Stdout)
37+
case "zsh":
38+
return cmd.Root().GenZshCompletion(os.Stdout)
39+
case "fish":
40+
return cmd.Root().GenFishCompletion(os.Stdout, true)
41+
case "powershell":
42+
return cmd.Root().GenPowerShellCompletion(os.Stdout)
43+
default:
44+
return fmt.Errorf("unsupported shell type %q", args[0])
45+
}
46+
},
47+
}
48+
return completionCmd
49+
}

pkg/cmd/cmd.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ import (
2121
"io"
2222
"os"
2323

24+
"github.com/spf13/cobra"
25+
2426
"github.com/openkruise/kruise-tools/pkg/cmd/create"
2527
cmdexec "github.com/openkruise/kruise-tools/pkg/cmd/exec"
2628
"github.com/openkruise/kruise-tools/pkg/cmd/expose"
2729
"github.com/openkruise/kruise-tools/pkg/cmd/migrate"
2830
krollout "github.com/openkruise/kruise-tools/pkg/cmd/rollout"
2931
"github.com/openkruise/kruise-tools/pkg/cmd/scaledown"
3032
kset "github.com/openkruise/kruise-tools/pkg/cmd/set"
31-
"github.com/spf13/cobra"
3233

3334
"k8s.io/cli-runtime/pkg/genericclioptions"
3435
_ "k8s.io/client-go/plugin/pkg/client/auth"
@@ -441,6 +442,7 @@ func NewKubectlCommand(in io.Reader, out, err io.Writer) *cobra.Command {
441442

442443
cmds.AddCommand(alpha)
443444
cmds.AddCommand(NewCmdGenerateDocs(f, ioStreams))
445+
cmds.AddCommand(NewAutoCompleteCommand())
444446
cmds.AddCommand(cmdconfig.NewCmdConfig(clientcmd.NewDefaultPathOptions(), ioStreams))
445447
cmds.AddCommand(plugin.NewCmdPlugin(ioStreams))
446448
cmds.AddCommand(version.NewCmdVersion(f, ioStreams))

0 commit comments

Comments
 (0)