diff --git a/go.mod b/go.mod index 6490ed9..bc9a568 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/lancatlin/mdsh-go go 1.24.5 + +require gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..4bc0337 --- /dev/null +++ b/go.sum @@ -0,0 +1,3 @@ +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 4656f0f..56b8d7d 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,12 @@ import ( var tmpl = ` --- -title: This is the title +output: "reports/${from}.md" +params: + from: + required: true + file: + default: "ledger.j" --- Hello this is the text: {{ sh "hledger bal assets" }} @@ -50,7 +55,11 @@ func main() { if err != nil { panic(err) } - fmt.Printf("'%s'", frontmatter) + templateMeta, err := parseFrontmatter(frontmatter) + if err != nil { + panic(err) + } + fmt.Printf("'%s'", templateMeta) t, err := template.New("foo").Funcs(funcMap).Parse(body) if err != nil { panic(err) diff --git a/parse.go b/parse.go new file mode 100644 index 0000000..8796aa3 --- /dev/null +++ b/parse.go @@ -0,0 +1,16 @@ +package main + +import ( + "fmt" + + "gopkg.in/yaml.v3" +) + +func parseFrontmatter(input string) (meta TemplateMeta, err error) { + err = yaml.Unmarshal([]byte(input), &meta) + if err != nil { + err = fmt.Errorf("Failed to parse frontmatter: %w", err) + return + } + return +} diff --git a/types.go b/types.go new file mode 100644 index 0000000..e65c8ed --- /dev/null +++ b/types.go @@ -0,0 +1,21 @@ +package main + +import "encoding/json" + +type TemplateMeta struct { + Output string `yaml:"output"` + Params map[string]ParamSpec `yaml:"params"` +} + +func (meta TemplateMeta) String() string { + data, err := json.Marshal(meta) + if err != nil { + panic(err) + } + return string(data) +} + +type ParamSpec struct { + Required bool `yaml:"required"` + Default string `yaml:"default"` +}