Use Cobra for shell completion

This commit is contained in:
Asbjørn Apeland
2020-10-16 01:26:59 +02:00
parent 7e04be9ec6
commit 9d0fba81f0
8 changed files with 51 additions and 98 deletions

View File

@@ -1,72 +0,0 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package complete
import (
"os"
"strings"
"github.com/posener/complete/v2"
"github.com/posener/complete/v2/predict"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/commands"
"sigs.k8s.io/kustomize/kyaml/errors"
)
// NewCommand returns a new install-completion command
func NewCommand() *cobra.Command {
return &cobra.Command{
Use: "install-completion",
Short: commands.CompletionShort,
Long: commands.CompletionLong,
PreRunE: preRunE,
Run: run,
}
}
func preRunE(cmd *cobra.Command, args []string) error {
// install by default
if os.Getenv("COMP_INSTALL") == "" {
if err := errors.Wrap(os.Setenv("COMP_INSTALL", "1")); err != nil {
return err
}
}
return nil
}
func run(cmd *cobra.Command, args []string) {
// find the root command
for cmd.Parent() != nil {
cmd = cmd.Parent()
}
// do completion
Complete(cmd).Complete("kustomize")
}
// Complete returns a completion command for a cobra command
func Complete(cmd *cobra.Command) *complete.Command {
cc := &complete.Command{
Flags: map[string]complete.Predictor{},
Sub: map[string]*complete.Command{},
}
if strings.Contains(cmd.Use, "DIR") {
// if usage contains directory, then use a file predictor
cc.Args = predict.Dirs("*")
}
// add completion for each subcommand
for i := range cmd.Commands() {
c := cmd.Commands()[i]
name := strings.Split(c.Use, " ")[0]
cc.Sub[name] = Complete(c)
}
// add completion for each flag
cmd.Flags().VisitAll(func(flag *pflag.Flag) {
cc.Flags[flag.Name] = predict.Nothing
})
return cc
}

View File

@@ -0,0 +1,33 @@
// Copyright 2020 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package completion
import (
"os"
"github.com/spf13/cobra"
)
func NewCommand() *cobra.Command {
return &cobra.Command{
Use: "completion [bash|zsh|fish|powershell]",
Short: "Generate shell completion script",
Long: "Generate shell completion.",
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
Args: cobra.ExactValidArgs(1),
Run: func(cmd *cobra.Command, args []string) {
switch args[0] {
case "bash":
cmd.Root().GenBashCompletion(os.Stdout)
case "zsh":
cmd.Root().GenZshCompletion(os.Stdout)
case "fish":
cmd.Root().GenFishCompletion(os.Stdout, true)
case "powershell":
cmd.Root().GenPowerShellCompletion(os.Stdout)
}
},
}
}

View File

@@ -1,20 +1,25 @@
## install-completion
## completion
Install shell completion.
Generate shell completion.
### Synopsis
Install shell completion for kustomize commands and flags -- supports bash, fish and zsh.
Generate shell completion for `kustomize` -- supports bash, zsh, fish and powershell.
kustomize install-completion
### Examples
Registers the completion command with known shells (e.g. .bashrc, .bash_profile, etc):
# load completion for Bash
source <(kustomize completion bash)
complete -C /Users/USER/go/bin/kustomize kustomize
# install for Bash in Linux
kustomize completion bash > /etc/bash_completion.d/kustomize
Because the completion command is embedded in kustomize directly, there is no need to update
it separately from the kustomize binary.
# install for Bash in MacOS
kustomize completion bash > /usr/local/etc/bash_completion.d/kustomize
To uninstall shell completion run:
# package for Bash
kustomize completion bash > /usr/share/bash-completion/completions/kustomize
# package for zsh
kustomize completion zsh > /usr/share/zsh/site-functions/_kustomize
COMP_UNINSTALL=1 kustomize install-completion

View File

@@ -9,7 +9,6 @@ require (
github.com/kr/text v0.2.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/olekukonko/tablewriter v0.0.4
github.com/posener/complete/v2 v2.0.1-alpha.12
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.6.1

View File

@@ -230,10 +230,6 @@ github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete/v2 v2.0.1-alpha.12 h1:0wvkuDfHb5vSZlNBYgpEH4XQHpF46MjLPHav8XC77Nc=
github.com/posener/complete/v2 v2.0.1-alpha.12/go.mod h1://JlL91cS2JV7rOl6LVHrRqBXoBUecJu3ILQPgbJiMQ=
github.com/posener/script v1.0.4 h1:nSuXW5ZdmFnQIueLB2s0qvs4oNsUloM1Zydzh75v42w=
github.com/posener/script v1.0.4/go.mod h1:Rg3ijooqulo05aGLyGsHoLmIOUzHUVK19WVgrYBPU/E=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=