From a29b0c945dba29432d937e3213f1a37d98286bde Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 19 Mar 2019 16:21:03 +0100 Subject: [PATCH] bump mattn/go-shellwords v1.0.5 Signed-off-by: Sebastiaan van Stijn --- vendor.conf | 2 +- .../mattn/go-shellwords/shellwords.go | 74 ++++++++++++++++--- .../mattn/go-shellwords/util_go15.go | 24 ++++++ .../mattn/go-shellwords/util_posix.go | 5 +- .../mattn/go-shellwords/util_windows.go | 5 ++ 5 files changed, 96 insertions(+), 14 deletions(-) create mode 100644 vendor/github.com/mattn/go-shellwords/util_go15.go diff --git a/vendor.conf b/vendor.conf index ee748b26bb..b68009a163 100755 --- a/vendor.conf +++ b/vendor.conf @@ -46,7 +46,7 @@ github.com/hashicorp/go-version 23480c0 github.com/imdario/mergo v0.3.6 github.com/inconshreveable/mousetrap 76626ae9c91c4f2a10f34cad8ce83ea42c93bb75 # v1.0 github.com/json-iterator/go 1624edc4454b8682399def8740d46db5e4362ba4 # 1.1.5 -github.com/mattn/go-shellwords v1.0.3 +github.com/mattn/go-shellwords a72fbe27a1b0ed0df2f02754945044ce1456608b # v1.0.5 github.com/matttproud/golang_protobuf_extensions v1.0.1 github.com/Microsoft/hcsshim v0.8.6 github.com/Microsoft/go-winio v0.4.11 diff --git a/vendor/github.com/mattn/go-shellwords/shellwords.go b/vendor/github.com/mattn/go-shellwords/shellwords.go index 107803927a..41429d8f26 100644 --- a/vendor/github.com/mattn/go-shellwords/shellwords.go +++ b/vendor/github.com/mattn/go-shellwords/shellwords.go @@ -4,6 +4,7 @@ import ( "errors" "os" "regexp" + "strings" ) var ( @@ -21,13 +22,17 @@ func isSpace(r rune) bool { return false } -func replaceEnv(s string) string { +func replaceEnv(getenv func(string) string, s string) string { + if getenv == nil { + getenv = os.Getenv + } + return envRe.ReplaceAllStringFunc(s, func(s string) string { s = s[1:] if s[0] == '{' { s = s[1 : len(s)-1] } - return os.Getenv(s) + return getenv(s) }) } @@ -35,16 +40,24 @@ type Parser struct { ParseEnv bool ParseBacktick bool Position int + + // If ParseEnv is true, use this for getenv. + // If nil, use os.Getenv. + Getenv func(string) string } func NewParser() *Parser { - return &Parser{ParseEnv, ParseBacktick, 0} + return &Parser{ + ParseEnv: ParseEnv, + ParseBacktick: ParseBacktick, + Position: 0, + } } func (p *Parser) Parse(line string) ([]string, error) { args := []string{} buf := "" - var escaped, doubleQuoted, singleQuoted, backQuote bool + var escaped, doubleQuoted, singleQuoted, backQuote, dollarQuote bool backtick := "" pos := -1 @@ -68,12 +81,12 @@ loop: } if isSpace(r) { - if singleQuoted || doubleQuoted || backQuote { + if singleQuoted || doubleQuoted || backQuote || dollarQuote { buf += string(r) backtick += string(r) } else if got { if p.ParseEnv { - buf = replaceEnv(buf) + buf = replaceEnv(p.Getenv, buf) } args = append(args, buf) buf = "" @@ -84,7 +97,7 @@ loop: switch r { case '`': - if !singleQuoted && !doubleQuoted { + if !singleQuoted && !doubleQuoted && !dollarQuote { if p.ParseBacktick { if backQuote { out, err := shellRun(backtick) @@ -100,18 +113,55 @@ loop: backtick = "" backQuote = !backQuote } + case ')': + if !singleQuoted && !doubleQuoted && !backQuote { + if p.ParseBacktick { + if dollarQuote { + out, err := shellRun(backtick) + if err != nil { + return nil, err + } + if r == ')' { + buf = buf[:len(buf)-len(backtick)-2] + out + } else { + buf = buf[:len(buf)-len(backtick)-1] + out + } + } + backtick = "" + dollarQuote = !dollarQuote + continue + } + backtick = "" + dollarQuote = !dollarQuote + } + case '(': + if !singleQuoted && !doubleQuoted && !backQuote { + if !dollarQuote && strings.HasSuffix(buf, "$") { + dollarQuote = true + buf += "(" + continue + } else { + return nil, errors.New("invalid command line string") + } + } case '"': - if !singleQuoted { + if !singleQuoted && !dollarQuote { doubleQuoted = !doubleQuoted continue } case '\'': - if !doubleQuoted { + if !doubleQuoted && !dollarQuote { singleQuoted = !singleQuoted continue } case ';', '&', '|', '<', '>': if !(escaped || singleQuoted || doubleQuoted || backQuote) { + if r == '>' && len(buf) > 0 { + if c := buf[0]; '0' <= c && c <= '9' { + i -= 1 + got = false + } + } pos = i break loop } @@ -119,19 +169,19 @@ loop: got = true buf += string(r) - if backQuote { + if backQuote || dollarQuote { backtick += string(r) } } if got { if p.ParseEnv { - buf = replaceEnv(buf) + buf = replaceEnv(p.Getenv, buf) } args = append(args, buf) } - if escaped || singleQuoted || doubleQuoted || backQuote { + if escaped || singleQuoted || doubleQuoted || backQuote || dollarQuote { return nil, errors.New("invalid command line string") } diff --git a/vendor/github.com/mattn/go-shellwords/util_go15.go b/vendor/github.com/mattn/go-shellwords/util_go15.go new file mode 100644 index 0000000000..180f00f0bd --- /dev/null +++ b/vendor/github.com/mattn/go-shellwords/util_go15.go @@ -0,0 +1,24 @@ +// +build !go1.6 + +package shellwords + +import ( + "os" + "os/exec" + "runtime" + "strings" +) + +func shellRun(line string) (string, error) { + var b []byte + var err error + if runtime.GOOS == "windows" { + b, err = exec.Command(os.Getenv("COMSPEC"), "/c", line).Output() + } else { + b, err = exec.Command(os.Getenv("SHELL"), "-c", line).Output() + } + if err != nil { + return "", err + } + return strings.TrimSpace(string(b)), nil +} diff --git a/vendor/github.com/mattn/go-shellwords/util_posix.go b/vendor/github.com/mattn/go-shellwords/util_posix.go index 4f8ac55e47..eaf1011d60 100644 --- a/vendor/github.com/mattn/go-shellwords/util_posix.go +++ b/vendor/github.com/mattn/go-shellwords/util_posix.go @@ -1,4 +1,4 @@ -// +build !windows +// +build !windows,go1.6 package shellwords @@ -13,6 +13,9 @@ func shellRun(line string) (string, error) { shell := os.Getenv("SHELL") b, err := exec.Command(shell, "-c", line).Output() if err != nil { + if eerr, ok := err.(*exec.ExitError); ok { + b = eerr.Stderr + } return "", errors.New(err.Error() + ":" + string(b)) } return strings.TrimSpace(string(b)), nil diff --git a/vendor/github.com/mattn/go-shellwords/util_windows.go b/vendor/github.com/mattn/go-shellwords/util_windows.go index 7cad4cf06f..e46f89a1fe 100644 --- a/vendor/github.com/mattn/go-shellwords/util_windows.go +++ b/vendor/github.com/mattn/go-shellwords/util_windows.go @@ -1,3 +1,5 @@ +// +build windows,go1.6 + package shellwords import ( @@ -11,6 +13,9 @@ func shellRun(line string) (string, error) { shell := os.Getenv("COMSPEC") b, err := exec.Command(shell, "/c", line).Output() if err != nil { + if eerr, ok := err.(*exec.ExitError); ok { + b = eerr.Stderr + } return "", errors.New(err.Error() + ":" + string(b)) } return strings.TrimSpace(string(b)), nil