feat: added jinja2 and userdata plugins

This commit is contained in:
David Allen 2025-08-19 21:33:58 -06:00
parent 5c4bbe0b58
commit 2da5ca3702
Signed by: towk
GPG key ID: 0430CDBE22619155
3 changed files with 80 additions and 13 deletions

View file

@ -1,19 +1,58 @@
package plugin
import (
"bytes"
"git.towk2.me/towk/configurator/pkg/storage"
"github.com/nikolalohinski/gonja/v2"
"github.com/nikolalohinski/gonja/v2/exec"
)
type Jinja2 struct{}
func Name() string { return "jinja2" }
func Version() string { return "test" }
func Description() string { return "Renders Jinja 2 templates" }
func Metadata() map[string]string {
func (p *Jinja2) Name() string { return "jinja2" }
func (p *Jinja2) Version() string { return "test" }
func (p *Jinja2) Description() string { return "Renders Jinja 2 templates" }
func (p *Jinja2) Metadata() map[string]string {
return map[string]string{
"author.name": "David J. Allen",
"author.email": "davidallendj@gmail.com",
}
}
func Init() {
// initialize Jinja2 (gonja)
func (p *Jinja2) Init() error {
// nothing to initialize
return nil
}
func (p *Jinja2) Run(data storage.KVStore, args []string) error {
// render the files using Jinja 2 from args
newContent := []string{}
for _, arg := range args {
var (
context *exec.Context
template *exec.Template
output bytes.Buffer
err error
)
template, err = gonja.FromString(arg)
if err != nil {
panic(err)
}
context = exec.NewContext(data.GetData().(map[string]any))
if err = template.Execute(&output, context); err != nil { // Prints: Hello Bob!
panic(err)
}
newContent = append(newContent, output.String())
}
// write back to the data storage
data.Set("out", newContent)
return nil
}
func (p *Jinja2) Cleanup() error {
// nothing to clean up
return nil
}
func Run() {}
func Cleanup() {}