feat: added working rendering with plugins

This commit is contained in:
David Allen 2025-08-26 22:11:17 -06:00
parent fbdaf218eb
commit 1ebea8cb73
Signed by: towk
GPG key ID: 0430CDBE22619155
7 changed files with 139 additions and 81 deletions

View file

@ -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
}

View file

@ -82,21 +82,23 @@ func (p *SmdClient) Version() string { return "v0.0.1-alpha" }
func (p *SmdClient) Description() string { return "Fetchs data from SMD and writes to store" }
func (p *SmdClient) Metadata() makeshift.Metadata {
return makeshift.Metadata{
"author.name": "David J. Allen",
"author.email": "davidallendj@gmail.com",
"author.links": []string{
"https://github.com/davidallendj",
"https://git.towk2.me/towk",
"author": map[string]any{
"name": "David J. Allen",
"email": "davidallendj@gmail.com",
"links": []string{
"https://github.com/davidallendj",
"https://git.towk2.me/towk",
},
},
}
}
func (p *SmdClient) Init() error {
log.Debug().Str("plugin", p.Name()).Msg("smd.Init()")
log.Debug().Str("plugin", p.Name()).Msg("(smd) Init()")
return nil
}
func (p *SmdClient) Run(data storage.KVStore, args []string) error {
func (p *SmdClient) Run(store storage.KVStore, args []string) error {
// set all the defaults for variables
var (
client SmdClient
@ -104,6 +106,13 @@ func (p *SmdClient) Run(data storage.KVStore, args []string) error {
err error
)
log.Debug().
Str("plugin", p.Name()).
Strs("args", args).
Int("arg_count", len(args)).
Any("store", store).
Msg("(smd) Run()")
// if we have a client, try making the request for the ethernet interfaces
err = client.FetchEthernetInterfaces()
if err != nil {
@ -124,14 +133,14 @@ func (p *SmdClient) Run(data storage.KVStore, args []string) error {
if err != nil {
return fmt.Errorf("(smd) failed to marshal SMD client: %v")
}
data.Set("shared", bytes)
store.Set("shared", bytes)
// apply template substitutions and return output as byte array
return nil
}
func (p *SmdClient) Cleanup() error {
log.Debug().Str("plugin", p.Name()).Msg("smd.Init()")
log.Debug().Str("plugin", p.Name()).Msg("(smd) Init()")
return nil
}