Implement completion for "kops get"

This commit is contained in:
John Gardiner Myers 2021-07-13 20:42:07 -07:00
parent 14d58a4e87
commit c312c19dcb
14 changed files with 50 additions and 79 deletions

View File

@ -39,10 +39,7 @@ var (
Display one or many resources.` + validResources))
getExample = templates.Examples(i18n.T(`
# Get all clusters in a state store
kops get clusters
# Get a cluster and its instancegroups
# Get a cluster and its instance groups
kops get k8s-cluster.example.com
# Get a cluster and its instancegroups' YAML desired configuration
@ -50,19 +47,14 @@ var (
# Save a cluster and its instancegroups' desired configuration to YAML file
kops get k8s-cluster.example.com -o yaml > cluster-desired-config.yaml
# Get a secret
kops get secrets kube -oplaintext
# Get the admin password for a cluster
kops get secrets admin -oplaintext`))
`))
getShort = i18n.T(`Get one or many resources.`)
)
type GetOptions struct {
output string
clusterName string
ClusterName string
Output string
}
const (
@ -73,38 +65,26 @@ const (
func NewCmdGet(f *util.Factory, out io.Writer) *cobra.Command {
options := &GetOptions{
output: OutputTable,
Output: OutputTable,
}
cmd := &cobra.Command{
Use: "get",
SuggestFor: []string{"list"},
Short: getShort,
Long: getLong,
Example: getExample,
Run: func(cmd *cobra.Command, args []string) {
ctx := context.TODO()
if len(args) != 0 {
options.clusterName = args[0]
}
if rootCommand.clusterName != "" {
if len(args) != 0 {
exitWithError(fmt.Errorf("cannot mix --name for cluster with positional arguments"))
}
options.clusterName = rootCommand.clusterName
}
err := RunGet(ctx, &rootCommand, os.Stdout, options)
if err != nil {
exitWithError(err)
}
Use: "get",
SuggestFor: []string{"list"},
Short: getShort,
Long: getLong,
Example: getExample,
Args: rootCommand.clusterNameArgs(&options.ClusterName),
ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, true),
RunE: func(cmd *cobra.Command, args []string) error {
return RunGet(context.TODO(), &rootCommand, out, options)
},
}
cmd.PersistentFlags().StringVarP(&options.output, "output", "o", options.output, "output format. One of: table, yaml, json")
cmd.PersistentFlags().StringVarP(&options.Output, "output", "o", options.Output, "output format. One of: table, yaml, json")
cmd.RegisterFlagCompletionFunc("output", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"table", "json", "yaml"}, cobra.ShellCompDirectiveNoFileComp
})
// create subcommands
cmd.AddCommand(NewCmdGetAssets(f, out, options))
@ -124,7 +104,7 @@ func RunGet(ctx context.Context, f commandutils.Factory, out io.Writer, options
return err
}
cluster, err := client.GetCluster(ctx, options.clusterName)
cluster, err := client.GetCluster(ctx, options.ClusterName)
if err != nil {
return err
}
@ -148,14 +128,14 @@ func RunGet(ctx context.Context, f commandutils.Factory, out io.Writer, options
}
var obj []runtime.Object
if options.output != OutputTable {
if options.Output != OutputTable {
obj = append(obj, cluster)
for _, group := range instancegroups {
obj = append(obj, group)
}
}
switch options.output {
switch options.Output {
case OutputYaml:
if err := fullOutputYAML(out, obj...); err != nil {
return fmt.Errorf("error writing cluster yaml to stdout: %v", err)
@ -170,19 +150,19 @@ func RunGet(ctx context.Context, f commandutils.Factory, out io.Writer, options
return nil
case OutputTable:
fmt.Fprintf(os.Stdout, "Cluster\n")
fmt.Fprintf(out, "Cluster\n")
err = clusterOutputTable([]*api.Cluster{cluster}, out)
if err != nil {
return err
}
fmt.Fprintf(os.Stdout, "\nInstance Groups\n")
fmt.Fprintf(out, "\nInstance Groups\n")
err = igOutputTable(cluster, instancegroups, out)
if err != nil {
return err
}
default:
return fmt.Errorf("Unknown output format: %q", options.output)
return fmt.Errorf("Unknown output format: %q", options.Output)
}
return nil

View File

@ -99,7 +99,7 @@ func NewCmdGetAssets(f *util.Factory, out io.Writer, getOptions *GetOptions) *co
func RunGetAssets(ctx context.Context, f *util.Factory, out io.Writer, options *GetAssetsOptions) error {
clusterName := rootCommand.ClusterName(true)
options.clusterName = clusterName
options.ClusterName = clusterName
if clusterName == "" {
return fmt.Errorf("--name is required")
}
@ -150,7 +150,7 @@ func RunGetAssets(ctx context.Context, f *util.Factory, out io.Writer, options *
}
}
switch options.output {
switch options.Output {
case OutputTable:
if err = imageOutputTable(result.Images, out); err != nil {
return err
@ -173,7 +173,7 @@ func RunGetAssets(ctx context.Context, f *util.Factory, out io.Writer, options *
return fmt.Errorf("error writing to output: %v", err)
}
default:
return fmt.Errorf("unsupported output format: %q", options.output)
return fmt.Errorf("unsupported output format: %q", options.Output)
}
return nil

View File

@ -168,13 +168,13 @@ func RunGetClusters(ctx context.Context, f commandutils.Factory, out io.Writer,
}
var obj []runtime.Object
if options.output != OutputTable {
if options.Output != OutputTable {
for _, c := range clusters {
obj = append(obj, c)
}
}
switch options.output {
switch options.Output {
case OutputTable:
return clusterOutputTable(clusters, out)
case OutputYaml:
@ -182,7 +182,7 @@ func RunGetClusters(ctx context.Context, f commandutils.Factory, out io.Writer,
case OutputJSON:
return fullOutputJSON(out, obj...)
default:
return fmt.Errorf("Unknown output format: %q", options.output)
return fmt.Errorf("Unknown output format: %q", options.Output)
}
}

View File

@ -116,13 +116,13 @@ func RunGetInstanceGroups(ctx context.Context, options *GetInstanceGroupsOptions
}
var obj []runtime.Object
if options.output != OutputTable {
if options.Output != OutputTable {
for _, c := range instancegroups {
obj = append(obj, c)
}
}
switch options.output {
switch options.Output {
case OutputTable:
return igOutputTable(cluster, instancegroups, out)
case OutputYaml:
@ -130,7 +130,7 @@ func RunGetInstanceGroups(ctx context.Context, options *GetInstanceGroupsOptions
case OutputJSON:
return fullOutputJSON(out, obj...)
default:
return fmt.Errorf("Unknown output format: %q", options.output)
return fmt.Errorf("Unknown output format: %q", options.Output)
}
}

View File

@ -84,18 +84,18 @@ func RunGetInstances(ctx context.Context, f *util.Factory, out io.Writer, option
}
clusterName := rootCommand.ClusterName(true)
options.clusterName = clusterName
options.ClusterName = clusterName
if clusterName == "" {
return fmt.Errorf("--name is required")
}
cluster, err := clientset.GetCluster(ctx, options.clusterName)
cluster, err := clientset.GetCluster(ctx, options.ClusterName)
if err != nil {
return err
}
if cluster == nil {
return fmt.Errorf("cluster not found %q", options.clusterName)
return fmt.Errorf("cluster not found %q", options.ClusterName)
}
cloud, err := cloudup.BuildCloud(cluster)
@ -137,11 +137,11 @@ func RunGetInstances(ctx context.Context, f *util.Factory, out io.Writer, option
cg.AdjustNeedUpdate()
}
switch options.output {
switch options.Output {
case OutputTable:
return instanceOutputTable(cloudInstances, out)
default:
return fmt.Errorf("unsupported output format: %q", options.output)
return fmt.Errorf("unsupported output format: %q", options.Output)
}
}

View File

@ -144,7 +144,7 @@ func RunGetKeypairs(ctx context.Context, out io.Writer, options *GetKeypairsOpti
if len(items) == 0 {
return fmt.Errorf("no keypairs found")
}
switch options.output {
switch options.Output {
case OutputTable:
t := &tables.Table{}
@ -191,7 +191,7 @@ func RunGetKeypairs(ctx context.Context, out io.Writer, options *GetKeypairsOpti
return fmt.Errorf("json output format is not (currently) supported for keypairs")
default:
return fmt.Errorf("Unknown output format: %q", options.output)
return fmt.Errorf("Unknown output format: %q", options.Output)
}
}

View File

@ -193,7 +193,7 @@ func RunGetSecrets(ctx context.Context, options *GetSecretsOptions, args []strin
if len(items) == 0 {
return fmt.Errorf("No secrets found")
}
switch options.output {
switch options.Output {
case OutputTable:
@ -239,6 +239,6 @@ func RunGetSecrets(ctx context.Context, options *GetSecretsOptions, args []strin
return nil
default:
return fmt.Errorf("Unknown output format: %q", options.output)
return fmt.Errorf("Unknown output format: %q", options.Output)
}
}

13
docs/cli/kops_get.md generated
View File

@ -20,10 +20,7 @@ kops get [flags]
### Examples
```
# Get all clusters in a state store
kops get clusters
# Get a cluster and its instancegroups
# Get a cluster and its instance groups
kops get k8s-cluster.example.com
# Get a cluster and its instancegroups' YAML desired configuration
@ -31,19 +28,13 @@ kops get [flags]
# Save a cluster and its instancegroups' desired configuration to YAML file
kops get k8s-cluster.example.com -o yaml > cluster-desired-config.yaml
# Get a secret
kops get secrets kube -oplaintext
# Get the admin password for a cluster
kops get secrets admin -oplaintext
```
### Options
```
-h, --help help for get
-o, --output string output format. One of: table, yaml, json (default "table")
-o, --output string output format. One of: table, yaml, json (default "table")
```
### Options inherited from parent commands

View File

@ -40,7 +40,7 @@ kops get assets [flags]
--logtostderr log to standard error instead of files (default true)
--name string Name of cluster. Overrides KOPS_CLUSTER_NAME environment variable
--one_output If true, only write logs to their native severity level (vs also writing to each lower severity level)
-o, --output string output format. One of: table, yaml, json (default "table")
-o, --output string output format. One of: table, yaml, json (default "table")
--skip_headers If true, avoid header prefixes in the log messages
--skip_log_headers If true, avoid headers when opening log files
--state string Location of state storage (kops 'config' file). Overrides KOPS_STATE_STORE environment variable

View File

@ -49,7 +49,7 @@ kops get clusters [flags]
--logtostderr log to standard error instead of files (default true)
--name string Name of cluster. Overrides KOPS_CLUSTER_NAME environment variable
--one_output If true, only write logs to their native severity level (vs also writing to each lower severity level)
-o, --output string output format. One of: table, yaml, json (default "table")
-o, --output string output format. One of: table, yaml, json (default "table")
--skip_headers If true, avoid header prefixes in the log messages
--skip_log_headers If true, avoid headers when opening log files
--state string Location of state storage (kops 'config' file). Overrides KOPS_STATE_STORE environment variable

View File

@ -45,7 +45,7 @@ kops get instancegroups [flags]
--logtostderr log to standard error instead of files (default true)
--name string Name of cluster. Overrides KOPS_CLUSTER_NAME environment variable
--one_output If true, only write logs to their native severity level (vs also writing to each lower severity level)
-o, --output string output format. One of: table, yaml, json (default "table")
-o, --output string output format. One of: table, yaml, json (default "table")
--skip_headers If true, avoid header prefixes in the log messages
--skip_log_headers If true, avoid headers when opening log files
--state string Location of state storage (kops 'config' file). Overrides KOPS_STATE_STORE environment variable

View File

@ -39,7 +39,7 @@ kops get instances [flags]
--logtostderr log to standard error instead of files (default true)
--name string Name of cluster. Overrides KOPS_CLUSTER_NAME environment variable
--one_output If true, only write logs to their native severity level (vs also writing to each lower severity level)
-o, --output string output format. One of: table, yaml, json (default "table")
-o, --output string output format. One of: table, yaml, json (default "table")
--skip_headers If true, avoid header prefixes in the log messages
--skip_log_headers If true, avoid headers when opening log files
--state string Location of state storage (kops 'config' file). Overrides KOPS_STATE_STORE environment variable

View File

@ -39,7 +39,7 @@ kops get keypairs [KEYSET]... [flags]
--logtostderr log to standard error instead of files (default true)
--name string Name of cluster. Overrides KOPS_CLUSTER_NAME environment variable
--one_output If true, only write logs to their native severity level (vs also writing to each lower severity level)
-o, --output string output format. One of: table, yaml, json (default "table")
-o, --output string output format. One of: table, yaml, json (default "table")
--skip_headers If true, avoid header prefixes in the log messages
--skip_log_headers If true, avoid headers when opening log files
--state string Location of state storage (kops 'config' file). Overrides KOPS_STATE_STORE environment variable

View File

@ -43,7 +43,7 @@ kops get secrets [flags]
--logtostderr log to standard error instead of files (default true)
--name string Name of cluster. Overrides KOPS_CLUSTER_NAME environment variable
--one_output If true, only write logs to their native severity level (vs also writing to each lower severity level)
-o, --output string output format. One of: table, yaml, json (default "table")
-o, --output string output format. One of: table, yaml, json (default "table")
--skip_headers If true, avoid header prefixes in the log messages
--skip_log_headers If true, avoid headers when opening log files
--state string Location of state storage (kops 'config' file). Overrides KOPS_STATE_STORE environment variable