Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions util/sh/sh.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"unicode"
)

// Verbose enables verbose output
Expand All @@ -38,8 +38,39 @@ func RunCommand(name string, arg ...string) error {
return cmd.Run()
}

// SplitParameters splits shell command parameters, taking quoting in account.
// SplitParameters splits shell command parameters, taking quoting into account.
// Single and double quotes group text without becoming part of the parameter.
func SplitParameters(s string) []string {
r := regexp.MustCompile(`'[^']*'|[^ ]+`)
return r.FindAllString(s, -1)
var (
params []string
current strings.Builder
inParam bool
quote rune
)
for _, r := range s {
switch {
case quote != 0:
if r == quote {
quote = 0
continue
}
current.WriteRune(r)
case r == '\'' || r == '"':
quote = r
inParam = true
case unicode.IsSpace(r):
if inParam {
params = append(params, current.String())
current.Reset()
inParam = false
}
default:
current.WriteRune(r)
inParam = true
}
}
if inParam {
params = append(params, current.String())
}
return params
}
56 changes: 48 additions & 8 deletions util/sh/sh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,57 @@
package sh

import (
"strings"
"reflect"
"testing"
)

func TestSplitParameters(t *testing.T) {
in := `-a -tags 'netgo static_build'`
expect := []string{"-a", "-tags", `'netgo static_build'`}
got := SplitParameters(in)
for i, g := range got {
if expect[i] != g {
t.Error("expected", expect[i], "got", g, "full output: ", strings.Join(got, "#"))
}
for _, tc := range []struct {
name string
in string
want []string
}{
{
name: "empty",
in: "",
want: nil,
},
{
name: "blanks only",
in: " \t ",
want: nil,
},
{
name: "unquoted",
in: "-a -tags netgo",
want: []string{"-a", "-tags", "netgo"},
},
{
name: "extra blanks",
in: " -a \t -mod=vendor ",
want: []string{"-a", "-mod=vendor"},
},
{
name: "single quotes",
in: `-a -tags 'netgo static_build'`,
want: []string{"-a", "-tags", "netgo static_build"},
},
{
name: "double quotes",
in: `-a -tags "netgo static_build"`,
want: []string{"-a", "-tags", "netgo static_build"},
},
{
name: "quotes attached to the flag",
in: `-gcflags="all=-N -l"`,
want: []string{"-gcflags=all=-N -l"},
},
} {
t.Run(tc.name, func(t *testing.T) {
got := SplitParameters(tc.in)
if !reflect.DeepEqual(got, tc.want) {
t.Errorf("SplitParameters(%q) = %q, want %q", tc.in, got, tc.want)
}
})
}
}