From 27624dcb3d819687b4a1568960e73897a7dc808c Mon Sep 17 00:00:00 2001 From: Eeo Jun <141bytes@gmail.com> Date: Sun, 23 Sep 2018 19:35:19 +0800 Subject: [PATCH] use shellquote (#24) --- bubbler_test.go | 12 ++++++++++++ fizz.go | 9 ++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/bubbler_test.go b/bubbler_test.go index cd7011cf..dacaecb9 100644 --- a/bubbler_test.go +++ b/bubbler_test.go @@ -17,3 +17,15 @@ func Test_Exec(t *testing.T) { c("echo hello") r.Equal("hello\n", bb.String()) } + +func Test_ExecQuoted(t *testing.T) { + r := require.New(t) + + b := NewBubbler(nil) + f := fizzer{b} + bb := &bytes.Buffer{} + c := f.Exec(bb) + // without proper splitting we would get "'a b c'" + c("echo 'a b c'") + r.Equal("a b c\n", bb.String()) +} diff --git a/fizz.go b/fizz.go index 53648308..cb75fed0 100644 --- a/fizz.go +++ b/fizz.go @@ -5,8 +5,8 @@ import ( "io/ioutil" "os" "os/exec" - "strings" + "github.com/kballard/go-shellquote" "github.com/pkg/errors" ) @@ -26,12 +26,15 @@ func (f fizzer) add(s string, err error) error { func (f fizzer) Exec(out io.Writer) func(string) error { return func(s string) error { - args := strings.Split(s, " ") + args, err := shellquote.Split(s) + if err != nil { + return errors.Wrapf(err, "error parsing command: %s", s) + } cmd := exec.Command(args[0], args[1:]...) cmd.Stdin = os.Stdin cmd.Stdout = out cmd.Stderr = os.Stderr - err := cmd.Run() + err = cmd.Run() if err != nil { return errors.Wrapf(err, "error executing command: %s", s) }