Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement completion for "kops edit" commands #11980

Merged
merged 5 commits into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion cmd/kops/BUILD.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cmd/kops/create_instancegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func RunCreateInstanceGroup(ctx context.Context, f *util.Factory, out io.Writer,

if options.Edit {
var (
edit = editor.NewDefaultEditor(editorEnvs)
edit = editor.NewDefaultEditor(commandutils.EditorEnvs)
)

raw, err := kopscodecs.ToVersionedYaml(ig)
Expand Down
4 changes: 2 additions & 2 deletions cmd/kops/delete_instancegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ func NewCmdDeleteInstanceGroup(f *util.Factory, out io.Writer) *cobra.Command {
}

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]

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
Expand Down
27 changes: 2 additions & 25 deletions cmd/kops/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,12 @@ import (
"github.com/spf13/cobra"
"k8s.io/kops/cmd/kops/util"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
)

var (
editLong = templates.LongDesc(i18n.T(`Edit a resource configuration.
This command changes the desired configuration in the registry.

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.

kops edit does not update the cloud resources, to apply the changes use "kops update cluster".
`))

editExample = templates.Examples(i18n.T(`
# Edit a cluster configuration.
kops edit cluster k8s-cluster.example.com --state=s3://my-state-store

# Edit a instance group configuration.
kops edit ig --name k8s-cluster.example.com \
--state=s3://my-state-store nodes
`))
)

func NewCmdEdit(f *util.Factory, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "edit",
Short: i18n.T("Edit clusters and other resources."),
Long: editLong,
Example: editExample,
Use: "edit",
Short: i18n.T("Edit clusters and other resources."),
}

// create subcommands
Expand Down
49 changes: 22 additions & 27 deletions cmd/kops/edit_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ import (
"k8s.io/kops/pkg/apis/kops/validation"
"k8s.io/kops/pkg/assets"
"k8s.io/kops/pkg/commands"
"k8s.io/kops/pkg/commands/commandutils"
"k8s.io/kops/pkg/edit"
"k8s.io/kops/pkg/kopscodecs"
"k8s.io/kops/pkg/pretty"
"k8s.io/kops/pkg/try"
"k8s.io/kops/upup/pkg/fi/cloudup"
util_editor "k8s.io/kubectl/pkg/cmd/util/editor"
Expand All @@ -42,52 +44,45 @@ import (
)

type EditClusterOptions struct {
ClusterName string
}

var (
editClusterLong = templates.LongDesc(i18n.T(`Edit a cluster configuration.

This command changes the desired cluster configuration in the registry.

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.
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.

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 ` + pretty.Bash("kops update cluster") + `.`))

editClusterExample = templates.Examples(i18n.T(`
# Edit a cluster configuration in AWS.
kops edit cluster k8s.cluster.site --state=s3://my-state-store
# Edit a cluster configuration in AWS.
kops edit cluster k8s.cluster.site --state=s3://my-state-store
`))
)

func NewCmdEditCluster(f *util.Factory, out io.Writer) *cobra.Command {
options := &EditClusterOptions{}

cmd := &cobra.Command{
Use: "cluster",
Short: i18n.T("Edit cluster."),
Long: editClusterLong,
Example: editClusterExample,
Run: func(cmd *cobra.Command, args []string) {
ctx := context.TODO()

err := RunEditCluster(ctx, f, cmd, args, out, options)
if err != nil {
exitWithError(err)
}
Use: "cluster [CLUSTER]",
Short: i18n.T("Edit cluster."),
Long: editClusterLong,
Example: editClusterExample,
Args: rootCommand.clusterNameArgs(&options.ClusterName),
ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, true),
RunE: func(cmd *cobra.Command, args []string) error {
return RunEditCluster(context.TODO(), f, out, options)
},
}

return cmd
}

func RunEditCluster(ctx context.Context, f *util.Factory, cmd *cobra.Command, args []string, out io.Writer, options *EditClusterOptions) error {
err := rootCommand.ProcessArgs(args)
if err != nil {
return err
}

oldCluster, err := rootCommand.Cluster(ctx)
func RunEditCluster(ctx context.Context, f *util.Factory, out io.Writer, options *EditClusterOptions) error {
oldCluster, err := GetCluster(ctx, f, options.ClusterName)
if err != nil {
return err
}
Expand All @@ -108,7 +103,7 @@ func RunEditCluster(ctx context.Context, f *util.Factory, cmd *cobra.Command, ar
}

var (
editor = util_editor.NewDefaultEditor(editorEnvs)
editor = util_editor.NewDefaultEditor(commandutils.EditorEnvs)
)

ext := "yaml"
Expand Down Expand Up @@ -145,7 +140,7 @@ func RunEditCluster(ctx context.Context, f *util.Factory, cmd *cobra.Command, ar

if containsError {
if bytes.Equal(stripComments(editedDiff), stripComments(edited)) {
return preservedFile(fmt.Errorf("%s", "Edit cancelled, no valid changes were saved."), file, out)
return preservedFile(fmt.Errorf("%s", "Edit cancelled: no valid changes were saved."), file, out)
}
}

Expand All @@ -155,7 +150,7 @@ func RunEditCluster(ctx context.Context, f *util.Factory, cmd *cobra.Command, ar

if bytes.Equal(stripComments(raw), stripComments(edited)) {
try.RemoveFile(file)
fmt.Fprintln(out, "Edit cancelled, no changes made.")
fmt.Fprintln(out, "Edit cancelled: no changes made.")
return nil
}

Expand All @@ -165,7 +160,7 @@ func RunEditCluster(ctx context.Context, f *util.Factory, cmd *cobra.Command, ar
}
if !lines {
try.RemoveFile(file)
fmt.Fprintln(out, "Edit cancelled, saved file was empty.")
fmt.Fprintln(out, "Edit cancelled: saved file was empty.")
return nil
}

Expand Down
Loading