From 72c52fbac653c96a88cf204ee377f79d24e4752a Mon Sep 17 00:00:00 2001 From: David Allen Date: Sat, 20 Sep 2025 15:35:39 -0600 Subject: [PATCH] refactor: updated plugin implementations --- pkg/plugins/jinja2/jinja2.go | 18 ++++++++++++--- pkg/plugins/mapper/mapper.go | 39 -------------------------------- pkg/plugins/pyjinja2/pyjinja2.go | 13 ++++++++++- pkg/plugins/user/user.go | 36 ----------------------------- 4 files changed, 27 insertions(+), 79 deletions(-) delete mode 100644 pkg/plugins/mapper/mapper.go delete mode 100644 pkg/plugins/user/user.go diff --git a/pkg/plugins/jinja2/jinja2.go b/pkg/plugins/jinja2/jinja2.go index daf6b20..6282d7c 100644 --- a/pkg/plugins/jinja2/jinja2.go +++ b/pkg/plugins/jinja2/jinja2.go @@ -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(), diff --git a/pkg/plugins/mapper/mapper.go b/pkg/plugins/mapper/mapper.go deleted file mode 100644 index 66b4a5d..0000000 --- a/pkg/plugins/mapper/mapper.go +++ /dev/null @@ -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 diff --git a/pkg/plugins/pyjinja2/pyjinja2.go b/pkg/plugins/pyjinja2/pyjinja2.go index 673f1a1..f625c26 100644 --- a/pkg/plugins/pyjinja2/pyjinja2.go +++ b/pkg/plugins/pyjinja2/pyjinja2.go @@ -4,6 +4,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" jinja2 "github.com/kluctl/kluctl/lib/go-jinja2" @@ -42,6 +43,7 @@ func (p *Jinja2) Run(store storage.KVStore, args []string) error { mappings struct { Data map[string]any `json:"data"` } + userdata *kwargs.KWArgs profiles any // makeshift.ProfileMap input any // []byte output string @@ -54,11 +56,19 @@ func (p *Jinja2) Run(store storage.KVStore, args []string) error { Int("arg_count", len(args)). Msg("(pyjinja2) Run()") + // get profile data used as variable `{{ makeshift.profiles }}` profiles, err = store.Get("profiles") if err != nil { 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") if err != nil { 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) for profileID, profile := range profiles.(makeshift.ProfileMap) { ps[profileID] = map[string]any{ @@ -89,6 +99,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(), diff --git a/pkg/plugins/user/user.go b/pkg/plugins/user/user.go deleted file mode 100644 index ff59640..0000000 --- a/pkg/plugins/user/user.go +++ /dev/null @@ -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 -}