upup: add edit command

This commit is contained in:
Justin Santa Barbara 2016-06-27 09:12:22 -04:00
parent a0d8302255
commit 4704846051
2 changed files with 97 additions and 0 deletions

16
upup/cmd/upup/edit.go Normal file
View File

@ -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)
}

View File

@ -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
}