diff --git a/TODO.md b/TODO.md index a72756b..2e189e7 100644 --- a/TODO.md +++ b/TODO.md @@ -4,4 +4,4 @@ This file used to track TODO list ## at15 -- [ ] better shell execute, see `github/hub/cmd`, which use `github.com/kballard/go-shellquote` for split and join \ No newline at end of file +- [x] better shell execute, see `github/hub/cmd`, which use `github.com/kballard/go-shellquote` for split and join \ No newline at end of file diff --git a/glide.lock b/glide.lock index 636f078..b543651 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ -hash: fdc947f5d6612b86d2e8768b21f180f5936f1718d86b71e2ea96e3c3a19269af -updated: 2016-07-07T22:55:36.5512877+08:00 +hash: b4548f5368b604731bbdadbe2ac3227698bd2742ada8cecdc464c1ceaa0e34ce +updated: 2016-07-09T00:01:57.2141105+08:00 imports: - name: github.com/BurntSushi/toml version: f0aeabca5a127c4078abb8c8d64298b147264b55 @@ -21,6 +21,9 @@ imports: - json/token - name: github.com/inconshreveable/mousetrap version: 76626ae9c91c4f2a10f34cad8ce83ea42c93bb75 +- name: github.com/kballard/go-shellquote + version: d8ec1a69a250a17bb0e419c386eac1f3711dc142 + vcs: git - name: github.com/magiconair/properties version: c265cfa48dda6474e208715ca93e987829f572f8 - name: github.com/mattn/go-colorable @@ -47,10 +50,18 @@ imports: - name: github.com/stretchr/testify version: d77da356e56a7428ad25149ca77381849a6a5232 vcs: git -- name: golang.org/x/sys - version: a408501be4d17ee978c04a618e7a1b22af058c0e subpackages: - - unix + - assert +- name: golang.org/x/sys/unix + version: "" - name: gopkg.in/yaml.v2 version: a83829b6f1293c91addabc89d0571c246397bbf4 -testImports: [] +testImports: +- name: github.com/davecgh/go-spew + version: 5215b55f46b2b919f50a1df0eaa5886afe4e3b3d + subpackages: + - spew +- name: github.com/pmezard/go-difflib + version: d8ed2627bdf02c080bf22230dbb337003b7aba2d + subpackages: + - difflib diff --git a/glide.yaml b/glide.yaml index 3c1ee72..54f0ec9 100644 --- a/glide.yaml +++ b/glide.yaml @@ -14,4 +14,7 @@ import: version: 87d4004f2ab62d0d255e0a38f1680aa534549fe3 - package: github.com/stretchr/testify vcs: git - version: d77da356e56a7428ad25149ca77381849a6a5232 \ No newline at end of file + version: d77da356e56a7428ad25149ca77381849a6a5232 +- package: github.com/kballard/go-shellquote + vcs: git + version: d8ec1a69a250a17bb0e419c386eac1f3711dc142 \ No newline at end of file diff --git a/util/command.go b/util/command.go index cf78f35..1031c72 100644 --- a/util/command.go +++ b/util/command.go @@ -3,12 +3,17 @@ package util import ( "os" "os/exec" - "strings" + + "github.com/kballard/go-shellquote" + "github.com/pkg/errors" ) // Command return a Command struct from a full commad -func Command(cmd string) *exec.Cmd { - segments := strings.Fields(cmd) +func Command(cmd string) (*exec.Cmd, error) { + segments, err := shellquote.Split(cmd) + if err != nil { + return nil, errors.Wrap(err, "Cannot parse command") + } name := segments[0] // FIXME: this is not working ... // if (name == "sh") && (segments[1] == "-c") { @@ -16,16 +21,21 @@ func Command(cmd string) *exec.Cmd { // fmt.Println(strings.Join(segments[2:], " ")) // return exec.Command("sh", "-c", strings.Join(segments[2:], " ")) // } - return exec.Command(name, segments[1:]...) + return exec.Command(name, segments[1:]...), nil } // RunCommand runs a commad and show all output in console, block current routine func RunCommand(cmd string) error { - command := Command(cmd) + command, err := Command(cmd) + if err != nil { + return err + } command.Stdin = os.Stdin command.Stdout = os.Stdout command.Stderr = os.Stderr - // TODO: wrap it up using errors - err := command.Run() - return err + err = command.Run() + if err != nil { + return errors.Wrap(err, "Failure when executing command") + } + return nil } diff --git a/util/command_test.go b/util/command_test.go index 8a432be..3497132 100644 --- a/util/command_test.go +++ b/util/command_test.go @@ -8,12 +8,17 @@ import ( func TestCommand(t *testing.T) { assert := assert.New(t) - cmd := Command("ls") + cmd, _ := Command("ls") assert.Equal(1, len(cmd.Args)) // FIXME: $(glide novendor) will not be interpreted // TODO: try sh -c "go test -v -cover $(glide novendor)" - cmd = Command("go test -v -cover $(glide novendor)") + cmd, _ = Command("go test -v -cover $(glide novendor)") assert.Equal("test", cmd.Args[1]) - // FIXME: this is also broken - // assert.Nil(RunCommand("sh -c \"echo Hi\"")) + cmd, _ = Command("sh -c \"go test -v -cover $(glide novendor)\"") + assert.Equal("go test -v -cover $(glide novendor)", cmd.Args[2]) +} + +func TestRunCommand(t *testing.T) { + assert := assert.New(t) + assert.Nil(RunCommand("sh -c \"echo Hi\"")) }