From def57119f4a0af4e431778c5b7dbaacb24762bc6 Mon Sep 17 00:00:00 2001 From: Alexey Khit Date: Tue, 13 Sep 2022 15:42:23 +0300 Subject: [PATCH] Move shell QuoteSplit to separate pkg --- cmd/exec/exec.go | 39 ++------------------------------------- pkg/shell/shell.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 37 deletions(-) create mode 100644 pkg/shell/shell.go diff --git a/cmd/exec/exec.go b/cmd/exec/exec.go index b5f01dbd..5f96c374 100644 --- a/cmd/exec/exec.go +++ b/cmd/exec/exec.go @@ -8,6 +8,7 @@ import ( "github.com/AlexxIT/go2rtc/cmd/rtsp" "github.com/AlexxIT/go2rtc/cmd/streams" pkg "github.com/AlexxIT/go2rtc/pkg/rtsp" + "github.com/AlexxIT/go2rtc/pkg/shell" "github.com/AlexxIT/go2rtc/pkg/streamer" "github.com/rs/zerolog" "os" @@ -49,7 +50,7 @@ func Handle(url string) (streamer.Producer, error) { ) // remove `exec:` - args := QuoteSplit(url[5:]) + args := shell.QuoteSplit(url[5:]) cmd := exec.Command(args[0], args[1:]...) if log.Trace().Enabled() { @@ -86,39 +87,3 @@ func Handle(url string) (streamer.Producer, error) { var log zerolog.Logger 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 -} diff --git a/pkg/shell/shell.go b/pkg/shell/shell.go new file mode 100644 index 00000000..0b080876 --- /dev/null +++ b/pkg/shell/shell.go @@ -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 +}