diff --git a/upup/cmd/upup/edit.go b/upup/cmd/upup/edit.go new file mode 100644 index 0000000000..98a5e22fba --- /dev/null +++ b/upup/cmd/upup/edit.go @@ -0,0 +1,16 @@ +package main + +import ( + "github.com/spf13/cobra" +) + +// editCmd represents the edit command +var editCmd = &cobra.Command{ + Use: "edit", + Short: "edit clusters", + Long: `edit clusters`, +} + +func init() { + rootCommand.AddCommand(editCmd) +} diff --git a/upup/cmd/upup/edit_cluster.go b/upup/cmd/upup/edit_cluster.go new file mode 100644 index 0000000000..d8126e0f11 --- /dev/null +++ b/upup/cmd/upup/edit_cluster.go @@ -0,0 +1,81 @@ +package main + +import ( + "fmt" + + "bytes" + "github.com/golang/glog" + "github.com/spf13/cobra" + "k8s.io/kubernetes/pkg/kubectl/cmd/util/editor" + "os" + "path/filepath" +) + +var editorEnvs = []string{"KUBE_EDITOR", "EDITOR"} + +type EditClusterCmd struct { +} + +var editClusterCmd EditClusterCmd + +func init() { + cmd := &cobra.Command{ + Use: "cluster", + Short: "Edit cluster", + Long: `Edit a cluster configuration.`, + Run: func(cmd *cobra.Command, args []string) { + err := editClusterCmd.Run() + if err != nil { + glog.Exitf("%v", err) + } + }, + } + + editCmd.AddCommand(cmd) +} + +func (c *EditClusterCmd) Run() error { + stateStore, err := rootCommand.StateStore() + if err != nil { + return err + } + + //cluster, _, err := api.ReadConfig(stateStore) + //if err != nil { + // return fmt.Errorf("error reading configuration: %v", err) + //} + + var ( + edit = editor.NewDefaultEditor(editorEnvs) + ) + + ext := "yaml" + + raw, err := stateStore.VFSPath().Join("config").ReadFile() + if err != nil { + return fmt.Errorf("error reading config file: %v", err) + } + + // launch the editor + edited, file, err := edit.LaunchTempFile(fmt.Sprintf("%s-edit-", filepath.Base(os.Args[0])), ext, bytes.NewReader(raw)) + defer func() { + if file != "" { + os.Remove(file) + } + }() + if err != nil { + return fmt.Errorf("error launching editor: %v", err) + } + + if bytes.Equal(edited, raw) { + fmt.Fprintln(os.Stderr, "Edit cancelled, no changes made.") + return nil + } + + err = stateStore.VFSPath().Join("config").WriteFile(edited) + if err != nil { + return fmt.Errorf("error writing config file: %v", err) + } + + return nil +}