Add config file, handle it with Viper, add a command to open it in an editor
This commit is contained in:
parent
b8a8ba2e08
commit
61aa5880d3
234 changed files with 44636 additions and 383 deletions
48
commands/config.go
Normal file
48
commands/config.go
Normal file
|
@ -0,0 +1,48 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"syscall"
|
||||
|
||||
"github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/spf13/cobra"
|
||||
|
||||
"github.com/khlieng/name_pending/storage"
|
||||
)
|
||||
|
||||
var (
|
||||
editors = []string{"nano", "notepad", "vi", "emacs"}
|
||||
|
||||
configCmd = &cobra.Command{
|
||||
Use: "config",
|
||||
Short: "Edit config file",
|
||||
Run: func(cmd *cobra.Command, _ []string) {
|
||||
if editor := findEditor(); editor != "" {
|
||||
args := []string{editor, path.Join(storage.AppDir, "config.toml")}
|
||||
syscall.Exec(editor, args, os.Environ())
|
||||
} else {
|
||||
log.Println("Unable to locate editor")
|
||||
}
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
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 ""
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue