feat: added working rendering with plugins
This commit is contained in:
parent
fbdaf218eb
commit
1ebea8cb73
7 changed files with 139 additions and 81 deletions
|
|
@ -5,7 +5,7 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
configurator "git.towk2.me/towk/makeshift/pkg"
|
||||
makeshift "git.towk2.me/towk/makeshift/pkg"
|
||||
"git.towk2.me/towk/makeshift/pkg/storage"
|
||||
"github.com/nikolalohinski/gonja/v2"
|
||||
"github.com/nikolalohinski/gonja/v2/exec"
|
||||
|
|
@ -17,13 +17,15 @@ type Jinja2 struct{}
|
|||
func (p *Jinja2) Name() string { return "jinja2" }
|
||||
func (p *Jinja2) Version() string { return "v0.0.1-alpha" }
|
||||
func (p *Jinja2) Description() string { return "Renders Jinja 2 templates" }
|
||||
func (p *Jinja2) Metadata() configurator.Metadata {
|
||||
return configurator.Metadata{
|
||||
"author.name": "David J. Allen",
|
||||
"author.email": "davidallendj@gmail.com",
|
||||
"author.links": []string{
|
||||
"https://github.com/davidallendj",
|
||||
"https://git.towk2.me/towk",
|
||||
func (p *Jinja2) Metadata() makeshift.Metadata {
|
||||
return makeshift.Metadata{
|
||||
"author": map[string]any{
|
||||
"name": "David J. Allen",
|
||||
"email": "davidallendj@gmail.com",
|
||||
"links": []string{
|
||||
"https://github.com/davidallendj",
|
||||
"https://git.towk2.me/towk",
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
@ -34,25 +36,32 @@ func (p *Jinja2) Init() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (p *Jinja2) Run(data storage.KVStore, args []string) error {
|
||||
func (p *Jinja2) Run(store storage.KVStore, args []string) error {
|
||||
// render the files using Jinja 2 from args
|
||||
var (
|
||||
rendered []string
|
||||
mappings struct {
|
||||
Data map[string]any `json:"data"`
|
||||
}
|
||||
context *exec.Context
|
||||
template *exec.Template
|
||||
mappings map[string]any
|
||||
input any // must be a byte array
|
||||
profiles any // makeshift.ProfileMap
|
||||
input any // []byte
|
||||
output bytes.Buffer
|
||||
err error
|
||||
)
|
||||
log.Debug().
|
||||
Str("plugin", p.Name()).
|
||||
Any("data", data).
|
||||
// Bytes("input", input.([]byte)).
|
||||
Any("store", store).
|
||||
Strs("args", args).
|
||||
Int("arg_count", len(args)).
|
||||
Msg("Run()")
|
||||
Msg("(jinja2) Run()")
|
||||
|
||||
input, err = data.Get("file")
|
||||
profiles, err = store.Get("profiles")
|
||||
if err != nil {
|
||||
return fmt.Errorf("(jinja2) failed to get profiles: %v", err)
|
||||
}
|
||||
|
||||
input, err = store.Get("file")
|
||||
if err != nil {
|
||||
return fmt.Errorf("(jinja2) failed to get input data: %v", err)
|
||||
}
|
||||
|
|
@ -63,35 +72,56 @@ func (p *Jinja2) Run(data storage.KVStore, args []string) error {
|
|||
return fmt.Errorf("(jinja2) failed to get template from args: %v", err)
|
||||
}
|
||||
|
||||
// get mappings from shared data
|
||||
shared, err := data.Get("shared")
|
||||
// get mappings from shared data (optional)
|
||||
shared, err := store.Get("shared")
|
||||
if err != nil {
|
||||
return fmt.Errorf("(jinja2) failed to get data from store: %v", err)
|
||||
log.Warn().Err(err).Msg("(jinja2) could not retrieve shared data")
|
||||
} else {
|
||||
err = json.Unmarshal(shared.([]byte), &mappings)
|
||||
if err != nil {
|
||||
return fmt.Errorf("(jinja2) failed to unmarshal mappings from shared data: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
err = json.Unmarshal(shared.([]byte), &mappings)
|
||||
if err != nil {
|
||||
return fmt.Errorf("(jinja2) failed to unmarshal mappings from shared data: %v", err)
|
||||
var ps = make(map[string]any)
|
||||
for profileID, profile := range profiles.(makeshift.ProfileMap) {
|
||||
ps[profileID] = map[string]any{
|
||||
"id": profile.ID,
|
||||
"description": profile.Description,
|
||||
"data": profile.Data,
|
||||
}
|
||||
}
|
||||
|
||||
data.Set("mappings", mappings)
|
||||
// inject profiles and plugin-specific mapping
|
||||
mappings.Data = map[string]any{
|
||||
"makeshift": map[string]any{
|
||||
"profiles": ps,
|
||||
"plugin": map[string]any{
|
||||
"name": p.Name(),
|
||||
"version": p.Version(),
|
||||
"description": p.Description(),
|
||||
"metadata": p.Metadata(),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
log.Debug().Any("mappings", mappings).Send()
|
||||
|
||||
// use the provided data in the store to render templates
|
||||
// NOTE: this may be changed to specifically use "shared" data instead
|
||||
context = exec.NewContext(data.GetData().(map[string]any))
|
||||
context = exec.NewContext(mappings.Data)
|
||||
if err = template.Execute(&output, context); err != nil { // Prints: Hello Bob!
|
||||
return fmt.Errorf("(jinja2) failed to render template: %v", err)
|
||||
}
|
||||
rendered = append(rendered, output.String())
|
||||
|
||||
// write render templates to data store output
|
||||
data.Set("out", rendered)
|
||||
store.Set("out", output.String())
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Jinja2) Cleanup() error {
|
||||
// nothing to clean up
|
||||
log.Debug().Str("plugin", p.Name()).Msg("jinja2.Cleanup()")
|
||||
log.Debug().Str("plugin", p.Name()).Msg("(jinja2) Cleanup()")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue