diff --git a/adapter/handlebars/handlebars_test.go b/adapter/handlebars/handlebars_test.go index c630b5f..b8a38f6 100644 --- a/adapter/handlebars/handlebars_test.go +++ b/adapter/handlebars/handlebars_test.go @@ -158,6 +158,9 @@ func TestShellHelper(t *testing.T) { nil, "Hello, world!", ) + + // using pipes + testString(t, `{{sh "echo hello | tr '[:lower:]' '[:upper:]'"}}`, nil, "HELLO") } func TestStyleHelper(t *testing.T) { diff --git a/adapter/handlebars/helpers/shell.go b/adapter/handlebars/helpers/shell.go index 611d58e..b28d6b8 100644 --- a/adapter/handlebars/helpers/shell.go +++ b/adapter/handlebars/helpers/shell.go @@ -1,12 +1,11 @@ package helpers import ( - "os/exec" "strings" "github.com/aymerick/raymond" - "github.com/kballard/go-shellquote" "github.com/mickael-menu/zk/util" + "github.com/mickael-menu/zk/util/exec" ) // RegisterShell registers the {{sh}} template helper, which runs shell commands. @@ -15,17 +14,8 @@ import ( // {{sh "echo 'Hello, world!'"}} -> Hello, world! func RegisterShell(logger util.Logger) { raymond.RegisterHelper("sh", func(arg string, options *raymond.Options) string { - args, err := shellquote.Split(arg) - if err != nil { - logger.Printf("{{sh}} failed to parse command: %v: %v", arg, err) - return "" - } - if len(args) == 0 { - logger.Printf("{{sh}} expects a valid shell command, received: %v", arg) - return "" - } + cmd := exec.CommandFromString(arg) - cmd := exec.Command(args[0], args[1:]...) // Feed any block content as piped input cmd.Stdin = strings.NewReader(options.Fn())