2021-01-02 17:01:30 +00:00
|
|
|
package note
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-02-07 16:48:23 +00:00
|
|
|
"strings"
|
2021-01-02 17:01:30 +00:00
|
|
|
|
2021-02-10 19:27:34 +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"
|
2021-02-07 16:48:23 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2021-02-10 19:27:34 +00:00
|
|
|
cmd := executil.CommandFromString(editor.String() + " " + shellquote.Join(paths...))
|
2021-01-02 17:01:30 +00:00
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stdout = os.Stdout
|
2021-02-07 16:48:23 +00:00
|
|
|
cmd.Stderr = os.Stderr
|
2021-01-02 17:01:30 +00:00
|
|
|
|
2021-02-07 16:48:23 +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 {
|
2021-02-20 20:45:36 +00:00
|
|
|
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"))
|
|
|
|
}
|