Implement completion for "kops edit instancegroup"

This commit is contained in:
John Gardiner Myers 2021-07-11 22:38:07 -07:00
parent ea9678573e
commit 6eda65d9f7
3 changed files with 37 additions and 32 deletions

View File

@ -75,13 +75,13 @@ func NewCmdDeleteInstanceGroup(f *util.Factory, out io.Writer) *cobra.Command {
} }
if len(args) == 0 { if len(args) == 0 {
return fmt.Errorf("must specify the name of instance group to delete") return fmt.Errorf("must specify the name of the instance group to delete")
} }
options.GroupName = args[0] options.GroupName = args[0]
if len(args) != 1 { if len(args) != 1 {
return fmt.Errorf("can only edit one instance group at a time") return fmt.Errorf("can only delete one instance group at a time")
} }
return nil return nil

View File

@ -44,55 +44,65 @@ var (
This command changes the instancegroup desired configuration in the registry. This command changes the instancegroup desired configuration in the registry.
To set your preferred editor, you can define the EDITOR environment variable. To set your preferred editor, you can define the EDITOR environment variable.
When you have done this, kOps will use the editor that you have set. When you have done this, kOps will use the editor that you have set.
kops edit does not update the cloud resources, to apply the changes use "kops update cluster".`)) kops edit does not update the cloud resources, to apply the changes use "kops update cluster".`))
editInstancegroupExample = templates.Examples(i18n.T(` editInstancegroupExample = templates.Examples(i18n.T(`
# Edit an instancegroup desired configuration. # Edit an instancegroup desired configuration.
kops edit ig --name k8s-cluster.example.com nodes --state=s3://my-state-store kops edit instancegroup --name k8s-cluster.example.com nodes --state=s3://my-state-store
`)) `))
editInstancegroupShort = i18n.T(`Edit instancegroup.`) editInstancegroupShort = i18n.T(`Edit instancegroup.`)
) )
type EditInstanceGroupOptions struct { type EditInstanceGroupOptions struct {
ClusterName string
GroupName string
} }
func NewCmdEditInstanceGroup(f *util.Factory, out io.Writer) *cobra.Command { func NewCmdEditInstanceGroup(f *util.Factory, out io.Writer) *cobra.Command {
options := &EditInstanceGroupOptions{} options := &EditInstanceGroupOptions{}
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "instancegroup", Use: "instancegroup INSTANCE_GROUP",
Aliases: []string{"instancegroups", "ig"}, Aliases: []string{"instancegroups", "ig"},
Short: editInstancegroupShort, Short: editInstancegroupShort,
Long: editInstancegroupLong, Long: editInstancegroupLong,
Example: editInstancegroupExample, Example: editInstancegroupExample,
Run: func(cmd *cobra.Command, args []string) { Args: func(cmd *cobra.Command, args []string) error {
ctx := context.TODO() options.ClusterName = rootCommand.ClusterName(true)
err := RunEditInstanceGroup(ctx, f, cmd, args, os.Stdout, options) if options.ClusterName == "" {
if err != nil { return fmt.Errorf("--name is required")
exitWithError(err)
} }
if len(args) == 0 {
return fmt.Errorf("must specify the name of the instance group to edit")
}
options.GroupName = args[0]
if len(args) != 1 {
return fmt.Errorf("can only edit one instance group at a time")
}
return nil
},
ValidArgsFunction: completeInstanceGroup(nil, nil),
RunE: func(cmd *cobra.Command, args []string) error {
return RunEditInstanceGroup(context.TODO(), f, out, options)
}, },
} }
return cmd return cmd
} }
func RunEditInstanceGroup(ctx context.Context, f *util.Factory, cmd *cobra.Command, args []string, out io.Writer, options *EditInstanceGroupOptions) error { func RunEditInstanceGroup(ctx context.Context, f *util.Factory, out io.Writer, options *EditInstanceGroupOptions) error {
if len(args) == 0 { groupName := options.GroupName
return fmt.Errorf("Specify name of instance group to edit")
}
if len(args) != 1 {
return fmt.Errorf("Can only edit one instance group at a time")
}
groupName := args[0] cluster, err := GetCluster(ctx, f, options.ClusterName)
cluster, err := rootCommand.Cluster(ctx)
if err != nil { if err != nil {
return err return err
} }
@ -102,15 +112,11 @@ func RunEditInstanceGroup(ctx context.Context, f *util.Factory, cmd *cobra.Comma
klog.Warningf("%v", err) klog.Warningf("%v", err)
} }
clientset, err := rootCommand.Clientset() clientset, err := f.Clientset()
if err != nil { if err != nil {
return err return err
} }
if groupName == "" {
return fmt.Errorf("name is required")
}
oldGroup, err := clientset.InstanceGroupsFor(cluster).Get(ctx, groupName, metav1.GetOptions{}) oldGroup, err := clientset.InstanceGroupsFor(cluster).Get(ctx, groupName, metav1.GetOptions{})
if err != nil { if err != nil {
return fmt.Errorf("error reading InstanceGroup %q: %v", groupName, err) return fmt.Errorf("error reading InstanceGroup %q: %v", groupName, err)
@ -141,7 +147,7 @@ func RunEditInstanceGroup(ctx context.Context, f *util.Factory, cmd *cobra.Comma
} }
if bytes.Equal(edited, raw) { if bytes.Equal(edited, raw) {
fmt.Fprintln(os.Stderr, "Edit cancelled, no changes made.") fmt.Fprintln(out, "Edit cancelled: no changes made.")
return nil return nil
} }

View File

@ -11,20 +11,19 @@ Edit a cluster configuration.
This command changes the instancegroup desired configuration in the registry. This command changes the instancegroup desired configuration in the registry.
To set your preferred editor, you can define the EDITOR environment variable. To set your preferred editor, you can define the EDITOR environment variable. When you have done this, kOps will use the editor that you have set.
When you have done this, kOps will use the editor that you have set.
kops edit does not update the cloud resources, to apply the changes use "kops update cluster". kops edit does not update the cloud resources, to apply the changes use "kops update cluster".
``` ```
kops edit instancegroup [flags] kops edit instancegroup INSTANCE_GROUP [flags]
``` ```
### Examples ### Examples
``` ```
# Edit an instancegroup desired configuration. # Edit an instancegroup desired configuration.
kops edit ig --name k8s-cluster.example.com nodes --state=s3://my-state-store kops edit instancegroup --name k8s-cluster.example.com nodes --state=s3://my-state-store
``` ```
### Options ### Options