├── .github ├── FUNDING.yml └── workflows │ └── ci.yml ├── .travis.yml ├── LICENSE ├── README.md ├── _example └── pipe.go ├── go.mod ├── go.test.sh ├── shellwords.go ├── shellwords_test.go ├── util_posix.go └── util_windows.go /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: mattn # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: ci 2 | on: 3 | pull_request: 4 | branches: 5 | - master 6 | push: 7 | branches: 8 | - master 9 | jobs: 10 | test: 11 | strategy: 12 | matrix: 13 | os: [ubuntu-latest, macos-latest, windows-latest] 14 | go: ["1.14", "1.15"] 15 | runs-on: ${{ matrix.os }} 16 | steps: 17 | - uses: actions/checkout@v2 18 | - uses: actions/setup-go@v2 19 | with: 20 | go-version: ${{ matrix.go }} 21 | - run: | 22 | go test -cover -coverprofile coverage.txt -race -v ./... 23 | - uses: codecov/codecov-action@v1 24 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | arch: 2 | - amd64 3 | - ppc64le 4 | language: go 5 | sudo: false 6 | go: 7 | - tip 8 | 9 | before_install: 10 | - go get -t -v ./... 11 | 12 | script: 13 | - ./go.test.sh 14 | 15 | after_success: 16 | - bash <(curl -s https://codecov.io/bash) 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Yasuhiro Matsumoto 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # go-shellwords 2 | 3 | [![codecov](https://codecov.io/gh/mattn/go-shellwords/branch/master/graph/badge.svg)](https://codecov.io/gh/mattn/go-shellwords) 4 | [![Build Status](https://travis-ci.org/mattn/go-shellwords.svg?branch=master)](https://travis-ci.org/mattn/go-shellwords) 5 | [![PkgGoDev](https://pkg.go.dev/badge/github.com/mattn/go-shellwords)](https://pkg.go.dev/github.com/mattn/go-shellwords) 6 | [![ci](https://github.com/mattn/go-shellwords/ci/badge.svg)](https://github.com/mattn/go-shellwords/actions) 7 | 8 | Parse line as shell words. 9 | 10 | ## Usage 11 | 12 | ```go 13 | args, err := shellwords.Parse("./foo --bar=baz") 14 | // args should be ["./foo", "--bar=baz"] 15 | ``` 16 | 17 | ```go 18 | envs, args, err := shellwords.ParseWithEnvs("FOO=foo BAR=baz ./foo --bar=baz") 19 | // envs should be ["FOO=foo", "BAR=baz"] 20 | // args should be ["./foo", "--bar=baz"] 21 | ``` 22 | 23 | ```go 24 | os.Setenv("FOO", "bar") 25 | p := shellwords.NewParser() 26 | p.ParseEnv = true 27 | args, err := p.Parse("./foo $FOO") 28 | // args should be ["./foo", "bar"] 29 | ``` 30 | 31 | ```go 32 | p := shellwords.NewParser() 33 | p.ParseBacktick = true 34 | args, err := p.Parse("./foo `echo $SHELL`") 35 | // args should be ["./foo", "/bin/bash"] 36 | ``` 37 | 38 | ```go 39 | shellwords.ParseBacktick = true 40 | p := shellwords.NewParser() 41 | args, err := p.Parse("./foo `echo $SHELL`") 42 | // args should be ["./foo", "/bin/bash"] 43 | ``` 44 | 45 | # Thanks 46 | 47 | This is based on cpan module [Parse::CommandLine](https://metacpan.org/pod/Parse::CommandLine). 48 | 49 | # License 50 | 51 | under the MIT License: http://mattn.mit-license.org/2017 52 | 53 | # Author 54 | 55 | Yasuhiro Matsumoto (a.k.a mattn) 56 | -------------------------------------------------------------------------------- /_example/pipe.go: -------------------------------------------------------------------------------- 1 | // +build ignore 2 | 3 | package main 4 | 5 | import ( 6 | "fmt" 7 | "log" 8 | 9 | "github.com/mattn/go-shellwords" 10 | ) 11 | 12 | func isSpace(r byte) bool { 13 | switch r { 14 | case ' ', '\t', '\r', '\n': 15 | return true 16 | } 17 | return false 18 | } 19 | 20 | func main() { 21 | line := ` 22 | /usr/bin/ls -la | sort 2>&1 | tee files.log 23 | ` 24 | parser := shellwords.NewParser() 25 | 26 | for { 27 | args, err := parser.Parse(line) 28 | if err != nil { 29 | log.Fatal(err) 30 | } 31 | fmt.Println(args) 32 | if parser.Position < 0 { 33 | break 34 | } 35 | i := parser.Position 36 | for ; i < len(line); i++ { 37 | if isSpace(line[i]) { 38 | break 39 | } 40 | } 41 | fmt.Println(string([]rune(line)[parser.Position:i])) 42 | line = string([]rune(line)[i+1:]) 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/mattn/go-shellwords 2 | 3 | go 1.13 4 | -------------------------------------------------------------------------------- /go.test.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | echo "" > coverage.txt 5 | 6 | for d in $(go list ./... | grep -v vendor); do 7 | go test -coverprofile=profile.out -covermode=atomic "$d" 8 | if [ -f profile.out ]; then 9 | cat profile.out >> coverage.txt 10 | rm profile.out 11 | fi 12 | done 13 | -------------------------------------------------------------------------------- /shellwords.go: -------------------------------------------------------------------------------- 1 | package shellwords 2 | 3 | import ( 4 | "bytes" 5 | "errors" 6 | "os" 7 | "strings" 8 | "unicode" 9 | ) 10 | 11 | var ( 12 | ParseEnv bool = false 13 | ParseBacktick bool = false 14 | ) 15 | 16 | func isSpace(r rune) bool { 17 | switch r { 18 | case ' ', '\t', '\r', '\n': 19 | return true 20 | } 21 | return false 22 | } 23 | 24 | func replaceEnv(getenv func(string) string, s string) string { 25 | if getenv == nil { 26 | getenv = os.Getenv 27 | } 28 | 29 | var buf bytes.Buffer 30 | rs := []rune(s) 31 | for i := 0; i < len(rs); i++ { 32 | r := rs[i] 33 | if r == '\\' { 34 | i++ 35 | if i == len(rs) { 36 | break 37 | } 38 | buf.WriteRune(rs[i]) 39 | continue 40 | } else if r == '$' { 41 | i++ 42 | if i == len(rs) { 43 | buf.WriteRune(r) 44 | break 45 | } 46 | if rs[i] == 0x7b { 47 | i++ 48 | p := i 49 | for ; i < len(rs); i++ { 50 | r = rs[i] 51 | if r == '\\' { 52 | i++ 53 | if i == len(rs) { 54 | return s 55 | } 56 | continue 57 | } 58 | if r == 0x7d || (!unicode.IsLetter(r) && r != '_' && !unicode.IsDigit(r)) { 59 | break 60 | } 61 | } 62 | if r != 0x7d { 63 | return s 64 | } 65 | if i > p { 66 | buf.WriteString(getenv(s[p:i])) 67 | } 68 | } else { 69 | p := i 70 | for ; i < len(rs); i++ { 71 | r := rs[i] 72 | if r == '\\' { 73 | i++ 74 | if i == len(rs) { 75 | return s 76 | } 77 | continue 78 | } 79 | if !unicode.IsLetter(r) && r != '_' && !unicode.IsDigit(r) { 80 | break 81 | } 82 | } 83 | if i > p { 84 | buf.WriteString(getenv(s[p:i])) 85 | i-- 86 | } else { 87 | buf.WriteString(s[p:]) 88 | } 89 | } 90 | } else { 91 | buf.WriteRune(r) 92 | } 93 | } 94 | return buf.String() 95 | } 96 | 97 | type Parser struct { 98 | ParseEnv bool 99 | ParseBacktick bool 100 | Position int 101 | Dir string 102 | 103 | // If ParseEnv is true, use this for getenv. 104 | // If nil, use os.Getenv. 105 | Getenv func(string) string 106 | } 107 | 108 | func NewParser() *Parser { 109 | return &Parser{ 110 | ParseEnv: ParseEnv, 111 | ParseBacktick: ParseBacktick, 112 | Position: 0, 113 | Dir: "", 114 | } 115 | } 116 | 117 | type argType int 118 | 119 | const ( 120 | argNo argType = iota 121 | argSingle 122 | argQuoted 123 | ) 124 | 125 | func (p *Parser) Parse(line string) ([]string, error) { 126 | args := []string{} 127 | buf := "" 128 | var escaped, doubleQuoted, singleQuoted, backQuote, dollarQuote bool 129 | backtick := "" 130 | 131 | pos := -1 132 | got := argNo 133 | 134 | i := -1 135 | loop: 136 | for _, r := range line { 137 | i++ 138 | if escaped { 139 | if r == 't' { 140 | r = '\t' 141 | } 142 | if r == 'n' { 143 | r = '\n' 144 | } 145 | buf += string(r) 146 | escaped = false 147 | got = argSingle 148 | continue 149 | } 150 | 151 | if r == '\\' { 152 | if singleQuoted { 153 | buf += string(r) 154 | } else { 155 | escaped = true 156 | } 157 | continue 158 | } 159 | 160 | if isSpace(r) { 161 | if singleQuoted || doubleQuoted || backQuote || dollarQuote { 162 | buf += string(r) 163 | backtick += string(r) 164 | } else if got != argNo { 165 | if p.ParseEnv { 166 | if got == argSingle { 167 | parser := &Parser{ParseEnv: false, ParseBacktick: false, Position: 0, Dir: p.Dir} 168 | strs, err := parser.Parse(replaceEnv(p.Getenv, buf)) 169 | if err != nil { 170 | return nil, err 171 | } 172 | args = append(args, strs...) 173 | } else { 174 | args = append(args, replaceEnv(p.Getenv, buf)) 175 | } 176 | } else { 177 | args = append(args, buf) 178 | } 179 | buf = "" 180 | got = argNo 181 | } 182 | continue 183 | } 184 | 185 | switch r { 186 | case '`': 187 | if !singleQuoted && !doubleQuoted && !dollarQuote { 188 | if p.ParseBacktick { 189 | if backQuote { 190 | out, err := shellRun(backtick, p.Dir) 191 | if err != nil { 192 | return nil, err 193 | } 194 | buf = buf[:len(buf)-len(backtick)] + out 195 | } 196 | backtick = "" 197 | backQuote = !backQuote 198 | continue 199 | } 200 | backtick = "" 201 | backQuote = !backQuote 202 | } 203 | case ')': 204 | if !singleQuoted && !doubleQuoted && !backQuote { 205 | if p.ParseBacktick { 206 | if dollarQuote { 207 | out, err := shellRun(backtick, p.Dir) 208 | if err != nil { 209 | return nil, err 210 | } 211 | buf = buf[:len(buf)-len(backtick)-2] + out 212 | } 213 | backtick = "" 214 | dollarQuote = !dollarQuote 215 | continue 216 | } 217 | backtick = "" 218 | dollarQuote = !dollarQuote 219 | } 220 | case '(': 221 | if !singleQuoted && !doubleQuoted && !backQuote { 222 | if !dollarQuote && strings.HasSuffix(buf, "$") { 223 | dollarQuote = true 224 | buf += "(" 225 | continue 226 | } else { 227 | return nil, errors.New("invalid command line string") 228 | } 229 | } 230 | case '"': 231 | if !singleQuoted && !dollarQuote { 232 | if doubleQuoted { 233 | got = argQuoted 234 | } 235 | doubleQuoted = !doubleQuoted 236 | continue 237 | } 238 | case '\'': 239 | if !doubleQuoted && !dollarQuote { 240 | if singleQuoted { 241 | got = argQuoted 242 | } 243 | singleQuoted = !singleQuoted 244 | continue 245 | } 246 | case ';', '&', '|', '<', '>': 247 | if !(escaped || singleQuoted || doubleQuoted || backQuote || dollarQuote) { 248 | if r == '>' && len(buf) > 0 { 249 | if c := buf[0]; '0' <= c && c <= '9' { 250 | i -= 1 251 | got = argNo 252 | } 253 | } 254 | pos = i 255 | break loop 256 | } 257 | } 258 | 259 | got = argSingle 260 | buf += string(r) 261 | if backQuote || dollarQuote { 262 | backtick += string(r) 263 | } 264 | } 265 | 266 | if got != argNo { 267 | if p.ParseEnv { 268 | if got == argSingle { 269 | parser := &Parser{ParseEnv: false, ParseBacktick: false, Position: 0, Dir: p.Dir} 270 | strs, err := parser.Parse(replaceEnv(p.Getenv, buf)) 271 | if err != nil { 272 | return nil, err 273 | } 274 | args = append(args, strs...) 275 | } else { 276 | args = append(args, replaceEnv(p.Getenv, buf)) 277 | } 278 | } else { 279 | args = append(args, buf) 280 | } 281 | } 282 | 283 | if escaped || singleQuoted || doubleQuoted || backQuote || dollarQuote { 284 | return nil, errors.New("invalid command line string") 285 | } 286 | 287 | p.Position = pos 288 | 289 | return args, nil 290 | } 291 | 292 | func (p *Parser) ParseWithEnvs(line string) (envs []string, args []string, err error) { 293 | _args, err := p.Parse(line) 294 | if err != nil { 295 | return nil, nil, err 296 | } 297 | envs = []string{} 298 | args = []string{} 299 | parsingEnv := true 300 | for _, arg := range _args { 301 | if parsingEnv && isEnv(arg) { 302 | envs = append(envs, arg) 303 | } else { 304 | if parsingEnv { 305 | parsingEnv = false 306 | } 307 | args = append(args, arg) 308 | } 309 | } 310 | return envs, args, nil 311 | } 312 | 313 | func isEnv(arg string) bool { 314 | return len(strings.Split(arg, "=")) == 2 315 | } 316 | 317 | func Parse(line string) ([]string, error) { 318 | return NewParser().Parse(line) 319 | } 320 | 321 | func ParseWithEnvs(line string) (envs []string, args []string, err error) { 322 | return NewParser().ParseWithEnvs(line) 323 | } 324 | -------------------------------------------------------------------------------- /shellwords_test.go: -------------------------------------------------------------------------------- 1 | package shellwords 2 | 3 | import ( 4 | "errors" 5 | "go/build" 6 | "os" 7 | "os/exec" 8 | "path" 9 | "reflect" 10 | "testing" 11 | ) 12 | 13 | var testcases = []struct { 14 | line string 15 | expected []string 16 | }{ 17 | {``, []string{}}, 18 | {`""`, []string{``}}, 19 | {`''`, []string{``}}, 20 | {`var --bar=baz`, []string{`var`, `--bar=baz`}}, 21 | {`var --bar="baz"`, []string{`var`, `--bar=baz`}}, 22 | {`var "--bar=baz"`, []string{`var`, `--bar=baz`}}, 23 | {`var "--bar='baz'"`, []string{`var`, `--bar='baz'`}}, 24 | {"var --bar=`baz`", []string{`var`, "--bar=`baz`"}}, 25 | {`var "--bar=\"baz'"`, []string{`var`, `--bar="baz'`}}, 26 | {`var "--bar=\'baz\'"`, []string{`var`, `--bar='baz'`}}, 27 | {`var --bar='\'`, []string{`var`, `--bar=\`}}, 28 | {`var "--bar baz"`, []string{`var`, `--bar baz`}}, 29 | {`var --"bar baz"`, []string{`var`, `--bar baz`}}, 30 | {`var --"bar baz"`, []string{`var`, `--bar baz`}}, 31 | {`a "b"`, []string{`a`, `b`}}, 32 | {`a " b "`, []string{`a`, ` b `}}, 33 | {`a " "`, []string{`a`, ` `}}, 34 | {`a 'b'`, []string{`a`, `b`}}, 35 | {`a ' b '`, []string{`a`, ` b `}}, 36 | {`a ' '`, []string{`a`, ` `}}, 37 | {"foo bar\\ ", []string{`foo`, `bar `}}, 38 | {`foo "" bar ''`, []string{`foo`, ``, `bar`, ``}}, 39 | {`foo \\`, []string{`foo`, `\`}}, 40 | {`foo \& bar`, []string{`foo`, `&`, `bar`}}, 41 | {`sh -c "printf 'Hello\tworld\n'"`, []string{`sh`, `-c`, "printf 'Hello\tworld\n'"}}, 42 | } 43 | 44 | func TestSimple(t *testing.T) { 45 | for _, testcase := range testcases { 46 | args, err := Parse(testcase.line) 47 | if err != nil { 48 | t.Fatal(err) 49 | } 50 | if !reflect.DeepEqual(args, testcase.expected) { 51 | t.Fatalf("Expected %#v for %q, but %#v:", testcase.expected, testcase.line, args) 52 | } 53 | } 54 | } 55 | 56 | func TestError(t *testing.T) { 57 | _, err := Parse("foo '") 58 | if err == nil { 59 | t.Fatal("Should be an error") 60 | } 61 | _, err = Parse(`foo "`) 62 | if err == nil { 63 | t.Fatal("Should be an error") 64 | } 65 | 66 | _, err = Parse("foo `") 67 | if err == nil { 68 | t.Fatal("Should be an error") 69 | } 70 | } 71 | 72 | func TestShellRun(t *testing.T) { 73 | dir, err := os.Getwd() 74 | if err != nil { 75 | t.Fatal(err) 76 | } 77 | 78 | pwd, err := shellRun("pwd", "") 79 | if err != nil { 80 | t.Fatal(err) 81 | } 82 | 83 | pwd2, err := shellRun("pwd", path.Join(dir, "/_example")) 84 | if err != nil { 85 | t.Fatal(err) 86 | } 87 | 88 | if pwd == pwd2 { 89 | t.Fatal("`pwd` should be changed") 90 | } 91 | } 92 | 93 | func TestShellRunNoEnv(t *testing.T) { 94 | old := os.Getenv("SHELL") 95 | defer os.Setenv("SHELL", old) 96 | os.Unsetenv("SHELL") 97 | 98 | dir, err := os.Getwd() 99 | if err != nil { 100 | t.Fatal(err) 101 | } 102 | 103 | pwd, err := shellRun("pwd", "") 104 | if err != nil { 105 | t.Fatal(err) 106 | } 107 | 108 | pwd2, err := shellRun("pwd", path.Join(dir, "/_example")) 109 | if err != nil { 110 | t.Fatal(err) 111 | } 112 | 113 | if pwd == pwd2 { 114 | t.Fatal("`pwd` should be changed") 115 | } 116 | } 117 | 118 | func TestBacktick(t *testing.T) { 119 | goversion, err := shellRun("go version", "") 120 | if err != nil { 121 | t.Fatal(err) 122 | } 123 | 124 | parser := NewParser() 125 | parser.ParseBacktick = true 126 | args, err := parser.Parse("echo `go version`") 127 | if err != nil { 128 | t.Fatal(err) 129 | } 130 | expected := []string{"echo", goversion} 131 | if !reflect.DeepEqual(args, expected) { 132 | t.Fatalf("Expected %#v, but %#v:", expected, args) 133 | } 134 | 135 | args, err = parser.Parse(`echo $(echo foo)`) 136 | if err != nil { 137 | t.Fatal(err) 138 | } 139 | expected = []string{"echo", "foo"} 140 | if !reflect.DeepEqual(args, expected) { 141 | t.Fatalf("Expected %#v, but %#v:", expected, args) 142 | } 143 | 144 | args, err = parser.Parse(`echo bar=$(echo 200)cm`) 145 | if err != nil { 146 | t.Fatal(err) 147 | } 148 | expected = []string{"echo", "bar=200cm"} 149 | if !reflect.DeepEqual(args, expected) { 150 | t.Fatalf("Expected %#v, but %#v:", expected, args) 151 | } 152 | 153 | parser.ParseBacktick = false 154 | args, err = parser.Parse(`echo $(echo "foo")`) 155 | if err != nil { 156 | t.Fatal(err) 157 | } 158 | expected = []string{"echo", `$(echo "foo")`} 159 | if !reflect.DeepEqual(args, expected) { 160 | t.Fatalf("Expected %#v, but %#v:", expected, args) 161 | } 162 | args, err = parser.Parse("echo $(`echo1)") 163 | if err != nil { 164 | t.Fatal(err) 165 | } 166 | expected = []string{"echo", "$(`echo1)"} 167 | if !reflect.DeepEqual(args, expected) { 168 | t.Fatalf("Expected %#v, but %#v:", expected, args) 169 | } 170 | } 171 | 172 | func TestBacktickMulti(t *testing.T) { 173 | parser := NewParser() 174 | parser.ParseBacktick = true 175 | args, err := parser.Parse(`echo $(go env GOPATH && go env GOROOT)`) 176 | if err != nil { 177 | t.Fatal(err) 178 | } 179 | expected := []string{"echo", build.Default.GOPATH + "\n" + build.Default.GOROOT} 180 | if !reflect.DeepEqual(args, expected) { 181 | t.Fatalf("Expected %#v, but %#v:", expected, args) 182 | } 183 | } 184 | 185 | func TestBacktickError(t *testing.T) { 186 | parser := NewParser() 187 | parser.ParseBacktick = true 188 | _, err := parser.Parse("echo `go Version`") 189 | if err == nil { 190 | t.Fatal("Should be an error") 191 | } 192 | var eerr *exec.ExitError 193 | if !errors.As(err, &eerr) { 194 | t.Fatal("Should be able to unwrap to *exec.ExitError") 195 | } 196 | _, err = parser.Parse(`echo $(echo1)`) 197 | if err == nil { 198 | t.Fatal("Should be an error") 199 | } 200 | _, err = parser.Parse(`echo FOO=$(echo1)`) 201 | if err == nil { 202 | t.Fatal("Should be an error") 203 | } 204 | _, err = parser.Parse(`echo $(echo1`) 205 | if err == nil { 206 | t.Fatal("Should be an error") 207 | } 208 | _, err = parser.Parse(`echo $ (echo1`) 209 | if err == nil { 210 | t.Fatal("Should be an error") 211 | } 212 | _, err = parser.Parse(`echo (echo1`) 213 | if err == nil { 214 | t.Fatal("Should be an error") 215 | } 216 | _, err = parser.Parse(`echo )echo1`) 217 | if err == nil { 218 | t.Fatal("Should be an error") 219 | } 220 | } 221 | 222 | func TestEnv(t *testing.T) { 223 | os.Setenv("FOO", "bar") 224 | 225 | parser := NewParser() 226 | parser.ParseEnv = true 227 | args, err := parser.Parse("echo $FOO") 228 | if err != nil { 229 | t.Fatal(err) 230 | } 231 | expected := []string{"echo", "bar"} 232 | if !reflect.DeepEqual(args, expected) { 233 | t.Fatalf("Expected %#v, but %#v:", expected, args) 234 | } 235 | } 236 | 237 | func TestCustomEnv(t *testing.T) { 238 | parser := NewParser() 239 | parser.ParseEnv = true 240 | parser.Getenv = func(k string) string { return map[string]string{"FOO": "baz"}[k] } 241 | args, err := parser.Parse("echo $FOO") 242 | if err != nil { 243 | t.Fatal(err) 244 | } 245 | expected := []string{"echo", "baz"} 246 | if !reflect.DeepEqual(args, expected) { 247 | t.Fatalf("Expected %#v, but %#v:", expected, args) 248 | } 249 | } 250 | 251 | func TestNoEnv(t *testing.T) { 252 | parser := NewParser() 253 | parser.ParseEnv = true 254 | args, err := parser.Parse("echo $BAR") 255 | if err != nil { 256 | t.Fatal(err) 257 | } 258 | expected := []string{"echo"} 259 | if !reflect.DeepEqual(args, expected) { 260 | t.Fatalf("Expected %#v, but %#v:", expected, args) 261 | } 262 | } 263 | 264 | func TestEnvArguments(t *testing.T) { 265 | os.Setenv("FOO", "bar baz") 266 | 267 | parser := NewParser() 268 | parser.ParseEnv = true 269 | args, err := parser.Parse("echo $FOO") 270 | if err != nil { 271 | t.Fatal(err) 272 | } 273 | expected := []string{"echo", "bar", "baz"} 274 | if !reflect.DeepEqual(args, expected) { 275 | t.Fatalf("Expected %#v, but %#v:", expected, args) 276 | } 277 | } 278 | 279 | func TestEnvArgumentsFail(t *testing.T) { 280 | os.Setenv("FOO", "bar '") 281 | 282 | parser := NewParser() 283 | parser.ParseEnv = true 284 | _, err := parser.Parse("echo $FOO") 285 | if err == nil { 286 | t.Fatal("Should be an error") 287 | } 288 | _, err = parser.Parse("$FOO") 289 | if err == nil { 290 | t.Fatal("Should be an error") 291 | } 292 | _, err = parser.Parse("echo $FOO") 293 | if err == nil { 294 | t.Fatal("Should be an error") 295 | } 296 | os.Setenv("FOO", "bar `") 297 | result, err := parser.Parse("$FOO ") 298 | if err == nil { 299 | t.Fatal("Should be an error: ", result) 300 | } 301 | } 302 | 303 | func TestDupEnv(t *testing.T) { 304 | os.Setenv("FOO", "bar") 305 | os.Setenv("FOO_BAR", "baz") 306 | 307 | parser := NewParser() 308 | parser.ParseEnv = true 309 | args, err := parser.Parse("echo $FOO$") 310 | if err != nil { 311 | t.Fatal(err) 312 | } 313 | expected := []string{"echo", "bar$"} 314 | if !reflect.DeepEqual(args, expected) { 315 | t.Fatalf("Expected %#v, but %#v:", expected, args) 316 | } 317 | 318 | args, err = parser.Parse("echo ${FOO_BAR}$") 319 | if err != nil { 320 | t.Fatal(err) 321 | } 322 | expected = []string{"echo", "baz$"} 323 | if !reflect.DeepEqual(args, expected) { 324 | t.Fatalf("Expected %#v, but %#v:", expected, args) 325 | } 326 | } 327 | 328 | func TestHaveMore(t *testing.T) { 329 | parser := NewParser() 330 | parser.ParseEnv = true 331 | 332 | line := "echo 🍺; seq 1 10" 333 | args, err := parser.Parse(line) 334 | if err != nil { 335 | t.Fatalf(err.Error()) 336 | } 337 | expected := []string{"echo", "🍺"} 338 | if !reflect.DeepEqual(args, expected) { 339 | t.Fatalf("Expected %#v, but %#v:", expected, args) 340 | } 341 | 342 | if parser.Position == 0 { 343 | t.Fatalf("Commands should be remaining") 344 | } 345 | 346 | line = string([]rune(line)[parser.Position+1:]) 347 | args, err = parser.Parse(line) 348 | if err != nil { 349 | t.Fatalf(err.Error()) 350 | } 351 | expected = []string{"seq", "1", "10"} 352 | if !reflect.DeepEqual(args, expected) { 353 | t.Fatalf("Expected %#v, but %#v:", expected, args) 354 | } 355 | 356 | if parser.Position > 0 { 357 | t.Fatalf("Commands should not be remaining") 358 | } 359 | } 360 | 361 | func TestHaveRedirect(t *testing.T) { 362 | parser := NewParser() 363 | parser.ParseEnv = true 364 | 365 | line := "ls -la 2>foo" 366 | args, err := parser.Parse(line) 367 | if err != nil { 368 | t.Fatalf(err.Error()) 369 | } 370 | expected := []string{"ls", "-la"} 371 | if !reflect.DeepEqual(args, expected) { 372 | t.Fatalf("Expected %#v, but %#v:", expected, args) 373 | } 374 | 375 | if parser.Position == 0 { 376 | t.Fatalf("Commands should be remaining") 377 | } 378 | } 379 | 380 | func TestBackquoteInFlag(t *testing.T) { 381 | parser := NewParser() 382 | parser.ParseBacktick = true 383 | args, err := parser.Parse("cmd -flag=`echo val1` -flag=val2") 384 | if err != nil { 385 | panic(err) 386 | } 387 | expected := []string{"cmd", "-flag=val1", "-flag=val2"} 388 | if !reflect.DeepEqual(args, expected) { 389 | t.Fatalf("Expected %#v, but %#v:", expected, args) 390 | } 391 | } 392 | 393 | func TestEnvInQuoted(t *testing.T) { 394 | os.Setenv("FOO", "bar") 395 | 396 | parser := NewParser() 397 | parser.ParseEnv = true 398 | args, err := parser.Parse(`ssh 127.0.0.1 "echo $FOO"`) 399 | if err != nil { 400 | panic(err) 401 | } 402 | expected := []string{"ssh", "127.0.0.1", "echo bar"} 403 | if !reflect.DeepEqual(args, expected) { 404 | t.Fatalf("Expected %#v, but %#v:", expected, args) 405 | } 406 | 407 | args, err = parser.Parse(`ssh 127.0.0.1 "echo \\$FOO"`) 408 | if err != nil { 409 | panic(err) 410 | } 411 | expected = []string{"ssh", "127.0.0.1", "echo $FOO"} 412 | if !reflect.DeepEqual(args, expected) { 413 | t.Fatalf("Expected %#v, but %#v:", expected, args) 414 | } 415 | } 416 | 417 | func TestParseWithEnvs(t *testing.T) { 418 | tests := []struct { 419 | line string 420 | wantEnvs, wantArgs []string 421 | }{ 422 | { 423 | line: "FOO=foo cmd --args=A=B", 424 | wantEnvs: []string{"FOO=foo"}, 425 | wantArgs: []string{"cmd", "--args=A=B"}, 426 | }, 427 | { 428 | line: "FOO=foo BAR=bar cmd --args=A=B -A=B", 429 | wantEnvs: []string{"FOO=foo", "BAR=bar"}, 430 | wantArgs: []string{"cmd", "--args=A=B", "-A=B"}, 431 | }, 432 | { 433 | line: `sh -c "FOO=foo BAR=bar cmd --args=A=B -A=B"`, 434 | wantEnvs: []string{}, 435 | wantArgs: []string{"sh", "-c", "FOO=foo BAR=bar cmd --args=A=B -A=B"}, 436 | }, 437 | { 438 | line: "cmd --args=A=B -A=B", 439 | wantEnvs: []string{}, 440 | wantArgs: []string{"cmd", "--args=A=B", "-A=B"}, 441 | }, 442 | } 443 | for _, tt := range tests { 444 | t.Run(tt.line, func(t *testing.T) { 445 | envs, args, err := ParseWithEnvs(tt.line) 446 | if err != nil { 447 | t.Fatal(err) 448 | } 449 | if !reflect.DeepEqual(envs, tt.wantEnvs) { 450 | t.Errorf("Expected %#v, but %#v", tt.wantEnvs, envs) 451 | } 452 | if !reflect.DeepEqual(args, tt.wantArgs) { 453 | t.Errorf("Expected %#v, but %#v", tt.wantArgs, args) 454 | } 455 | }) 456 | } 457 | } 458 | 459 | func TestSubShellEnv(t *testing.T) { 460 | myParser := &Parser{ 461 | ParseEnv: true, 462 | } 463 | 464 | errTmpl := "bad arg parsing:\nexpected: %#v\nactual : %#v\n" 465 | 466 | t.Run("baseline", func(t *testing.T) { 467 | args, err := myParser.Parse(`program -f abc.txt`) 468 | if err != nil { 469 | t.Fatalf("err should be nil: %v", err) 470 | } 471 | expected := []string{"program", "-f", "abc.txt"} 472 | if len(args) != 3 { 473 | t.Fatalf(errTmpl, expected, args) 474 | } 475 | if args[0] != expected[0] || args[1] != expected[1] || args[2] != expected[2] { 476 | t.Fatalf(errTmpl, expected, args) 477 | } 478 | }) 479 | 480 | t.Run("single-quoted", func(t *testing.T) { 481 | args, err := myParser.Parse(`sh -c 'echo foo'`) 482 | if err != nil { 483 | t.Fatalf("err should be nil: %v", err) 484 | } 485 | expected := []string{"sh", "-c", "echo foo"} 486 | if len(args) != 3 { 487 | t.Fatalf(errTmpl, expected, args) 488 | } 489 | if args[0] != expected[0] || args[1] != expected[1] || args[2] != expected[2] { 490 | t.Fatalf(errTmpl, expected, args) 491 | } 492 | }) 493 | 494 | t.Run("double-quoted", func(t *testing.T) { 495 | args, err := myParser.Parse(`sh -c "echo foo"`) 496 | if err != nil { 497 | t.Fatalf("err should be nil: %v", err) 498 | } 499 | expected := []string{"sh", "-c", "echo foo"} 500 | if len(args) != 3 { 501 | t.Fatalf(errTmpl, expected, args) 502 | } 503 | if args[0] != expected[0] || args[1] != expected[1] || args[2] != expected[2] { 504 | t.Fatalf(errTmpl, expected, args) 505 | } 506 | }) 507 | } 508 | -------------------------------------------------------------------------------- /util_posix.go: -------------------------------------------------------------------------------- 1 | // +build !windows 2 | 3 | package shellwords 4 | 5 | import ( 6 | "fmt" 7 | "os" 8 | "os/exec" 9 | "strings" 10 | ) 11 | 12 | func shellRun(line, dir string) (string, error) { 13 | var shell string 14 | if shell = os.Getenv("SHELL"); shell == "" { 15 | shell = "/bin/sh" 16 | } 17 | cmd := exec.Command(shell, "-c", line) 18 | if dir != "" { 19 | cmd.Dir = dir 20 | } 21 | b, err := cmd.Output() 22 | if err != nil { 23 | if eerr, ok := err.(*exec.ExitError); ok { 24 | b = eerr.Stderr 25 | } 26 | return "", fmt.Errorf("%s: %w", string(b), err) 27 | } 28 | return strings.TrimSpace(string(b)), nil 29 | } 30 | -------------------------------------------------------------------------------- /util_windows.go: -------------------------------------------------------------------------------- 1 | // +build windows 2 | 3 | package shellwords 4 | 5 | import ( 6 | "fmt" 7 | "os" 8 | "os/exec" 9 | "strings" 10 | ) 11 | 12 | func shellRun(line, dir string) (string, error) { 13 | var shell string 14 | if shell = os.Getenv("COMSPEC"); shell == "" { 15 | shell = "cmd" 16 | } 17 | cmd := exec.Command(shell, "/c", line) 18 | if dir != "" { 19 | cmd.Dir = dir 20 | } 21 | b, err := cmd.Output() 22 | if err != nil { 23 | if eerr, ok := err.(*exec.ExitError); ok { 24 | b = eerr.Stderr 25 | } 26 | return "", fmt.Errorf("%s: %w", string(b), err) 27 | } 28 | return strings.TrimSpace(string(b)), nil 29 | } 30 | --------------------------------------------------------------------------------