2015-05-25 02:00:21 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
|
2015-12-11 03:35:48 +00:00
|
|
|
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/spf13/cobra"
|
2016-01-04 18:26:32 +00:00
|
|
|
|
|
|
|
"github.com/khlieng/dispatch/storage"
|
2015-05-25 02:00:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
configCmd = &cobra.Command{
|
|
|
|
Use: "config",
|
|
|
|
Short: "Edit config file",
|
2015-05-25 03:15:32 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2015-05-25 02:00:21 +00:00
|
|
|
if editor := findEditor(); editor != "" {
|
2016-01-04 18:26:32 +00:00
|
|
|
process := exec.Command(editor, storage.Path.Config())
|
2015-05-25 03:15:32 +00:00
|
|
|
process.Stdin = os.Stdin
|
|
|
|
process.Stdout = os.Stdout
|
|
|
|
process.Stderr = os.Stderr
|
|
|
|
process.Run()
|
2015-05-25 02:00:21 +00:00
|
|
|
} else {
|
|
|
|
log.Println("Unable to locate editor")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2015-05-28 23:59:08 +00:00
|
|
|
|
|
|
|
editors = []string{"nano", "notepad", "vi", "emacs"}
|
2015-05-25 02:00:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func findEditor() string {
|
|
|
|
if editor := os.Getenv("EDITOR"); editor != "" {
|
|
|
|
editor, err := exec.LookPath(editor)
|
|
|
|
if err == nil {
|
|
|
|
return editor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, editor := range editors {
|
|
|
|
editor, err := exec.LookPath(editor)
|
|
|
|
if err == nil {
|
|
|
|
return editor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|