Fixed server implementation and refactored
This commit is contained in:
parent
0e3eec733b
commit
22195fa00a
8 changed files with 289 additions and 215 deletions
|
|
@ -3,6 +3,7 @@ package generator
|
|||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"maps"
|
||||
"os"
|
||||
"plugin"
|
||||
|
||||
|
|
@ -10,6 +11,7 @@ import (
|
|||
"github.com/OpenCHAMI/configurator/internal/util"
|
||||
"github.com/nikolalohinski/gonja/v2"
|
||||
"github.com/nikolalohinski/gonja/v2/exec"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type Mappings = map[string]any
|
||||
|
|
@ -19,6 +21,13 @@ type Generator interface {
|
|||
Generate(config *configurator.Config, opts ...util.Option) ([]byte, error)
|
||||
}
|
||||
|
||||
type Params struct {
|
||||
Args []string
|
||||
PluginPaths []string
|
||||
Target string
|
||||
Verbose bool
|
||||
}
|
||||
|
||||
func LoadPlugin(path string) (Generator, error) {
|
||||
p, err := plugin.Open(path)
|
||||
if err != nil {
|
||||
|
|
@ -45,53 +54,33 @@ func LoadPlugins(dirpath string, opts ...util.Option) (map[string]Generator, err
|
|||
)
|
||||
|
||||
items, _ := os.ReadDir(dirpath)
|
||||
var LoadGenerator = func(path string) (Generator, error) {
|
||||
// load each generator plugin
|
||||
p, err := plugin.Open(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to load plugin: %v", err)
|
||||
}
|
||||
|
||||
// lookup symbol in plugin
|
||||
symbol, err := p.Lookup("Generator")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to look up symbol: %v", err)
|
||||
}
|
||||
|
||||
// assert that the loaded symbol is the correct type
|
||||
gen, ok := symbol.(Generator)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("failed to load the correct symbol type")
|
||||
}
|
||||
return gen, nil
|
||||
}
|
||||
for _, item := range items {
|
||||
if item.IsDir() {
|
||||
subitems, _ := os.ReadDir(item.Name())
|
||||
for _, subitem := range subitems {
|
||||
if !subitem.IsDir() {
|
||||
gen, err := LoadGenerator(subitem.Name())
|
||||
gen, err := LoadPlugin(subitem.Name())
|
||||
if err != nil {
|
||||
fmt.Printf("failed to load generator in directory '%s': %v\n", item.Name(), err)
|
||||
continue
|
||||
}
|
||||
if verbose, ok := params["verbose"].(bool); ok {
|
||||
if verbose {
|
||||
fmt.Printf("found plugin '%s'\n", item.Name())
|
||||
fmt.Printf("-- found plugin '%s'\n", item.Name())
|
||||
}
|
||||
}
|
||||
gens[gen.GetName()] = gen
|
||||
}
|
||||
}
|
||||
} else {
|
||||
gen, err := LoadGenerator(dirpath + item.Name())
|
||||
gen, err := LoadPlugin(dirpath + item.Name())
|
||||
if err != nil {
|
||||
fmt.Printf("failed to load generator: %v\n", err)
|
||||
continue
|
||||
}
|
||||
if verbose, ok := params["verbose"].(bool); ok {
|
||||
if verbose {
|
||||
fmt.Printf("found plugin '%s'\n", dirpath+item.Name())
|
||||
fmt.Printf("-- found plugin '%s'\n", dirpath+item.Name())
|
||||
}
|
||||
}
|
||||
gens[gen.GetName()] = gen
|
||||
|
|
@ -123,17 +112,15 @@ func WithClient(client configurator.SmdClient) util.Option {
|
|||
}
|
||||
}
|
||||
|
||||
// Syntactic sugar generic function to get parameter from util.Params.
|
||||
func Get[T any](params util.Params, key string) *T {
|
||||
if v, ok := params[key].(T); ok {
|
||||
return &v
|
||||
func WithOption(key string, value any) util.Option {
|
||||
return func(p util.Params) {
|
||||
p[key] = value
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Helper function to get client in generator plugins.
|
||||
func GetClient(params util.Params) *configurator.SmdClient {
|
||||
return Get[configurator.SmdClient](params, "client")
|
||||
return util.Get[configurator.SmdClient](params, "client")
|
||||
}
|
||||
|
||||
func GetParams(opts ...util.Option) util.Params {
|
||||
|
|
@ -144,10 +131,6 @@ func GetParams(opts ...util.Option) util.Params {
|
|||
return params
|
||||
}
|
||||
|
||||
func Generate(g Generator, config *configurator.Config, opts ...util.Option) {
|
||||
g.Generate(config, opts...)
|
||||
}
|
||||
|
||||
func ApplyTemplate(path string, mappings map[string]any) ([]byte, error) {
|
||||
data := exec.NewContext(mappings)
|
||||
|
||||
|
|
@ -165,3 +148,71 @@ func ApplyTemplate(path string, mappings map[string]any) ([]byte, error) {
|
|||
|
||||
return b.Bytes(), nil
|
||||
}
|
||||
|
||||
func Generate(config *configurator.Config, params Params) ([]byte, error) {
|
||||
// load generator plugins to generate configs or to print
|
||||
var (
|
||||
generators = make(map[string]Generator)
|
||||
client = configurator.SmdClient{
|
||||
Host: config.SmdClient.Host,
|
||||
Port: config.SmdClient.Port,
|
||||
AccessToken: config.AccessToken,
|
||||
}
|
||||
)
|
||||
|
||||
// make sure that we have a token present before trying to make request
|
||||
if config.AccessToken == "" {
|
||||
// TODO: make request to check if request will need token
|
||||
|
||||
// check if OCHAMI_ACCESS_TOKEN env var is set if no access token is provided and use that instead
|
||||
accessToken := os.Getenv("ACCESS_TOKEN")
|
||||
if accessToken != "" {
|
||||
config.AccessToken = accessToken
|
||||
} else {
|
||||
// TODO: try and fetch token first if it is needed
|
||||
if params.Verbose {
|
||||
fmt.Printf("No token found. Attempting to generate config without one...\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// load all plugins from params
|
||||
for _, path := range params.PluginPaths {
|
||||
if params.Verbose {
|
||||
fmt.Printf("loading plugins from '%s'\n", path)
|
||||
}
|
||||
gens, err := LoadPlugins(path)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to load plugins: %v\n", err)
|
||||
err = nil
|
||||
continue
|
||||
}
|
||||
|
||||
// add loaded generator plugins to set
|
||||
maps.Copy(generators, gens)
|
||||
}
|
||||
|
||||
// show available targets then exit
|
||||
if len(params.Args) == 0 && params.Target == "" {
|
||||
for g := range generators {
|
||||
fmt.Printf("-- found generator plugin \"%s\"\n", g)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if params.Target == "" {
|
||||
logrus.Errorf("no target supplied (--target name)")
|
||||
} else {
|
||||
// run the generator plugin from target passed
|
||||
gen := generators[params.Target]
|
||||
if gen == nil {
|
||||
return nil, fmt.Errorf("invalid generator target (%s)", params.Target)
|
||||
}
|
||||
return gen.Generate(
|
||||
config,
|
||||
WithTemplate(gen.GetName()),
|
||||
WithClient(client),
|
||||
)
|
||||
}
|
||||
return nil, fmt.Errorf("an unknown error has occurred")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue