Skip to content

Commit

Permalink
[util] use kballard/go-shellquote
Browse files Browse the repository at this point in the history
- now `sh -c "xxx"` is working, but don't know if glide no vendor will
  work
- update glide
  • Loading branch information
at15 committed Jul 8, 2016
1 parent e68e939 commit ebc6c20
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 20 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
- [x] better shell execute, see `github/hub/cmd`, which use `github.com/kballard/go-shellquote` for split and join
23 changes: 17 additions & 6 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ import:
version: 87d4004f2ab62d0d255e0a38f1680aa534549fe3
- package: github.com/stretchr/testify
vcs: git
version: d77da356e56a7428ad25149ca77381849a6a5232
version: d77da356e56a7428ad25149ca77381849a6a5232
- package: github.com/kballard/go-shellquote
vcs: git
version: d8ec1a69a250a17bb0e419c386eac1f3711dc142
26 changes: 18 additions & 8 deletions util/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,39 @@ 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") {
// // TODO: this does not support use like go test $(glide novendor)
// 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
}
13 changes: 9 additions & 4 deletions util/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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\""))
}

0 comments on commit ebc6c20

Please sign in to comment.