bump mattn/go-shellwords v1.0.5

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2019-03-19 16:21:03 +01:00
parent 0f0cedc5ac
commit a29b0c945d
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
5 changed files with 96 additions and 14 deletions

View File

@ -46,7 +46,7 @@ github.com/hashicorp/go-version 23480c0
github.com/imdario/mergo v0.3.6 github.com/imdario/mergo v0.3.6
github.com/inconshreveable/mousetrap 76626ae9c91c4f2a10f34cad8ce83ea42c93bb75 # v1.0 github.com/inconshreveable/mousetrap 76626ae9c91c4f2a10f34cad8ce83ea42c93bb75 # v1.0
github.com/json-iterator/go 1624edc4454b8682399def8740d46db5e4362ba4 # 1.1.5 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/matttproud/golang_protobuf_extensions v1.0.1
github.com/Microsoft/hcsshim v0.8.6 github.com/Microsoft/hcsshim v0.8.6
github.com/Microsoft/go-winio v0.4.11 github.com/Microsoft/go-winio v0.4.11

View File

@ -4,6 +4,7 @@ import (
"errors" "errors"
"os" "os"
"regexp" "regexp"
"strings"
) )
var ( var (
@ -21,13 +22,17 @@ func isSpace(r rune) bool {
return false 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 { return envRe.ReplaceAllStringFunc(s, func(s string) string {
s = s[1:] s = s[1:]
if s[0] == '{' { if s[0] == '{' {
s = s[1 : len(s)-1] s = s[1 : len(s)-1]
} }
return os.Getenv(s) return getenv(s)
}) })
} }
@ -35,16 +40,24 @@ type Parser struct {
ParseEnv bool ParseEnv bool
ParseBacktick bool ParseBacktick bool
Position int Position int
// If ParseEnv is true, use this for getenv.
// If nil, use os.Getenv.
Getenv func(string) string
} }
func NewParser() *Parser { func NewParser() *Parser {
return &Parser{ParseEnv, ParseBacktick, 0} return &Parser{
ParseEnv: ParseEnv,
ParseBacktick: ParseBacktick,
Position: 0,
}
} }
func (p *Parser) Parse(line string) ([]string, error) { func (p *Parser) Parse(line string) ([]string, error) {
args := []string{} args := []string{}
buf := "" buf := ""
var escaped, doubleQuoted, singleQuoted, backQuote bool var escaped, doubleQuoted, singleQuoted, backQuote, dollarQuote bool
backtick := "" backtick := ""
pos := -1 pos := -1
@ -68,12 +81,12 @@ loop:
} }
if isSpace(r) { if isSpace(r) {
if singleQuoted || doubleQuoted || backQuote { if singleQuoted || doubleQuoted || backQuote || dollarQuote {
buf += string(r) buf += string(r)
backtick += string(r) backtick += string(r)
} else if got { } else if got {
if p.ParseEnv { if p.ParseEnv {
buf = replaceEnv(buf) buf = replaceEnv(p.Getenv, buf)
} }
args = append(args, buf) args = append(args, buf)
buf = "" buf = ""
@ -84,7 +97,7 @@ loop:
switch r { switch r {
case '`': case '`':
if !singleQuoted && !doubleQuoted { if !singleQuoted && !doubleQuoted && !dollarQuote {
if p.ParseBacktick { if p.ParseBacktick {
if backQuote { if backQuote {
out, err := shellRun(backtick) out, err := shellRun(backtick)
@ -100,18 +113,55 @@ loop:
backtick = "" backtick = ""
backQuote = !backQuote 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 '"': case '"':
if !singleQuoted { if !singleQuoted && !dollarQuote {
doubleQuoted = !doubleQuoted doubleQuoted = !doubleQuoted
continue continue
} }
case '\'': case '\'':
if !doubleQuoted { if !doubleQuoted && !dollarQuote {
singleQuoted = !singleQuoted singleQuoted = !singleQuoted
continue continue
} }
case ';', '&', '|', '<', '>': case ';', '&', '|', '<', '>':
if !(escaped || singleQuoted || doubleQuoted || backQuote) { if !(escaped || singleQuoted || doubleQuoted || backQuote) {
if r == '>' && len(buf) > 0 {
if c := buf[0]; '0' <= c && c <= '9' {
i -= 1
got = false
}
}
pos = i pos = i
break loop break loop
} }
@ -119,19 +169,19 @@ loop:
got = true got = true
buf += string(r) buf += string(r)
if backQuote { if backQuote || dollarQuote {
backtick += string(r) backtick += string(r)
} }
} }
if got { if got {
if p.ParseEnv { if p.ParseEnv {
buf = replaceEnv(buf) buf = replaceEnv(p.Getenv, buf)
} }
args = append(args, buf) args = append(args, buf)
} }
if escaped || singleQuoted || doubleQuoted || backQuote { if escaped || singleQuoted || doubleQuoted || backQuote || dollarQuote {
return nil, errors.New("invalid command line string") return nil, errors.New("invalid command line string")
} }

24
vendor/github.com/mattn/go-shellwords/util_go15.go generated vendored Normal file
View File

@ -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
}

View File

@ -1,4 +1,4 @@
// +build !windows // +build !windows,go1.6
package shellwords package shellwords
@ -13,6 +13,9 @@ func shellRun(line string) (string, error) {
shell := os.Getenv("SHELL") shell := os.Getenv("SHELL")
b, err := exec.Command(shell, "-c", line).Output() b, err := exec.Command(shell, "-c", line).Output()
if err != nil { if err != nil {
if eerr, ok := err.(*exec.ExitError); ok {
b = eerr.Stderr
}
return "", errors.New(err.Error() + ":" + string(b)) return "", errors.New(err.Error() + ":" + string(b))
} }
return strings.TrimSpace(string(b)), nil return strings.TrimSpace(string(b)), nil

View File

@ -1,3 +1,5 @@
// +build windows,go1.6
package shellwords package shellwords
import ( import (
@ -11,6 +13,9 @@ func shellRun(line string) (string, error) {
shell := os.Getenv("COMSPEC") shell := os.Getenv("COMSPEC")
b, err := exec.Command(shell, "/c", line).Output() b, err := exec.Command(shell, "/c", line).Output()
if err != nil { if err != nil {
if eerr, ok := err.(*exec.ExitError); ok {
b = eerr.Stderr
}
return "", errors.New(err.Error() + ":" + string(b)) return "", errors.New(err.Error() + ":" + string(b))
} }
return strings.TrimSpace(string(b)), nil return strings.TrimSpace(string(b)), nil