mirror of
https://github.com/mickael-menu/zk
synced 2024-11-03 23:15:49 +00:00
46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
package note
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"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/exec"
|
|
"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.CommandFromString(editor.String() + " '" + path + "'")
|
|
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"))
|
|
}
|