split EDITOR in Go to remove dependency on sh (ref #19)

This commit is contained in:
itchyny 2023-04-21 22:12:21 +09:00
parent f627dff093
commit 7c8adbbb4c
3 changed files with 15 additions and 1 deletions

View file

@ -9,6 +9,7 @@ import (
"runtime"
"strings"
"github.com/kballard/go-shellquote"
_ "github.com/mattn/getwild"
"github.com/mattn/go-tty"
@ -98,7 +99,17 @@ func rename(args []string) error {
}
defer tty.Close()
cmd := exec.Command("sh", "-c", `eval exec "${EDITOR:-vi}" '"$@"'`, "", f.Name())
editor := os.Getenv("EDITOR")
if editor == "" {
editor = "vi"
}
editorWithArgs, err := shellquote.Split(editor)
if err != nil {
return fmt.Errorf("%s: %s", err, editor)
}
editorWithArgs = append(editorWithArgs, f.Name())
cmd := exec.Command(editorWithArgs[0], editorWithArgs[1:]...)
cmd.Stdin = tty.Input()
cmd.Stdout = tty.Output()
cmd.Stderr = tty.Output()

1
go.mod
View file

@ -3,6 +3,7 @@ module github.com/itchyny/mmv
go 1.20
require (
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/mattn/getwild v0.0.2-0.20200919000855-c2e221927ad6
github.com/mattn/go-tty v0.0.4
)

2
go.sum
View file

@ -1,3 +1,5 @@
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/mattn/getwild v0.0.2-0.20200919000855-c2e221927ad6 h1:uWR+2CTTaHQzDS/DApbJ2H8UEPQl90atrKtczXj2xcs=
github.com/mattn/getwild v0.0.2-0.20200919000855-c2e221927ad6/go.mod h1:AG+GKQydHp7iLJn+VV+D7y8LeYs5bQ0Xz4fmKd5o1Sg=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=