mirror of
https://github.com/lancatlin/mdsh.git
synced 2026-11-05 19:30:12 +08:00
feat: render template context
This commit is contained in:
1 parent
99f6e8cefd
commit
437efc6f2e
4 files changed
+59
-39
No files matched your search
@@ -2,10 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
var tmpl = `
|
||||
@@ -18,39 +14,11 @@ params:
|
||||
default: "ledger.j"
|
||||
---
|
||||
Hello this is the text:
|
||||
{{ sh "hledger bal assets" }}
|
||||
Start from {{ .from }}
|
||||
{{ sh "hledger bal assets -b $from" }}
|
||||
`
|
||||
|
||||
func sh(command string) string {
|
||||
args := strings.Split(command, " ")
|
||||
out, err := exec.Command(args[0], args[1:]...).Output()
|
||||
if err != nil {
|
||||
return fmt.Sprintf("> Failed to execute the command:\n> %s\n", err.Error())
|
||||
}
|
||||
return fmt.Sprintf("\n```\n%s\n```\n", string(out))
|
||||
}
|
||||
|
||||
func parseDocument(doc string) (frontmatter string, body string, err error) {
|
||||
doc = strings.TrimLeft(doc, " \n")
|
||||
sep := "---\n"
|
||||
if !strings.HasPrefix(doc, sep) {
|
||||
err = fmt.Errorf("Invalid document: frontmatter required.\n%s", doc)
|
||||
return
|
||||
}
|
||||
parts := strings.Split(doc, sep)
|
||||
if len(parts) < 3 {
|
||||
err = fmt.Errorf("Invalid document: frontmatter required.\n%s", doc)
|
||||
return
|
||||
}
|
||||
frontmatter = parts[1]
|
||||
body = parts[2]
|
||||
return
|
||||
}
|
||||
|
||||
func main() {
|
||||
funcMap := template.FuncMap{
|
||||
"sh": sh,
|
||||
}
|
||||
frontmatter, body, err := parseDocument(tmpl)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@@ -60,9 +28,7 @@ func main() {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("'%s'", templateMeta)
|
||||
t, err := template.New("foo").Funcs(funcMap).Parse(body)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
t.Execute(os.Stdout, "hledger is awesome")
|
||||
render(body, map[string]string{
|
||||
"from": "2025-07-15",
|
||||
})
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
@@ -14,3 +15,20 @@ func parseFrontmatter(input string) (meta TemplateMeta, err error) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func parseDocument(doc string) (frontmatter string, body string, err error) {
|
||||
doc = strings.TrimLeft(doc, " \n")
|
||||
sep := "---\n"
|
||||
if !strings.HasPrefix(doc, sep) {
|
||||
err = fmt.Errorf("Invalid document: frontmatter required.\n%s", doc)
|
||||
return
|
||||
}
|
||||
parts := strings.Split(doc, sep)
|
||||
if len(parts) < 3 {
|
||||
err = fmt.Errorf("Invalid document: frontmatter required.\n%s", doc)
|
||||
return
|
||||
}
|
||||
frontmatter = parts[1]
|
||||
body = parts[2]
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"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)
|
||||
}
|
||||
fmt.Println(cmd.Environ())
|
||||
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) {
|
||||
funcMap := template.FuncMap{
|
||||
"sh": sh(ctx),
|
||||
}
|
||||
t, err := template.New("foo").Funcs(funcMap).Parse(tmpl)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
t.Execute(os.Stdout, ctx)
|
||||
}
|
||||
Reference in new issue
Block a user