Skip to content

Commit

Permalink
Add support for editor env variable with flags
Browse files Browse the repository at this point in the history
  • Loading branch information
mkchoi212 committed May 25, 2018
1 parent d0fb86a commit 9c16b8a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
17 changes: 13 additions & 4 deletions editor/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,21 @@ func writeTmpFile(content io.Reader) (string, error) {

// editorCmd returns a os/exec.Cmd to open the provided file
func editorCmd(filename string) *exec.Cmd {
editorPath := os.Getenv("EDITOR")
if editorPath == "" {
editorPath = defaultEditor
editorEnv := os.Getenv("EDITOR")
if editorEnv == "" {
editorEnv = defaultEditor
}

editor := execCommand(editorPath, filename)
editorVars := strings.Split(editorEnv, " ")

path := editorVars[0]
args := []string{filename}

if len(editorVars) > 1 {
args = append(editorVars[1:], args...)
}

editor := execCommand(path, args...)

editor.Stdin = os.Stdin
editor.Stdout = os.Stdout
Expand Down
9 changes: 8 additions & 1 deletion editor/editor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ import (
)

func TestEditorCmd(t *testing.T) {
editor := editorCmd(".")
os.Setenv("EDITOR", "")
editor := editorCmd("foobar")
testhelper.Assert(t, editor != nil, "editor should not be nil")
testhelper.Equals(t, editor.Args, []string{"vim", "foobar"})

os.Setenv("EDITOR", "subl -w")
editor = editorCmd("foobar")
testhelper.Assert(t, editor != nil, "editor should not be nil")
testhelper.Equals(t, editor.Args, []string{"subl", "-w", "foobar"})
}

func TestWriteTmpFile(t *testing.T) {
Expand Down

0 comments on commit 9c16b8a

Please sign in to comment.