Move shell QuoteSplit to separate pkg
This commit is contained in:
+2
-37
@@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/AlexxIT/go2rtc/cmd/rtsp"
|
"github.com/AlexxIT/go2rtc/cmd/rtsp"
|
||||||
"github.com/AlexxIT/go2rtc/cmd/streams"
|
"github.com/AlexxIT/go2rtc/cmd/streams"
|
||||||
pkg "github.com/AlexxIT/go2rtc/pkg/rtsp"
|
pkg "github.com/AlexxIT/go2rtc/pkg/rtsp"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/shell"
|
||||||
"github.com/AlexxIT/go2rtc/pkg/streamer"
|
"github.com/AlexxIT/go2rtc/pkg/streamer"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"os"
|
"os"
|
||||||
@@ -49,7 +50,7 @@ func Handle(url string) (streamer.Producer, error) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
// remove `exec:`
|
// remove `exec:`
|
||||||
args := QuoteSplit(url[5:])
|
args := shell.QuoteSplit(url[5:])
|
||||||
cmd := exec.Command(args[0], args[1:]...)
|
cmd := exec.Command(args[0], args[1:]...)
|
||||||
|
|
||||||
if log.Trace().Enabled() {
|
if log.Trace().Enabled() {
|
||||||
@@ -86,39 +87,3 @@ func Handle(url string) (streamer.Producer, error) {
|
|||||||
|
|
||||||
var log zerolog.Logger
|
var log zerolog.Logger
|
||||||
var waiters map[string]chan streamer.Producer
|
var waiters map[string]chan streamer.Producer
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package shell
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user