Use $SHELL -c for the {{sh}} template helper

pull/6/head
Mickaël Menu 3 years ago
parent 4c8a7b4105
commit 1f89978f84
No known key found for this signature in database
GPG Key ID: 53D73664CD359895

@ -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) {

@ -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())

Loading…
Cancel
Save