Skip to content

fix: Error when using EDITOR environment variable with arguments #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 6, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion internal/cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"
"time"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -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...)
Copy link
Owner

@caarlos0 caarlos0 Mar 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can make this a bit nicer:

editor := strings.Fields(os.Getenv("EDITOR"))
if len(editor) == 0 {
  return fmt.Errorf("no $EDITOR set")
}

cmd := editor[0]
var args []string
if len(editor) > 1 {
  args = append(args, editor[1:])
}
args = append(args, tmp)

edit := exec.Command(cmd, args)
// ...

wdyt?

Copy link
Contributor Author

@stefan-fast stefan-fast Mar 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I didn't know strings.Fields yet. Thanks for the suggestion, I'll update the PR in just a moment. I have to rename the variables though since cmd and args are the parameter names of the function already :)

edit.Stderr = os.Stderr
edit.Stdout = os.Stdout
edit.Stdin = os.Stdin
Expand Down