refactor: updated plugin implementations

This commit is contained in:
David Allen 2025-09-20 15:35:39 -06:00
parent 505dbefb67
commit 72c52fbac6
Signed by: towk
GPG key ID: 0430CDBE22619155
4 changed files with 27 additions and 79 deletions

View file

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"git.towk2.me/towk/makeshift/internal/kwargs"
makeshift "git.towk2.me/towk/makeshift/pkg" makeshift "git.towk2.me/towk/makeshift/pkg"
"git.towk2.me/towk/makeshift/pkg/storage" "git.towk2.me/towk/makeshift/pkg/storage"
"github.com/nikolalohinski/gonja/v2" "github.com/nikolalohinski/gonja/v2"
@ -42,10 +43,11 @@ func (p *Jinja2) Run(store storage.KVStore, args []string) error {
mappings struct { mappings struct {
Data map[string]any `json:"data"` Data map[string]any `json:"data"`
} }
userdata *kwargs.KWArgs
context *exec.Context context *exec.Context
template *exec.Template template *exec.Template
profiles any // makeshift.ProfileMap profiles any // makeshift.ProfileMap
input any // []byte contents any // []byte
output bytes.Buffer output bytes.Buffer
err error err error
) )
@ -56,18 +58,26 @@ func (p *Jinja2) Run(store storage.KVStore, args []string) error {
Int("arg_count", len(args)). Int("arg_count", len(args)).
Msg("(jinja2) Run()") Msg("(jinja2) Run()")
// get profile data used as variable `{{ makeshift.profiles }}`
profiles, err = store.Get("profiles") profiles, err = store.Get("profiles")
if err != nil { if err != nil {
return fmt.Errorf("(jinja2) failed to get profiles: %v", err) 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 { if err != nil {
return fmt.Errorf("(jinja2) failed to get input data: %v", err) return fmt.Errorf("(jinja2) failed to get input data: %v", err)
} }
// get the templates provided as args to the plugin // get the templates provided as args to the plugin
template, err = gonja.FromBytes(input.([]byte)) template, err = gonja.FromBytes(contents.([]byte))
if err != nil { if err != nil {
return fmt.Errorf("(jinja2) failed to get template from args: %v", err) 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) var ps = make(map[string]any)
for profileID, profile := range profiles.(makeshift.ProfileMap) { for profileID, profile := range profiles.(makeshift.ProfileMap) {
ps[profileID] = map[string]any{ ps[profileID] = map[string]any{
@ -96,6 +107,7 @@ func (p *Jinja2) Run(store storage.KVStore, args []string) error {
mappings.Data = map[string]any{ mappings.Data = map[string]any{
"makeshift": map[string]any{ "makeshift": map[string]any{
"profiles": ps, "profiles": ps,
"userdata": userdata,
"plugin": map[string]any{ "plugin": map[string]any{
"name": p.Name(), "name": p.Name(),
"version": p.Version(), "version": p.Version(),

View file

@ -1,39 +0,0 @@
package main
import (
makeshift "git.towk2.me/towk/makeshift/pkg"
"git.towk2.me/towk/makeshift/pkg/storage"
)
type Mapper struct{}
func (p *Mapper) Name() string { return "mapper" }
func (p *Mapper) Version() string { return "v0.0.1-alpha" }
func (p *Mapper) Description() string { return "Directly maps data to store" }
func (p *Mapper) 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",
},
},
}
}
func (p *Mapper) Init() error {
// nothing to initialize
return nil
}
func (p *Mapper) Run(data storage.KVStore, args []string) error {
return nil
}
func (p *Mapper) Clean() error {
return nil
}
var Makeshift Mapper

View file

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"git.towk2.me/towk/makeshift/internal/kwargs"
makeshift "git.towk2.me/towk/makeshift/pkg" makeshift "git.towk2.me/towk/makeshift/pkg"
"git.towk2.me/towk/makeshift/pkg/storage" "git.towk2.me/towk/makeshift/pkg/storage"
jinja2 "github.com/kluctl/kluctl/lib/go-jinja2" jinja2 "github.com/kluctl/kluctl/lib/go-jinja2"
@ -42,6 +43,7 @@ func (p *Jinja2) Run(store storage.KVStore, args []string) error {
mappings struct { mappings struct {
Data map[string]any `json:"data"` Data map[string]any `json:"data"`
} }
userdata *kwargs.KWArgs
profiles any // makeshift.ProfileMap profiles any // makeshift.ProfileMap
input any // []byte input any // []byte
output string output string
@ -54,11 +56,19 @@ func (p *Jinja2) Run(store storage.KVStore, args []string) error {
Int("arg_count", len(args)). Int("arg_count", len(args)).
Msg("(pyjinja2) Run()") Msg("(pyjinja2) Run()")
// get profile data used as variable `{{ makeshift.profiles }}`
profiles, err = store.Get("profiles") profiles, err = store.Get("profiles")
if err != nil { if err != nil {
return fmt.Errorf("(pyjinja2) failed to get profiles: %v", err) return fmt.Errorf("(pyjinja2) failed to get profiles: %v", err)
} }
// get userdata used as variable `{{ makeshift.userdata }}`
userdata, err = store.GetKWArgs()
if err != nil {
return fmt.Errorf("(pyjinja2) failed to get key-word arguments: %v", err)
}
// get file contents used for templating
input, err = store.Get("file") input, err = store.Get("file")
if err != nil { if err != nil {
return fmt.Errorf("(pyjinja2) failed to get input data: %v", err) return fmt.Errorf("(pyjinja2) failed to get input data: %v", err)
@ -75,7 +85,7 @@ func (p *Jinja2) Run(store storage.KVStore, args []string) error {
} }
} }
// get mappings from provided profiles // get mappings from provided profiles `{{ makeshift.plugin.*}}`
var ps = make(map[string]any) var ps = make(map[string]any)
for profileID, profile := range profiles.(makeshift.ProfileMap) { for profileID, profile := range profiles.(makeshift.ProfileMap) {
ps[profileID] = map[string]any{ ps[profileID] = map[string]any{
@ -89,6 +99,7 @@ func (p *Jinja2) Run(store storage.KVStore, args []string) error {
mappings.Data = map[string]any{ mappings.Data = map[string]any{
"makeshift": map[string]any{ "makeshift": map[string]any{
"profiles": ps, "profiles": ps,
"userdata": userdata,
"plugin": map[string]any{ "plugin": map[string]any{
"name": p.Name(), "name": p.Name(),
"version": p.Version(), "version": p.Version(),

View file

@ -1,36 +0,0 @@
package main
import (
makeshift "git.towk2.me/towk/makeshift/pkg"
"git.towk2.me/towk/makeshift/pkg/storage"
)
type User struct{}
func (p *User) Name() string { return "user" }
func (p *User) Version() string { return "v0.0.1-alpha" }
func (p *User) Description() string { return "Get user information" }
func (p *User) 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",
},
},
}
}
func (p *User) Init() error {
return nil
}
func (p *User) Run(store storage.KVStore, args []string) error {
return nil
}
func (p *User) Cleanup() error {
return nil
}