Skip to content

Commit 3c8da2f

Browse files
committed
Allow for spaces in command
Prior to this change, Windows users who had a space in the path to a command would encounter errors. Tested with a unit test which failed before commenting out code in exec.go to split out the command and arguments if a space was within the command. This is a breaking change and downstream users should make sure they populate Command and Args separately. alexellis/arkade#117 Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent fc7a3fd commit 3c8da2f

File tree

2 files changed

+56
-11
lines changed

2 files changed

+56
-11
lines changed

exec.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ type ExecTask struct {
1515
// or the executable with arguments. The arguments are detected by looking for
1616
// a space.
1717
//
18-
// Examples:
19-
// - Just a binary executable: `/bin/ls`
20-
// - Binary executable with arguments: `/bin/ls -la /`
18+
// Any arguments must be given via Args
2119
Command string
2220

2321
// Args are the arguments to pass to the command. These are ignored if the
@@ -110,14 +108,18 @@ func (et ExecTask) Execute(ctx context.Context) (ExecResult, error) {
110108
commandArgs = append([]string{"-c"}, fmt.Sprintf("%s %s", et.Command, script))
111109
}
112110
} else {
113-
if strings.Contains(et.Command, " ") {
114-
parts := strings.Split(et.Command, " ")
115-
command = parts[0]
116-
commandArgs = parts[1:]
117-
} else {
118-
command = et.Command
119-
commandArgs = et.Args
120-
}
111+
112+
command = et.Command
113+
commandArgs = et.Args
114+
115+
// AE: This had to be removed to fix: #117 where Windows users
116+
// have spaces in their paths, which are misinterpreted as
117+
// arguments for the command.
118+
// if strings.Contains(et.Command, " ") {
119+
// parts := strings.Split(et.Command, " ")
120+
// command = parts[0]
121+
// commandArgs = parts[1:]
122+
// }
121123
}
122124

123125
cmd := exec.CommandContext(ctx, command, commandArgs...)

exec_unix_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//go:build linux || darwin
2+
3+
package execute
4+
5+
import (
6+
"context"
7+
"os"
8+
"path"
9+
"testing"
10+
)
11+
12+
func Test_Exec_WithSpaceInCommandPath(t *testing.T) {
13+
14+
tmp, err := os.MkdirTemp(os.TempDir(), "exec test")
15+
if err != nil {
16+
t.Fatal(err)
17+
}
18+
19+
defer os.RemoveAll(tmp)
20+
21+
// copy /bin/echo to a new path with a space in it
22+
23+
data, err := os.ReadFile("/bin/echo")
24+
if err != nil {
25+
t.Fatal(err)
26+
}
27+
28+
newPath := path.Join(tmp, "echo")
29+
if err := os.WriteFile(newPath, data, 0755); err != nil {
30+
t.Fatal(err)
31+
}
32+
33+
task := ExecTask{Command: newPath, Args: []string{"hello world"}}
34+
35+
res, err := task.Execute(context.Background())
36+
if err != nil {
37+
t.Fatal(err)
38+
}
39+
40+
if res.Stdout != "hello world\n" {
41+
t.Fatalf("want %q, got %q", "hello world\n", res.Stdout)
42+
}
43+
}

0 commit comments

Comments
 (0)