Skip to content

Commit 501b5b2

Browse files
authored
Merge pull request #46 from johejo/wrap_error
Use fmt.Errorf to be able to unwrap error
2 parents ce7c607 + 3136567 commit 501b5b2

File tree

6 files changed

+39
-17
lines changed

6 files changed

+39
-17
lines changed

.github/workflows/ci.yml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
[![codecov](https://codecov.io/gh/mattn/go-shellwords/branch/master/graph/badge.svg)](https://codecov.io/gh/mattn/go-shellwords)
44
[![Build Status](https://travis-ci.org/mattn/go-shellwords.svg?branch=master)](https://travis-ci.org/mattn/go-shellwords)
5-
[![GoDoc](https://godoc.org/github.com/mattn/go-shellwords?status.svg)](http://godoc.org/github.com/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)
67

78
Parse line as shell words.
89

shellwords.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"errors"
66
"os"
7-
"regexp"
87
"strings"
98
"unicode"
109
)
@@ -14,8 +13,6 @@ var (
1413
ParseBacktick bool = false
1514
)
1615

17-
var envRe = regexp.MustCompile(`\$({[a-zA-Z0-9_]+}|[a-zA-Z0-9_]+)`)
18-
1916
func isSpace(r rune) bool {
2017
switch r {
2118
case ' ', '\t', '\r', '\n':
@@ -166,9 +163,7 @@ loop:
166163
if err != nil {
167164
return nil, err
168165
}
169-
for _, str := range strs {
170-
args = append(args, str)
171-
}
166+
args = append(args, strs...)
172167
} else {
173168
args = append(args, replaceEnv(p.Getenv, buf))
174169
}
@@ -270,9 +265,7 @@ loop:
270265
if err != nil {
271266
return nil, err
272267
}
273-
for _, str := range strs {
274-
args = append(args, str)
275-
}
268+
args = append(args, strs...)
276269
} else {
277270
args = append(args, replaceEnv(p.Getenv, buf))
278271
}

shellwords_test.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package shellwords
22

33
import (
4+
"errors"
45
"go/build"
56
"os"
7+
"os/exec"
68
"path"
79
"reflect"
810
"testing"
@@ -149,6 +151,9 @@ func TestBacktick(t *testing.T) {
149151

150152
parser.ParseBacktick = false
151153
args, err = parser.Parse(`echo $(echo "foo")`)
154+
if err != nil {
155+
t.Fatal(err)
156+
}
152157
expected = []string{"echo", `$(echo "foo")`}
153158
if !reflect.DeepEqual(args, expected) {
154159
t.Fatalf("Expected %#v, but %#v:", expected, args)
@@ -183,9 +188,9 @@ func TestBacktickError(t *testing.T) {
183188
if err == nil {
184189
t.Fatal("Should be an error")
185190
}
186-
expected := "exit status 2:go Version: unknown command\nRun 'go help' for usage.\n"
187-
if expected != err.Error() {
188-
t.Fatalf("Expected %q, but %q", expected, err.Error())
191+
var eerr *exec.ExitError
192+
if !errors.As(err, &eerr) {
193+
t.Fatal("Should be able to unwrap to *exec.ExitError")
189194
}
190195
_, err = parser.Parse(`echo $(echo1)`)
191196
if err == nil {

util_posix.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
package shellwords
44

55
import (
6-
"errors"
6+
"fmt"
77
"os"
88
"os/exec"
99
"strings"
@@ -23,7 +23,7 @@ func shellRun(line, dir string) (string, error) {
2323
if eerr, ok := err.(*exec.ExitError); ok {
2424
b = eerr.Stderr
2525
}
26-
return "", errors.New(err.Error() + ":" + string(b))
26+
return "", fmt.Errorf("%s: %w", string(b), err)
2727
}
2828
return strings.TrimSpace(string(b)), nil
2929
}

util_windows.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
package shellwords
44

55
import (
6-
"errors"
6+
"fmt"
77
"os"
88
"os/exec"
99
"strings"
@@ -23,7 +23,7 @@ func shellRun(line, dir string) (string, error) {
2323
if eerr, ok := err.(*exec.ExitError); ok {
2424
b = eerr.Stderr
2525
}
26-
return "", errors.New(err.Error() + ":" + string(b))
26+
return "", fmt.Errorf("%s: %w", string(b), err)
2727
}
2828
return strings.TrimSpace(string(b)), nil
2929
}

0 commit comments

Comments
 (0)