Skip to content

Commit

Permalink
Merge pull request #24 from tengattack/master
Browse files Browse the repository at this point in the history
Add dir option for parser
  • Loading branch information
mattn committed Aug 14, 2019
2 parents 6ccda1f + 0252dcf commit c99b838
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 10 deletions.
6 changes: 4 additions & 2 deletions shellwords.go
Expand Up @@ -40,6 +40,7 @@ type Parser struct {
ParseEnv bool
ParseBacktick bool
Position int
Dir string

// If ParseEnv is true, use this for getenv.
// If nil, use os.Getenv.
Expand All @@ -51,6 +52,7 @@ func NewParser() *Parser {
ParseEnv: ParseEnv,
ParseBacktick: ParseBacktick,
Position: 0,
Dir: "",
}
}

Expand Down Expand Up @@ -100,7 +102,7 @@ loop:
if !singleQuoted && !doubleQuoted && !dollarQuote {
if p.ParseBacktick {
if backQuote {
out, err := shellRun(backtick)
out, err := shellRun(backtick, p.Dir)
if err != nil {
return nil, err
}
Expand All @@ -117,7 +119,7 @@ loop:
if !singleQuoted && !doubleQuoted && !backQuote {
if p.ParseBacktick {
if dollarQuote {
out, err := shellRun(backtick)
out, err := shellRun(backtick, p.Dir)
if err != nil {
return nil, err
}
Expand Down
24 changes: 23 additions & 1 deletion shellwords_test.go
Expand Up @@ -3,6 +3,7 @@ package shellwords
import (
"go/build"
"os"
"path"
"reflect"
"testing"
)
Expand Down Expand Up @@ -68,8 +69,29 @@ func TestLastSpace(t *testing.T) {
}
}

func TestShellRun(t *testing.T) {
dir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}

pwd, err := shellRun("pwd", "")
if err != nil {
t.Fatal(err)
}

pwd2, err := shellRun("pwd", path.Join(dir, "/_example"))
if err != nil {
t.Fatal(err)
}

if pwd == pwd2 {
t.Fatal("`pwd` should be changed")
}
}

func TestBacktick(t *testing.T) {
goversion, err := shellRun("go version")
goversion, err := shellRun("go version", "")
if err != nil {
t.Fatal(err)
}
Expand Down
11 changes: 8 additions & 3 deletions util_go15.go
Expand Up @@ -9,14 +9,19 @@ import (
"strings"
)

func shellRun(line string) (string, error) {
func shellRun(line, dir string) (string, error) {
var b []byte
var err error
var cmd *exec.Cmd
if runtime.GOOS == "windows" {
b, err = exec.Command(os.Getenv("COMSPEC"), "/c", line).Output()
cmd = exec.Command(os.Getenv("COMSPEC"), "/c", line)
} else {
b, err = exec.Command(os.Getenv("SHELL"), "-c", line).Output()
cmd = exec.Command(os.Getenv("SHELL"), "-c", line)
}
if dir != "" {
cmd.Dir = dir
}
b, err = cmd.Output()
if err != nil {
return "", err
}
Expand Down
8 changes: 6 additions & 2 deletions util_posix.go
Expand Up @@ -9,9 +9,13 @@ import (
"strings"
)

func shellRun(line string) (string, error) {
func shellRun(line, dir string) (string, error) {
shell := os.Getenv("SHELL")
b, err := exec.Command(shell, "-c", line).Output()
cmd := exec.Command(shell, "-c", line)
if dir != "" {
cmd.Dir = dir
}
b, err := cmd.Output()
if err != nil {
if eerr, ok := err.(*exec.ExitError); ok {
b = eerr.Stderr
Expand Down
8 changes: 6 additions & 2 deletions util_windows.go
Expand Up @@ -9,9 +9,13 @@ import (
"strings"
)

func shellRun(line string) (string, error) {
func shellRun(line, dir string) (string, error) {
shell := os.Getenv("COMSPEC")
b, err := exec.Command(shell, "/c", line).Output()
cmd := exec.Command(shell, "/c", line)
if dir != "" {
cmd.Dir = dir
}
b, err := cmd.Output()
if err != nil {
if eerr, ok := err.(*exec.ExitError); ok {
b = eerr.Stderr
Expand Down

0 comments on commit c99b838

Please sign in to comment.