Skip to content

Commit 312ccd8

Browse files
committed
[cmd] Stop the surprising behavior of shell-splitting in cmd.New()
`cmd.New(programWithSpaces)` will now work for executables that have spaces in the path. The only place where we relied on shell-splitting was in openTextEditor, which now performs its own splitting.
1 parent 6a13adf commit 312ccd8

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

cmd/cmd.go

+8-10
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99
"syscall"
1010

1111
"github.com/github/hub/ui"
12-
"github.com/github/hub/utils"
13-
"github.com/kballard/go-shellquote"
1412
)
1513

1614
type Cmd struct {
@@ -123,14 +121,14 @@ func (cmd *Cmd) Exec() error {
123121
return syscall.Exec(binary, args, os.Environ())
124122
}
125123

126-
func New(cmd string) *Cmd {
127-
cmds, err := shellquote.Split(cmd)
128-
utils.Check(err)
129-
130-
name := cmds[0]
131-
args := cmds[1:]
132-
133-
return &Cmd{Name: name, Args: args, Stdin: os.Stdin, Stdout: os.Stdout, Stderr: os.Stderr}
124+
func New(name string) *Cmd {
125+
return &Cmd{
126+
Name: name,
127+
Args: []string{},
128+
Stdin: os.Stdin,
129+
Stdout: os.Stdout,
130+
Stderr: os.Stderr,
131+
}
134132
}
135133

136134
func NewWithArray(cmd []string) *Cmd {

cmd/cmd_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ import (
88

99
func TestNew(t *testing.T) {
1010
execCmd := New("vim --noplugin")
11-
assert.Equal(t, "vim", execCmd.Name)
12-
assert.Equal(t, 1, len(execCmd.Args))
13-
assert.Equal(t, "--noplugin", execCmd.Args[0])
11+
assert.Equal(t, "vim --noplugin", execCmd.Name)
12+
assert.Equal(t, 0, len(execCmd.Args))
1413
}
1514

1615
func TestWithArg(t *testing.T) {

github/editor.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/github/hub/cmd"
1414
"github.com/github/hub/git"
15+
"github.com/kballard/go-shellquote"
1516
)
1617

1718
const Scissors = "------------------------ >8 ------------------------"
@@ -137,7 +138,11 @@ func (e *Editor) readContent() (content []byte, err error) {
137138
}
138139

139140
func openTextEditor(program, file string) error {
140-
editCmd := cmd.New(program)
141+
programArgs, err := shellquote.Split(program)
142+
if err != nil {
143+
return err
144+
}
145+
editCmd := cmd.NewWithArray(programArgs)
141146
r := regexp.MustCompile(`\b(?:[gm]?vim)(?:\.exe)?$`)
142147
if r.MatchString(editCmd.Name) {
143148
editCmd.WithArg("--cmd")

0 commit comments

Comments
 (0)