refactor: updated plugin implementations
This commit is contained in:
parent
505dbefb67
commit
72c52fbac6
4 changed files with 27 additions and 79 deletions
|
|
@ -5,6 +5,7 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"git.towk2.me/towk/makeshift/internal/kwargs"
|
||||
makeshift "git.towk2.me/towk/makeshift/pkg"
|
||||
"git.towk2.me/towk/makeshift/pkg/storage"
|
||||
"github.com/nikolalohinski/gonja/v2"
|
||||
|
|
@ -42,10 +43,11 @@ func (p *Jinja2) Run(store storage.KVStore, args []string) error {
|
|||
mappings struct {
|
||||
Data map[string]any `json:"data"`
|
||||
}
|
||||
userdata *kwargs.KWArgs
|
||||
context *exec.Context
|
||||
template *exec.Template
|
||||
profiles any // makeshift.ProfileMap
|
||||
input any // []byte
|
||||
contents any // []byte
|
||||
output bytes.Buffer
|
||||
err error
|
||||
)
|
||||
|
|
@ -56,18 +58,26 @@ func (p *Jinja2) Run(store storage.KVStore, args []string) error {
|
|||
Int("arg_count", len(args)).
|
||||
Msg("(jinja2) Run()")
|
||||
|
||||
// get profile data used as variable `{{ makeshift.profiles }}`
|
||||
profiles, err = store.Get("profiles")
|
||||
if err != nil {
|
||||
return fmt.Errorf("(jinja2) failed to get profiles: %v", err)
|
||||
}
|
||||
|
||||
input, err = store.Get("file")
|
||||
// get userdata used as variable `{{ makeshift.userdata }}`
|
||||
userdata, err = store.GetKWArgs()
|
||||
if err != nil {
|
||||
return fmt.Errorf("(jinja2) failed to get key-word arguments: %v", err)
|
||||
}
|
||||
|
||||
// get file contents used for templating
|
||||
contents, err = store.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))
|
||||
template, err = gonja.FromBytes(contents.([]byte))
|
||||
if err != nil {
|
||||
return fmt.Errorf("(jinja2) failed to get template from args: %v", err)
|
||||
}
|
||||
|
|
@ -83,6 +93,7 @@ func (p *Jinja2) Run(store storage.KVStore, args []string) error {
|
|||
}
|
||||
}
|
||||
|
||||
// get mappings from provided profiles `{{ makeshift.plugin.*}}`
|
||||
var ps = make(map[string]any)
|
||||
for profileID, profile := range profiles.(makeshift.ProfileMap) {
|
||||
ps[profileID] = map[string]any{
|
||||
|
|
@ -96,6 +107,7 @@ func (p *Jinja2) Run(store storage.KVStore, args []string) error {
|
|||
mappings.Data = map[string]any{
|
||||
"makeshift": map[string]any{
|
||||
"profiles": ps,
|
||||
"userdata": userdata,
|
||||
"plugin": map[string]any{
|
||||
"name": p.Name(),
|
||||
"version": p.Version(),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue