refactor: moved plugin files
This commit is contained in:
parent
c24bcf34d4
commit
ea4819e97a
5 changed files with 372 additions and 61 deletions
98
pkg/plugins/jinja2/jinja2.go
Normal file
98
pkg/plugins/jinja2/jinja2.go
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
configurator "git.towk2.me/towk/makeshift/pkg"
|
||||
"git.towk2.me/towk/makeshift/pkg/storage"
|
||||
"github.com/nikolalohinski/gonja/v2"
|
||||
"github.com/nikolalohinski/gonja/v2/exec"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
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) Init() error {
|
||||
// nothing to initialize
|
||||
log.Debug().Str("plugin", p.Name()).Msg("jinja2.Init()")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Jinja2) Run(data storage.KVStore, args []string) error {
|
||||
// render the files using Jinja 2 from args
|
||||
var (
|
||||
rendered []string
|
||||
context *exec.Context
|
||||
template *exec.Template
|
||||
mappings map[string]any
|
||||
input any // must be a byte array
|
||||
output bytes.Buffer
|
||||
err error
|
||||
)
|
||||
log.Debug().
|
||||
Str("plugin", p.Name()).
|
||||
Any("data", data).
|
||||
// Bytes("input", input.([]byte)).
|
||||
Int("arg_count", len(args)).
|
||||
Msg("Run()")
|
||||
|
||||
input, err = data.Get("file")
|
||||
if err != nil {
|
||||
return fmt.Errorf("(jinja2) failed to get input data: %v", err)
|
||||
}
|
||||
|
||||
// get the templates provided as args to the plugin
|
||||
template, err = gonja.FromBytes(input.([]byte))
|
||||
if err != nil {
|
||||
return fmt.Errorf("(jinja2) failed to get template from args: %v", err)
|
||||
}
|
||||
|
||||
// get mappings from shared data
|
||||
shared, err := data.Get("shared")
|
||||
if err != nil {
|
||||
return fmt.Errorf("(jinja2) failed to get data from store: %v", err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(shared.([]byte), &mappings)
|
||||
if err != nil {
|
||||
return fmt.Errorf("(jinja2) failed to unmarshal mappings from shared data: %v", err)
|
||||
}
|
||||
|
||||
data.Set("mappings", mappings)
|
||||
|
||||
// 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))
|
||||
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)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Jinja2) Cleanup() error {
|
||||
// nothing to clean up
|
||||
log.Debug().Str("plugin", p.Name()).Msg("jinja2.Cleanup()")
|
||||
return nil
|
||||
}
|
||||
|
||||
var Makeshift Jinja2
|
||||
Loading…
Add table
Add a link
Reference in a new issue