Skip to content

Commit cff6c4e

Browse files
authored
cmd: remove quotes for command arguments (#630)
1 parent 9b2091f commit cff6c4e

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

pkg/manager/vip/network_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ func TestAddDelIP(t *testing.T) {
4242
},
4343
}
4444

45+
isOtherErr := func(err error) bool {
46+
return strings.Contains(err.Error(), "command not found") || strings.Contains(err.Error(), "not in the sudoers file")
47+
}
48+
4549
for i, test := range tests {
4650
operation, err := NewNetworkOperation(test.virtualIP, test.link)
4751
if test.initErr != "" {
@@ -54,7 +58,7 @@ func TestAddDelIP(t *testing.T) {
5458

5559
err = operation.AddIP()
5660
// Maybe the command is not installed.
57-
if err != nil && strings.Contains(err.Error(), "command not found") {
61+
if err != nil && isOtherErr(err) {
5862
continue
5963
}
6064
if test.addErr != "" {
@@ -65,7 +69,7 @@ func TestAddDelIP(t *testing.T) {
6569
}
6670

6771
err = operation.SendARP()
68-
if err == nil || !strings.Contains(err.Error(), "command not found") {
72+
if err == nil || !isOtherErr(err) {
6973
if test.sendErr != "" {
7074
require.Error(t, err, "case %d", i)
7175
require.Contains(t, err.Error(), test.sendErr, "case %d", i)

pkg/util/cmd/cmd.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package cmd
55

66
import (
7-
"fmt"
87
"os/exec"
98
"strings"
109

@@ -22,7 +21,7 @@ func ExecCmd(cmd string, args ...string) error {
2221
if !isValidArg(arg) {
2322
return errors.Errorf("invalid argument: %s", arg)
2423
}
25-
cmds = append(cmds, escapeArg(arg))
24+
cmds = append(cmds, arg)
2625
}
2726
output, err := exec.Command(cmds[0], cmds[1:]...).CombinedOutput()
2827
if err != nil {
@@ -40,7 +39,3 @@ func isValidArg(arg string) bool {
4039
}
4140
return true
4241
}
43-
44-
func escapeArg(arg string) string {
45-
return fmt.Sprintf("'%s'", strings.ReplaceAll(arg, "'", "'\\''"))
46-
}

pkg/util/cmd/cmd_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ func TestExecCmd(t *testing.T) {
2121
{
2222
cmds: []string{"echo", "abc"},
2323
},
24+
{
25+
cmds: []string{"cd", "."},
26+
},
2427
{
2528
cmds: []string{"hello"},
2629
hasErr: true,

0 commit comments

Comments
 (0)