Skip to content

Commit 9d0b249

Browse files
committed
Add 'editor' as a known config value to cobra and viper
'editor' configuration now appears in the viper config output when requested and shows the default configured editor.
1 parent 89e02e3 commit 9d0b249

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

cmd/root.go

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"encoding/json"
55
"fmt"
6+
"github.com/msp301/zb/editor"
67
"github.com/msp301/zb/graph"
78
"github.com/spf13/cobra"
89
"github.com/spf13/viper"
@@ -12,6 +13,7 @@ import (
1213
)
1314

1415
var cfgFile string
16+
var cfgEditor string
1517

1618
var rootCmd = &cobra.Command{
1719
Use: "zb",
@@ -39,8 +41,10 @@ func init() {
3941
cobra.OnInitialize(initConfig)
4042

4143
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.zb.toml)")
44+
rootCmd.PersistentFlags().StringVar(&cfgEditor, "editor", editor.FindEditor(), "program to open notes with")
4245
rootCmd.PersistentFlags().StringSlice("directory", []string{defaultNotebookDir()}, "notebook directories")
4346
viper.BindPFlag("directory", rootCmd.PersistentFlags().Lookup("directory"))
47+
viper.BindPFlag("editor", rootCmd.PersistentFlags().Lookup("editor"))
4448
}
4549

4650
func initConfig() {

editor/editor.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"os/exec"
77
)
88

9-
func findEditor() string {
9+
func FindEditor() string {
1010
editor := viper.GetString("editor")
1111
if editor != "" {
1212
return editor
@@ -21,7 +21,7 @@ func findEditor() string {
2121
}
2222

2323
func Open(path string) error {
24-
editor := findEditor()
24+
editor := FindEditor()
2525

2626
command := exec.Command(editor, path)
2727
command.Stdin = os.Stdin

0 commit comments

Comments
 (0)