refactor(config): use shellwords to parse editor commands (#838)
This commit is contained in:
parent
f983dbff0d
commit
bbb8cf0d65
6 changed files with 25 additions and 24 deletions
1
go.mod
1
go.mod
|
|
@ -22,6 +22,7 @@ require (
|
|||
github.com/google/go-cmp v0.6.0
|
||||
github.com/gorilla/websocket v1.5.3
|
||||
github.com/klauspost/compress v1.20.0
|
||||
github.com/mattn/go-shellwords v1.0.15
|
||||
github.com/ncruces/zenity v0.10.15
|
||||
github.com/rivo/uniseg v0.4.7
|
||||
github.com/sahilm/fuzzy v0.1.3
|
||||
|
|
|
|||
2
go.sum
2
go.sum
|
|
@ -68,6 +68,8 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
|
|||
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
||||
github.com/lucasb-eyer/go-colorful v1.4.1 h1:1EO+WB73+EH8EVbzlrG3KLAfEypQWVHIBqlTf+2hNss=
|
||||
github.com/lucasb-eyer/go-colorful v1.4.1/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||
github.com/mattn/go-shellwords v1.0.15 h1:rx0n8+ZdM9JWZMlr2BMPAjtLU0rfluLNtwMC2FJOTtY=
|
||||
github.com/mattn/go-shellwords v1.0.15/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
|
||||
github.com/ncruces/zenity v0.10.15 h1:UaHekkxwt7QjoZmMWtzhqSEtQT97/Cux6FDBWwfyuqk=
|
||||
github.com/ncruces/zenity v0.10.15/go.mod h1:45d81tt+vF/UjBjlWE/vvE0oD0hWuHUQld4vPbA4VxE=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
|
|
|
|||
19
internal/config/editor.go
Normal file
19
internal/config/editor.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os/exec"
|
||||
|
||||
"github.com/mattn/go-shellwords"
|
||||
)
|
||||
|
||||
func (cfg *Config) EditorCommand(path string) (*exec.Cmd, error) {
|
||||
args, err := shellwords.Parse(cfg.Editor)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(args) == 0 {
|
||||
return nil, errors.New("editor command is empty")
|
||||
}
|
||||
return exec.Command(args[0], append(args[1:], path)...), nil
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
//go:build !unix
|
||||
|
||||
package config
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
func (cfg *Config) CreateEditorCommand(path string) *exec.Cmd {
|
||||
return exec.Command(cfg.Editor, path)
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
//go:build unix
|
||||
|
||||
package config
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
func (cfg *Config) CreateEditorCommand(path string) *exec.Cmd {
|
||||
return exec.Command("sh", "-c", cfg.Editor+" \"$@\"", cfg.Editor, path)
|
||||
}
|
||||
|
|
@ -829,8 +829,9 @@ func (c *composer) editor() tview.Cmd {
|
|||
_, _ = file.WriteString(text)
|
||||
_ = file.Close()
|
||||
|
||||
cmd := cfg.CreateEditorCommand(name)
|
||||
if cmd == nil {
|
||||
cmd, err := cfg.EditorCommand(name)
|
||||
if err != nil {
|
||||
slog.Error("failed to create editor command", "err", err)
|
||||
return nil
|
||||
}
|
||||
cmd.Stdin = os.Stdin
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue