zk/core/note/edit.go

38 lines
992 B
Go
Raw Normal View History

2021-01-02 17:01:30 +00:00
package note
import (
"fmt"
"os"
"strings"
2021-01-02 17:01:30 +00:00
"github.com/kballard/go-shellquote"
2021-01-02 17:01:30 +00:00
"github.com/mickael-menu/zk/core/zk"
"github.com/mickael-menu/zk/util/errors"
executil "github.com/mickael-menu/zk/util/exec"
2021-01-02 17:01:30 +00:00
"github.com/mickael-menu/zk/util/opt"
osutil "github.com/mickael-menu/zk/util/os"
)
2021-01-23 20:15:36 +00:00
// Edit starts the editor with the notes at given paths.
func Edit(zk *zk.Zk, paths ...string) error {
2021-01-02 17:01:30 +00:00
editor := editor(zk)
if editor.IsNull() {
return fmt.Errorf("no editor set in config")
}
cmd := executil.CommandFromString(editor.String() + " " + shellquote.Join(paths...))
2021-01-02 17:01:30 +00:00
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
2021-01-02 17:01:30 +00:00
return errors.Wrapf(cmd.Run(), "failed to launch editor: %s %s", editor, strings.Join(paths, " "))
2021-01-02 17:01:30 +00:00
}
// editor returns the editor command to use to edit a note.
func editor(zk *zk.Zk) opt.String {
return osutil.GetOptEnv("ZK_EDITOR").
Or(zk.Config.Tool.Editor).
2021-01-02 17:01:30 +00:00
Or(osutil.GetOptEnv("VISUAL")).
Or(osutil.GetOptEnv("EDITOR"))
}