feat: add inline and block runner. Improve execution error handling

This commit is contained in:
Justin Lin committed 2025-07-25 12:23:47 +10:00
1 parent 8f764548b9
commit a2c99c710e
6 files changed
+218 -63

No files matched your search

+2 -17
View File
@@ -2,28 +2,13 @@ package main
import (
"bytes"
"fmt"
"os/exec"
"text/template"
)
func sh(ctx TemplateContext) func(string) string {
return func(command string) string {
cmd := exec.Command("sh", "-c", command)
for k, v := range ctx {
cmd.Env = append(cmd.Env, k+"="+v)
}
out, err := cmd.Output()
if err != nil {
return fmt.Sprintf("> Failed to execute the command: %s\n> %s\n", cmd.Args, err.Error())
}
return fmt.Sprintf("\n```\n%s\n```\n", string(out))
}
}
func render(tmpl string, ctx TemplateContext) []byte {
funcMap := template.FuncMap{
"sh": sh(ctx),
"sh": ctx.inline,
"shell": ctx.codeBlock,
}
t, err := template.New("foo").Funcs(funcMap).Parse(tmpl)
if err != nil {