Implement completion for "kops get clusters"

This commit is contained in:
John Gardiner Myers 2021-07-19 14:31:25 -07:00
parent c68da24d21
commit abd95d14f4
16 changed files with 35 additions and 30 deletions

View File

@ -71,7 +71,7 @@ func NewCmdDeleteCluster(f *util.Factory, out io.Writer) *cobra.Command {
Long: deleteClusterLong, Long: deleteClusterLong,
Example: deleteClusterExample, Example: deleteClusterExample,
Args: rootCommand.clusterNameArgsNoKubeconfig(&options.ClusterName), Args: rootCommand.clusterNameArgsNoKubeconfig(&options.ClusterName),
ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, true), ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, true, false),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return RunDeleteCluster(context.TODO(), f, out, options) return RunDeleteCluster(context.TODO(), f, out, options)
}, },

View File

@ -79,7 +79,7 @@ func NewCmdEditCluster(f *util.Factory, out io.Writer) *cobra.Command {
Long: editClusterLong, Long: editClusterLong,
Example: editClusterExample, Example: editClusterExample,
Args: rootCommand.clusterNameArgs(&options.ClusterName), Args: rootCommand.clusterNameArgs(&options.ClusterName),
ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, true), ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, true, false),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return RunEditCluster(context.TODO(), f, out, options) return RunEditCluster(context.TODO(), f, out, options)
}, },

View File

@ -88,7 +88,7 @@ func NewCmdExportKubeconfig(f *util.Factory, out io.Writer) *cobra.Command {
return rootCommand.clusterNameArgs(&options.ClusterName)(cmd, args) return rootCommand.clusterNameArgs(&options.ClusterName)(cmd, args)
} }
}, },
ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, true), ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, true, false),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return RunExportKubeconfig(context.TODO(), f, out, options, args) return RunExportKubeconfig(context.TODO(), f, out, options, args)
}, },

View File

@ -75,7 +75,7 @@ func NewCmdGet(f *util.Factory, out io.Writer) *cobra.Command {
Long: getLong, Long: getLong,
Example: getExample, Example: getExample,
Args: rootCommand.clusterNameArgs(&options.ClusterName), Args: rootCommand.clusterNameArgs(&options.ClusterName),
ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, true), ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, true, false),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return RunGet(context.TODO(), &rootCommand, out, options) return RunGet(context.TODO(), &rootCommand, out, options)
}, },

View File

@ -89,7 +89,7 @@ func NewCmdGetAssets(f *util.Factory, out io.Writer, getOptions *GetOptions) *co
Long: getAssetsLong, Long: getAssetsLong,
Example: getAssetsExample, Example: getAssetsExample,
Args: rootCommand.clusterNameArgs(&options.ClusterName), Args: rootCommand.clusterNameArgs(&options.ClusterName),
ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, true), ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, true, false),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return RunGetAssets(context.TODO(), f, out, &options) return RunGetAssets(context.TODO(), f, out, &options)
}, },

View File

@ -90,29 +90,26 @@ func NewCmdGetCluster(f *util.Factory, out io.Writer, getOptions *GetOptions) *c
} }
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "clusters", Use: "clusters [CLUSTER]...",
Aliases: []string{"cluster"}, Aliases: []string{"cluster"},
Short: getClusterShort, Short: getClusterShort,
Long: getClusterLong, Long: getClusterLong,
Example: getClusterExample, Example: getClusterExample,
Run: func(cmd *cobra.Command, args []string) { Args: func(cmd *cobra.Command, args []string) error {
ctx := context.TODO()
if len(args) != 0 { if len(args) != 0 {
options.ClusterNames = append(options.ClusterNames, args...)
}
if rootCommand.clusterName != "" { if rootCommand.clusterName != "" {
if len(args) != 0 { return fmt.Errorf("cannot mix --name for cluster with positional arguments")
exitWithError(fmt.Errorf("cannot mix --name for cluster with positional arguments")) }
options.ClusterNames = append(options.ClusterNames, args...)
} else {
options.ClusterNames = append(options.ClusterNames, rootCommand.ClusterName(true))
} }
options.ClusterNames = append(options.ClusterNames, rootCommand.clusterName) return nil
} },
ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, false, true),
err := RunGetClusters(ctx, &rootCommand, os.Stdout, &options) RunE: func(cmd *cobra.Command, args []string) error {
if err != nil { return RunGetClusters(context.TODO(), &rootCommand, os.Stdout, &options)
exitWithError(err)
}
}, },
} }

View File

@ -167,7 +167,7 @@ func NewCmdRollingUpdateCluster(f *util.Factory, out io.Writer) *cobra.Command {
Long: rollingupdateLong, Long: rollingupdateLong,
Example: rollingupdateExample, Example: rollingupdateExample,
Args: rootCommand.clusterNameArgs(&options.ClusterName), Args: rootCommand.clusterNameArgs(&options.ClusterName),
ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, true), ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, true, false),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return RunRollingUpdateCluster(context.TODO(), f, out, &options) return RunRollingUpdateCluster(context.TODO(), f, out, &options)
}, },

View File

@ -140,7 +140,7 @@ func NewCmdRoot(f *util.Factory, out io.Writer) *cobra.Command {
defaultClusterName := os.Getenv("KOPS_CLUSTER_NAME") defaultClusterName := os.Getenv("KOPS_CLUSTER_NAME")
cmd.PersistentFlags().StringVarP(&rootCommand.clusterName, "name", "", defaultClusterName, "Name of cluster. Overrides KOPS_CLUSTER_NAME environment variable") cmd.PersistentFlags().StringVarP(&rootCommand.clusterName, "name", "", defaultClusterName, "Name of cluster. Overrides KOPS_CLUSTER_NAME environment variable")
cmd.RegisterFlagCompletionFunc("name", commandutils.CompleteClusterName(&rootCommand, false)) cmd.RegisterFlagCompletionFunc("name", commandutils.CompleteClusterName(&rootCommand, false, false))
// create subcommands // create subcommands
cmd.AddCommand(NewCmdCreate(f, out)) cmd.AddCommand(NewCmdCreate(f, out))

View File

@ -82,7 +82,7 @@ func NewCmdToolboxDump(f *util.Factory, out io.Writer) *cobra.Command {
Long: toolboxDumpLong, Long: toolboxDumpLong,
Example: toolboxDumpExample, Example: toolboxDumpExample,
Args: rootCommand.clusterNameArgs(&options.ClusterName), Args: rootCommand.clusterNameArgs(&options.ClusterName),
ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, true), ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, true, false),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return RunToolboxDump(context.TODO(), f, out, options) return RunToolboxDump(context.TODO(), f, out, options)
}, },

View File

@ -86,7 +86,7 @@ func NewCmdToolboxTemplate(f *util.Factory, out io.Writer) *cobra.Command {
Long: toolboxTemplatingLong, Long: toolboxTemplatingLong,
Example: toolboxTemplatingExample, Example: toolboxTemplatingExample,
Args: rootCommand.clusterNameArgs(&options.ClusterName), Args: rootCommand.clusterNameArgs(&options.ClusterName),
ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, true), ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, true, false),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return RunToolBoxTemplate(f, out, options) return RunToolBoxTemplate(f, out, options)
}, },

View File

@ -106,7 +106,7 @@ func NewCmdUpdateCluster(f *util.Factory, out io.Writer) *cobra.Command {
Long: updateClusterLong, Long: updateClusterLong,
Example: updateClusterExample, Example: updateClusterExample,
Args: rootCommand.clusterNameArgs(&options.ClusterName), Args: rootCommand.clusterNameArgs(&options.ClusterName),
ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, true), ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, true, false),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
_, err := RunUpdateCluster(context.TODO(), f, out, options) _, err := RunUpdateCluster(context.TODO(), f, out, options)
return err return err

View File

@ -70,7 +70,7 @@ func NewCmdUpgradeCluster(f *util.Factory, out io.Writer) *cobra.Command {
Long: upgradeClusterLong, Long: upgradeClusterLong,
Example: upgradeClusterExample, Example: upgradeClusterExample,
Args: rootCommand.clusterNameArgs(&options.ClusterName), Args: rootCommand.clusterNameArgs(&options.ClusterName),
ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, true), ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, true, false),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.TODO() ctx := context.TODO()

View File

@ -83,7 +83,7 @@ func NewCmdValidateCluster(f *util.Factory, out io.Writer) *cobra.Command {
Long: validateClusterLong, Long: validateClusterLong,
Example: validateClusterExample, Example: validateClusterExample,
Args: rootCommand.clusterNameArgs(&options.ClusterName), Args: rootCommand.clusterNameArgs(&options.ClusterName),
ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, true), ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, true, false),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
result, err := RunValidateCluster(context.TODO(), f, out, options) result, err := RunValidateCluster(context.TODO(), f, out, options)
if err != nil { if err != nil {

View File

@ -10,7 +10,7 @@ Get one or many clusters.
Display one or many cluster resources. Display one or many cluster resources.
``` ```
kops get clusters [flags] kops get clusters [CLUSTER]... [flags]
``` ```
### Examples ### Examples

View File

@ -16,6 +16,7 @@ go_library(
"//pkg/client/simple:go_default_library", "//pkg/client/simple:go_default_library",
"//vendor/github.com/spf13/cobra:go_default_library", "//vendor/github.com/spf13/cobra:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/sets:go_default_library",
"//vendor/k8s.io/klog/v2:go_default_library", "//vendor/k8s.io/klog/v2:go_default_library",
], ],
) )

View File

@ -21,10 +21,11 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
) )
// CompleteClusterName returns a Cobra completion function for cluster names. // CompleteClusterName returns a Cobra completion function for cluster names.
func CompleteClusterName(f Factory, suppressIfArgs bool) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { func CompleteClusterName(f Factory, suppressIfArgs bool, suppressArgs bool) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if suppressIfArgs && len(args) > 0 { if suppressIfArgs && len(args) > 0 {
return nil, cobra.ShellCompDirectiveNoFileComp return nil, cobra.ShellCompDirectiveNoFileComp
@ -43,9 +44,15 @@ func CompleteClusterName(f Factory, suppressIfArgs bool) func(cmd *cobra.Command
} }
var clusterNames []string var clusterNames []string
alreadySelected := sets.NewString()
if suppressArgs {
alreadySelected = alreadySelected.Insert(args...)
}
for _, cluster := range list.Items { for _, cluster := range list.Items {
if !alreadySelected.Has(cluster.Name) {
clusterNames = append(clusterNames, cluster.Name) clusterNames = append(clusterNames, cluster.Name)
} }
}
return clusterNames, cobra.ShellCompDirectiveNoFileComp return clusterNames, cobra.ShellCompDirectiveNoFileComp
} }