mirror of
https://github.com/AlexxIT/go2rtc.git
synced 2025-10-05 08:16:55 +08:00
Rewrite shell cmd parser
This commit is contained in:
@@ -14,35 +14,28 @@ func QuoteSplit(s string) []string {
|
|||||||
var a []string
|
var a []string
|
||||||
|
|
||||||
for len(s) > 0 {
|
for len(s) > 0 {
|
||||||
is := strings.IndexByte(s, ' ')
|
switch c := s[0]; c {
|
||||||
if is >= 0 {
|
case '\t', '\n', '\r', ' ': // unicode.IsSpace
|
||||||
// skip prefix and double spaces
|
s = s[1:]
|
||||||
if is == 0 {
|
case '"', '\'': // quote chars
|
||||||
// goto next symbol
|
if i := strings.IndexByte(s[1:], c); i > 0 {
|
||||||
s = s[1:]
|
a = append(a, s[1:i+1])
|
||||||
continue
|
s = s[i+2:]
|
||||||
|
} else {
|
||||||
|
return nil // error
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
// check if quote in word
|
i := strings.IndexAny(s, "\t\n\r ")
|
||||||
if i := strings.IndexByte(s[:is], '"'); i >= 0 {
|
if i > 0 {
|
||||||
// search quote end
|
a = append(a, s[:i])
|
||||||
if is = strings.Index(s, `" `); is > 0 {
|
s = s[i:]
|
||||||
is += 1
|
} else {
|
||||||
} else {
|
a = append(a, s)
|
||||||
is = -1
|
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
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
|
18
pkg/shell/shell_test.go
Normal file
18
pkg/shell/shell_test.go
Normal file
@@ -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))
|
||||||
|
}
|
Reference in New Issue
Block a user