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(),
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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(),
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue