diff --git a/pkg/shell/shell.go b/pkg/shell/shell.go index b9aae5fe..15792725 100644 --- a/pkg/shell/shell.go +++ b/pkg/shell/shell.go @@ -14,35 +14,28 @@ func QuoteSplit(s string) []string { var a []string for len(s) > 0 { - is := strings.IndexByte(s, ' ') - if is >= 0 { - // skip prefix and double spaces - if is == 0 { - // goto next symbol - s = s[1:] - continue + switch c := s[0]; c { + case '\t', '\n', '\r', ' ': // unicode.IsSpace + s = s[1:] + case '"', '\'': // quote chars + if i := strings.IndexByte(s[1:], c); i > 0 { + a = append(a, s[1:i+1]) + s = s[i+2:] + } else { + return nil // error } - - // check if quote in word - if i := strings.IndexByte(s[:is], '"'); i >= 0 { - // search quote end - if is = strings.Index(s, `" `); is > 0 { - is += 1 - } else { - is = -1 - } + default: + i := strings.IndexAny(s, "\t\n\r ") + if i > 0 { + a = append(a, s[:i]) + s = s[i:] + } else { + a = append(a, s) + s = "" } } - - if is >= 0 { - a = append(a, strings.ReplaceAll(s[:is], `"`, "")) - s = s[is+1:] - } else { - //add last word - a = append(a, s) - break - } } + return a } diff --git a/pkg/shell/shell_test.go b/pkg/shell/shell_test.go new file mode 100644 index 00000000..e3481eb0 --- /dev/null +++ b/pkg/shell/shell_test.go @@ -0,0 +1,18 @@ +package shell + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestQuoteSplit(t *testing.T) { + s := ` +python "-c" 'import time +print("time", time.time())' +` + require.Equal(t, []string{"python", "-c", "import time\nprint(\"time\", time.time())"}, QuoteSplit(s)) + + s = `ffmpeg -i video="0" -i "DeckLink SDI (2)"` + require.Equal(t, []string{"ffmpeg", "-i", "video=\"0\"", "-i", "DeckLink SDI (2)"}, QuoteSplit(s)) +}