mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-14 10:30:59 +00:00
shell completion for kustomize commands
This commit is contained in:
68
cmd/config/cmd/complete/complete.go
Normal file
68
cmd/config/cmd/complete/complete.go
Normal file
@@ -0,0 +1,68 @@
|
||||
// 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/cmddocs/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{},
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
@@ -21,6 +21,23 @@ var CatExamples = `
|
||||
# unwrap Resource config from a directory in an ResourceList
|
||||
... | kustomize config cat`
|
||||
|
||||
var CompletionShort = `Install shell completion.`
|
||||
var CompletionLong = `
|
||||
Install shell completion for kustomize commands and flags -- supports bash, fish and zsh.
|
||||
|
||||
kustomize install-completion
|
||||
|
||||
Registers the completion command with known shells (e.g. .bashrc, .bash_profile, etc):
|
||||
|
||||
complete -C /Users/USER/go/bin/kustomize kustomize
|
||||
|
||||
Because the completion command is embedded in kustomize directly, there is no need to update
|
||||
it separately from the kustomize binary.
|
||||
|
||||
To uninstall shell completion run:
|
||||
|
||||
COMP_UNINSTALL=1 kustomize install-completion`
|
||||
|
||||
var CountShort = `[Alpha] Count Resources Config from a local directory.`
|
||||
var CountLong = `
|
||||
[Alpha] Count Resources Config from a local directory.
|
||||
@@ -123,7 +140,7 @@ var RunFnsShort = `[Alpha] Reoncile config functions to Resources.`
|
||||
var RunFnsLong = `
|
||||
[Alpha] Reconcile config functions to Resources.
|
||||
|
||||
run sequentially invokes all config functions in the directly, providing Resources
|
||||
run sequentially invokes all config functions in the directory, providing Resources
|
||||
in the directory as input to the first function, and writing the output of the last
|
||||
function back to the directory.
|
||||
|
||||
@@ -140,7 +157,7 @@ order they appear in the file).
|
||||
#### Config Functions:
|
||||
|
||||
Config functions are specified as Kubernetes types containing a metadata.configFn.container.image
|
||||
field. This fields tells run how to invoke the container.
|
||||
field. This field tells run how to invoke the container.
|
||||
|
||||
Example config function:
|
||||
|
||||
@@ -160,7 +177,7 @@ order they appear in the file).
|
||||
In the preceding example, 'kustomize config run example/' would identify the function by
|
||||
the metadata.configFn field. It would then write all Resources in the directory to
|
||||
a container stdin (running the gcr.io/example/examplefunction:v1.0.1 image). It
|
||||
would then writer the container stdout back to example/, replacing the directory
|
||||
would then write the container stdout back to example/, replacing the directory
|
||||
file contents.
|
||||
|
||||
See ` + "`" + `kustomize config help docs-fn` + "`" + ` for more details on writing functions.
|
||||
|
||||
20
cmd/config/docs/commands/completion.md
Normal file
20
cmd/config/docs/commands/completion.md
Normal file
@@ -0,0 +1,20 @@
|
||||
## install-completion
|
||||
|
||||
Install shell completion.
|
||||
|
||||
### Synopsis
|
||||
|
||||
Install shell completion for kustomize commands and flags -- supports bash, fish and zsh.
|
||||
|
||||
kustomize install-completion
|
||||
|
||||
Registers the completion command with known shells (e.g. .bashrc, .bash_profile, etc):
|
||||
|
||||
complete -C /Users/USER/go/bin/kustomize kustomize
|
||||
|
||||
Because the completion command is embedded in kustomize directly, there is no need to update
|
||||
it separately from the kustomize binary.
|
||||
|
||||
To uninstall shell completion run:
|
||||
|
||||
COMP_UNINSTALL=1 kustomize install-completion
|
||||
@@ -4,8 +4,9 @@ go 1.13
|
||||
|
||||
require (
|
||||
github.com/go-errors/errors v1.0.1
|
||||
github.com/posener/complete/v2 v2.0.1-alpha.12
|
||||
github.com/spf13/cobra v0.0.5
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/spf13/pflag v1.0.5
|
||||
github.com/stretchr/testify v1.4.0
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
|
||||
sigs.k8s.io/kustomize/kyaml v0.0.0
|
||||
|
||||
@@ -54,6 +54,10 @@ github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
|
||||
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=
|
||||
github.com/gophercloud/gophercloud v0.6.0/go.mod h1:GICNByuaEBibcjmjvI7QvYJSZEbGkcYwAR7EZK2WMqM=
|
||||
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
|
||||
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
|
||||
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||
github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uPribsnS6o=
|
||||
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
|
||||
github.com/hashicorp/golang-lru v0.5.3/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
|
||||
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
|
||||
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
||||
@@ -94,6 +98,10 @@ 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/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYeAmZ5ZIpBWTGllZSQnw97Dj+woV0toclVaRGI8pc=
|
||||
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
|
||||
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
|
||||
|
||||
Reference in New Issue
Block a user