From 33368cc4e59f6fbb6535812d1a5da90336f55977 Mon Sep 17 00:00:00 2001 From: Stefan Fast Date: Fri, 4 Mar 2022 19:00:52 +0100 Subject: [PATCH 1/2] Read arguments of command in EDITOR env variable and pass them --- internal/cmd/edit.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/internal/cmd/edit.go b/internal/cmd/edit.go index 5a3163b..7a26147 100644 --- a/internal/cmd/edit.go +++ b/internal/cmd/edit.go @@ -6,6 +6,7 @@ import ( "os" "os/exec" "path/filepath" + "strings" "time" "github.com/spf13/cobra" @@ -34,7 +35,15 @@ func newEditCmd() *editCmd { } log.Printf("%s %s\n", editor, tmp) - edit := exec.Command(editor, tmp) + + editorArgs := []string{tmp} + if strings.ContainsAny(editor, " ") { + editorParts := strings.Split(editor, " ") + editor = editorParts[0] + editorArgs = append(editorArgs, editorParts[1:]...) + } + + edit := exec.Command(editor, editorArgs...) edit.Stderr = os.Stderr edit.Stdout = os.Stdout edit.Stdin = os.Stdin From 8d2a6dcb908ad3aa8e08f98653d8b11beea13cd4 Mon Sep 17 00:00:00 2001 From: Stefan Fast Date: Sat, 5 Mar 2022 10:27:32 +0100 Subject: [PATCH 2/2] fix: Improved solution by making use of strings.Fields --- internal/cmd/edit.go | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/internal/cmd/edit.go b/internal/cmd/edit.go index 7a26147..a8dc9b7 100644 --- a/internal/cmd/edit.go +++ b/internal/cmd/edit.go @@ -2,7 +2,6 @@ package cmd import ( "fmt" - "log" "os" "os/exec" "path/filepath" @@ -29,21 +28,19 @@ func newEditCmd() *editCmd { return err } - editor := os.Getenv("EDITOR") - if editor == "" { + editor := strings.Fields(os.Getenv("EDITOR")) + if len(editor) == 0 { return fmt.Errorf("no $EDITOR set") } - log.Printf("%s %s\n", editor, tmp) - - editorArgs := []string{tmp} - if strings.ContainsAny(editor, " ") { - editorParts := strings.Split(editor, " ") - editor = editorParts[0] - editorArgs = append(editorArgs, editorParts[1:]...) + editorCmd := editor[0] + var editorArgs []string + if len(editor) > 1 { + editorArgs = append(editorArgs, editor[1:]...) } + editorArgs = append(editorArgs, tmp) - edit := exec.Command(editor, editorArgs...) + edit := exec.Command(editorCmd, editorArgs...) edit.Stderr = os.Stderr edit.Stdout = os.Stdout edit.Stdin = os.Stdin