You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
zk/core/note/edit.go

46 lines
1.0 KiB
Go

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