build(deps): bump github.com/mattn/go-shellwords from 1.0.10 to 1.0.11

Bumps [github.com/mattn/go-shellwords](https://github.com/mattn/go-shellwords) from 1.0.10 to 1.0.11.
- [Release notes](https://github.com/mattn/go-shellwords/releases)
- [Commits](https://github.com/mattn/go-shellwords/compare/v1.0.10...v1.0.11)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
This commit is contained in:
dependabot-preview[bot] 2021-01-18 10:17:25 +00:00 committed by Daniel J Walsh
parent 803a42f1e6
commit ace25461d3
No known key found for this signature in database
GPG Key ID: A2DF901DABE2C028
8 changed files with 153 additions and 41 deletions

2
go.mod
View File

@ -10,7 +10,7 @@ require (
github.com/hashicorp/go-multierror v1.1.0
github.com/klauspost/compress v1.11.6
github.com/klauspost/pgzip v1.2.5
github.com/mattn/go-shellwords v1.0.10
github.com/mattn/go-shellwords v1.0.11
github.com/mistifyio/go-zfs v2.1.1+incompatible
github.com/moby/sys/mountinfo v0.4.0
github.com/opencontainers/go-digest v1.0.0

4
go.sum
View File

@ -71,8 +71,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-shellwords v1.0.10 h1:Y7Xqm8piKOO3v10Thp7Z36h4FYFjt5xB//6XvOrs2Gw=
github.com/mattn/go-shellwords v1.0.10/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
github.com/mattn/go-shellwords v1.0.11 h1:vCoR9VPpsk/TZFW2JwK5I9S0xdrtUq2bph6/YjEPnaw=
github.com/mattn/go-shellwords v1.0.11/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
github.com/mistifyio/go-zfs v2.1.1+incompatible h1:gAMO1HM9xBRONLHHYnu5iFsOJUiJdNZo6oqSENd4eW8=
github.com/mistifyio/go-zfs v2.1.1+incompatible/go.mod h1:8AuVvqP/mXw1px98n46wfvcGfQ4ci2FwoAjKYxuo3Z4=
github.com/moby/sys/mountinfo v0.1.3 h1:KIrhRO14+AkwKvG/g2yIpNMOUVZ02xNhOw8KY1WsLOI=

View File

@ -1,3 +1,6 @@
arch:
- amd64
- ppc64le
language: go
sudo: false
go:

View File

@ -2,7 +2,8 @@
[![codecov](https://codecov.io/gh/mattn/go-shellwords/branch/master/graph/badge.svg)](https://codecov.io/gh/mattn/go-shellwords)
[![Build Status](https://travis-ci.org/mattn/go-shellwords.svg?branch=master)](https://travis-ci.org/mattn/go-shellwords)
[![GoDoc](https://godoc.org/github.com/mattn/go-shellwords?status.svg)](http://godoc.org/github.com/mattn/go-shellwords)
[![PkgGoDev](https://pkg.go.dev/badge/github.com/mattn/go-shellwords)](https://pkg.go.dev/github.com/mattn/go-shellwords)
[![ci](https://github.com/mattn/go-shellwords/ci/badge.svg)](https://github.com/mattn/go-shellwords/actions)
Parse line as shell words.
@ -13,6 +14,12 @@ args, err := shellwords.Parse("./foo --bar=baz")
// args should be ["./foo", "--bar=baz"]
```
```go
envs, args, err := shellwords.ParseWithEnvs("FOO=foo BAR=baz ./foo --bar=baz")
// envs should be ["FOO=foo", "BAR=baz"]
// args should be ["./foo", "--bar=baz"]
```
```go
os.Setenv("FOO", "bar")
p := shellwords.NewParser()

View File

@ -1,10 +1,11 @@
package shellwords
import (
"bytes"
"errors"
"os"
"regexp"
"strings"
"unicode"
)
var (
@ -12,8 +13,6 @@ var (
ParseBacktick bool = false
)
var envRe = regexp.MustCompile(`\$({[a-zA-Z0-9_]+}|[a-zA-Z0-9_]+)`)
func isSpace(r rune) bool {
switch r {
case ' ', '\t', '\r', '\n':
@ -27,13 +26,72 @@ func replaceEnv(getenv func(string) string, s string) string {
getenv = os.Getenv
}
return envRe.ReplaceAllStringFunc(s, func(s string) string {
s = s[1:]
if s[0] == '{' {
s = s[1 : len(s)-1]
var buf bytes.Buffer
rs := []rune(s)
for i := 0; i < len(rs); i++ {
r := rs[i]
if r == '\\' {
i++
if i == len(rs) {
break
}
buf.WriteRune(rs[i])
continue
} else if r == '$' {
i++
if i == len(rs) {
buf.WriteRune(r)
break
}
if rs[i] == 0x7b {
i++
p := i
for ; i < len(rs); i++ {
r = rs[i]
if r == '\\' {
i++
if i == len(rs) {
return s
}
continue
}
if r == 0x7d || (!unicode.IsLetter(r) && r != '_' && !unicode.IsDigit(r)) {
break
}
}
if r != 0x7d {
return s
}
if i > p {
buf.WriteString(getenv(s[p:i]))
}
} else {
p := i
for ; i < len(rs); i++ {
r := rs[i]
if r == '\\' {
i++
if i == len(rs) {
return s
}
continue
}
if !unicode.IsLetter(r) && r != '_' && !unicode.IsDigit(r) {
break
}
}
if i > p {
buf.WriteString(getenv(s[p:i]))
i--
} else {
buf.WriteString(s[p:])
}
}
} else {
buf.WriteRune(r)
}
return getenv(s)
})
}
return buf.String()
}
type Parser struct {
@ -56,6 +114,14 @@ func NewParser() *Parser {
}
}
type argType int
const (
argNo argType = iota
argSingle
argQuoted
)
func (p *Parser) Parse(line string) ([]string, error) {
args := []string{}
buf := ""
@ -63,13 +129,16 @@ func (p *Parser) Parse(line string) ([]string, error) {
backtick := ""
pos := -1
got := false
got := argNo
i := -1
loop:
for i, r := range line {
for _, r := range line {
i++
if escaped {
buf += string(r)
escaped = false
got = argSingle
continue
}
@ -86,21 +155,23 @@ loop:
if singleQuoted || doubleQuoted || backQuote || dollarQuote {
buf += string(r)
backtick += string(r)
} else if got {
} else if got != argNo {
if p.ParseEnv {
parser := &Parser{ParseEnv: false, ParseBacktick: false, Position: 0, Dir: p.Dir}
strs, err := parser.Parse(replaceEnv(p.Getenv, buf))
if err != nil {
return nil, err
}
for _, str := range strs {
args = append(args, str)
if got == argSingle {
parser := &Parser{ParseEnv: false, ParseBacktick: false, Position: 0, Dir: p.Dir}
strs, err := parser.Parse(replaceEnv(p.Getenv, buf))
if err != nil {
return nil, err
}
args = append(args, strs...)
} else {
args = append(args, replaceEnv(p.Getenv, buf))
}
} else {
args = append(args, buf)
}
buf = ""
got = false
got = argNo
}
continue
}
@ -153,7 +224,7 @@ loop:
case '"':
if !singleQuoted && !dollarQuote {
if doubleQuoted {
got = true
got = argQuoted
}
doubleQuoted = !doubleQuoted
continue
@ -161,7 +232,7 @@ loop:
case '\'':
if !doubleQuoted && !dollarQuote {
if singleQuoted {
got = true
got = argSingle
}
singleQuoted = !singleQuoted
continue
@ -171,7 +242,7 @@ loop:
if r == '>' && len(buf) > 0 {
if c := buf[0]; '0' <= c && c <= '9' {
i -= 1
got = false
got = argNo
}
}
pos = i
@ -179,22 +250,24 @@ loop:
}
}
got = true
got = argSingle
buf += string(r)
if backQuote || dollarQuote {
backtick += string(r)
}
}
if got {
if got != argNo {
if p.ParseEnv {
parser := &Parser{ParseEnv: false, ParseBacktick: false, Position: 0, Dir: p.Dir}
strs, err := parser.Parse(replaceEnv(p.Getenv, buf))
if err != nil {
return nil, err
}
for _, str := range strs {
args = append(args, str)
if got == argSingle {
parser := &Parser{ParseEnv: false, ParseBacktick: false, Position: 0, Dir: p.Dir}
strs, err := parser.Parse(replaceEnv(p.Getenv, buf))
if err != nil {
return nil, err
}
args = append(args, strs...)
} else {
args = append(args, replaceEnv(p.Getenv, buf))
}
} else {
args = append(args, buf)
@ -210,6 +283,35 @@ loop:
return args, nil
}
func (p *Parser) ParseWithEnvs(line string) (envs []string, args []string, err error) {
_args, err := p.Parse(line)
if err != nil {
return nil, nil, err
}
envs = []string{}
args = []string{}
parsingEnv := true
for _, arg := range _args {
if parsingEnv && isEnv(arg) {
envs = append(envs, arg)
} else {
if parsingEnv {
parsingEnv = false
}
args = append(args, arg)
}
}
return envs, args, nil
}
func isEnv(arg string) bool {
return len(strings.Split(arg, "=")) == 2
}
func Parse(line string) ([]string, error) {
return NewParser().Parse(line)
}
func ParseWithEnvs(line string) (envs []string, args []string, err error) {
return NewParser().ParseWithEnvs(line)
}

View File

@ -3,7 +3,7 @@
package shellwords
import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
@ -23,7 +23,7 @@ func shellRun(line, dir string) (string, error) {
if eerr, ok := err.(*exec.ExitError); ok {
b = eerr.Stderr
}
return "", errors.New(err.Error() + ":" + string(b))
return "", fmt.Errorf("%s: %w", string(b), err)
}
return strings.TrimSpace(string(b)), nil
}

View File

@ -3,7 +3,7 @@
package shellwords
import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
@ -23,7 +23,7 @@ func shellRun(line, dir string) (string, error) {
if eerr, ok := err.(*exec.ExitError); ok {
b = eerr.Stderr
}
return "", errors.New(err.Error() + ":" + string(b))
return "", fmt.Errorf("%s: %w", string(b), err)
}
return strings.TrimSpace(string(b)), nil
}

2
vendor/modules.txt vendored
View File

@ -65,7 +65,7 @@ github.com/klauspost/compress/zstd/internal/xxhash
# github.com/klauspost/pgzip v1.2.5
## explicit
github.com/klauspost/pgzip
# github.com/mattn/go-shellwords v1.0.10
# github.com/mattn/go-shellwords v1.0.11
## explicit
github.com/mattn/go-shellwords
# github.com/mistifyio/go-zfs v2.1.1+incompatible