You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
zk/internal/adapter/handlebars/helpers/shell.go

31 lines
774 B
Go

package helpers
import (
"strings"
"github.com/aymerick/raymond"
"github.com/zk-org/zk/internal/util"
"github.com/zk-org/zk/internal/util/exec"
)
// RegisterShell registers the {{sh}} template helper, which runs shell commands.
//
// {{#sh "tr '[a-z]' '[A-Z]'"}}Hello, world!{{/sh}} -> HELLO, WORLD!
// {{sh "echo 'Hello, world!'"}} -> Hello, world!
func RegisterShell(logger util.Logger) {
raymond.RegisterHelper("sh", func(arg string, options *raymond.Options) string {
cmd := exec.CommandFromString(arg)
// Feed any block content as piped input
cmd.Stdin = strings.NewReader(options.Fn())
output, err := cmd.Output()
if err != nil {
logger.Printf("{{sh}} command failed: %v", err)
return ""
}
return strings.TrimSpace(string(output))
})
}