mirror of https://github.com/mickael-menu/zk
Edit newly created notes
parent
4dad3a2309
commit
6831823e5d
@ -0,0 +1,45 @@
|
|||||||
|
package note
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
|
||||||
|
"github.com/kballard/go-shellquote"
|
||||||
|
"github.com/mickael-menu/zk/core/zk"
|
||||||
|
"github.com/mickael-menu/zk/util/errors"
|
||||||
|
"github.com/mickael-menu/zk/util/opt"
|
||||||
|
osutil "github.com/mickael-menu/zk/util/os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Edit starts the editor with the note at given path.
|
||||||
|
func Edit(zk *zk.Zk, path string) error {
|
||||||
|
editor := editor(zk)
|
||||||
|
if editor.IsNull() {
|
||||||
|
return fmt.Errorf("no editor set in config")
|
||||||
|
}
|
||||||
|
|
||||||
|
wrap := errors.Wrapperf("failed to launch editor: %v", editor)
|
||||||
|
|
||||||
|
args, err := shellquote.Split(editor.String())
|
||||||
|
if err != nil {
|
||||||
|
return wrap(err)
|
||||||
|
}
|
||||||
|
if len(args) == 0 {
|
||||||
|
return wrap(fmt.Errorf("editor command is not valid: %v", editor))
|
||||||
|
}
|
||||||
|
args = append(args, path)
|
||||||
|
|
||||||
|
cmd := exec.Command(args[0], args[1:]...)
|
||||||
|
cmd.Stdin = os.Stdin
|
||||||
|
cmd.Stdout = os.Stdout
|
||||||
|
|
||||||
|
return wrap(cmd.Run())
|
||||||
|
}
|
||||||
|
|
||||||
|
// editor returns the editor command to use to edit a note.
|
||||||
|
func editor(zk *zk.Zk) opt.String {
|
||||||
|
return zk.Config.Editor.
|
||||||
|
Or(osutil.GetOptEnv("VISUAL")).
|
||||||
|
Or(osutil.GetOptEnv("EDITOR"))
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
package note
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/mickael-menu/zk/core/zk"
|
||||||
|
"github.com/mickael-menu/zk/util/assert"
|
||||||
|
"github.com/mickael-menu/zk/util/opt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEditorUsesUserConfigFirst(t *testing.T) {
|
||||||
|
os.Setenv("VISUAL", "editor")
|
||||||
|
zk := zk.Zk{Config: zk.Config{Editor: opt.NewString("custom-editor")}}
|
||||||
|
|
||||||
|
assert.Equal(t, editor(&zk), opt.NewString("custom-editor"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEditorFallsbackOnVisual(t *testing.T) {
|
||||||
|
os.Setenv("VISUAL", "visual")
|
||||||
|
os.Setenv("EDITOR", "editor")
|
||||||
|
zk := zk.Zk{}
|
||||||
|
|
||||||
|
assert.Equal(t, editor(&zk), opt.NewString("visual"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEditorFallsbackOnEditor(t *testing.T) {
|
||||||
|
os.Unsetenv("VISUAL")
|
||||||
|
os.Setenv("EDITOR", "editor")
|
||||||
|
zk := zk.Zk{}
|
||||||
|
|
||||||
|
assert.Equal(t, editor(&zk), opt.NewString("editor"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEditorWhenUnset(t *testing.T) {
|
||||||
|
os.Unsetenv("VISUAL")
|
||||||
|
os.Unsetenv("EDITOR")
|
||||||
|
zk := zk.Zk{}
|
||||||
|
|
||||||
|
assert.Equal(t, editor(&zk), opt.NullString)
|
||||||
|
}
|
Loading…
Reference in New Issue