Add completion to the --output/-o flag

For example:
$ kubectl get -o json<TAB>
json   jsonpath   jsonpath-as-json  jsonpath-file

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>

Kubernetes-commit: 5f22baeaf1f662d0fe90ea41e453883cb726323c
This commit is contained in:
Marc Khouzam 2021-10-22 15:15:23 -04:00 committed by Kubernetes Publisher
parent 0b81755902
commit b9e380aef5
1 changed files with 13 additions and 0 deletions

View File

@ -26,6 +26,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/printers"
"k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util/openapi"
)
@ -162,6 +163,18 @@ func (f *PrintFlags) AddFlags(cmd *cobra.Command) {
if f.OutputFormat != nil {
cmd.Flags().StringVarP(f.OutputFormat, "output", "o", *f.OutputFormat, fmt.Sprintf("Output format. One of: %s See custom columns [https://kubernetes.io/docs/reference/kubectl/overview/#custom-columns], golang template [http://golang.org/pkg/text/template/#pkg-overview] and jsonpath template [https://kubernetes.io/docs/reference/kubectl/jsonpath/].", strings.Join(f.AllowedFormats(), "|")))
util.CheckErr(cmd.RegisterFlagCompletionFunc(
"output",
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var comps []string
for _, format := range f.AllowedFormats() {
if strings.HasPrefix(format, toComplete) {
comps = append(comps, format)
}
}
return comps, cobra.ShellCompDirectiveNoFileComp
},
))
}
if f.NoHeaders != nil {
cmd.Flags().BoolVar(f.NoHeaders, "no-headers", *f.NoHeaders, "When using the default or custom-column output format, don't print headers (default print headers).")