Changed how plugins and tempates are loaded

This commit is contained in:
David Allen 2024-09-20 16:55:31 -06:00
parent b922dbdbda
commit 601089672c
Signed by: towk
GPG key ID: 793B2924A49B3A3F

View file

@ -3,7 +3,7 @@ package generator
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"maps" "io/fs"
"os" "os"
"path/filepath" "path/filepath"
"plugin" "plugin"
@ -17,6 +17,7 @@ import (
type Mappings map[string]any type Mappings map[string]any
type FileMap map[string][]byte type FileMap map[string][]byte
type FileList [][]byte type FileList [][]byte
type Template []byte
// Generator interface used to define how files are created. Plugins can // Generator interface used to define how files are created. Plugins can
// be created entirely independent of the main driver program. // be created entirely independent of the main driver program.
@ -29,11 +30,12 @@ type Generator interface {
// Params defined and used by the "generate" subcommand. // Params defined and used by the "generate" subcommand.
type Params struct { type Params struct {
Args []string Args []string
PluginPaths []string Generators map[string]Generator
Generators map[string]Generator TemplatePaths []string
Target string PluginPath string
Verbose bool Target string
Verbose bool
} }
// Converts the file outputs from map[string][]byte to map[string]string. // Converts the file outputs from map[string][]byte to map[string]string.
@ -51,13 +53,13 @@ func LoadFiles(paths ...string) (FileMap, error) {
for _, path := range paths { for _, path := range paths {
expandedPaths, err := filepath.Glob(path) expandedPaths, err := filepath.Glob(path)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to glob path: %v", err) return nil, fmt.Errorf("failed to glob path: %w", err)
} }
for _, expandedPath := range expandedPaths { for _, expandedPath := range expandedPaths {
info, err := os.Stat(expandedPath) info, err := os.Stat(expandedPath)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return nil, fmt.Errorf("failed to stat file or directory: %v", err) return nil, fmt.Errorf("failed to stat file or directory: %w", err)
} }
// skip any directories found // skip any directories found
if info.IsDir() { if info.IsDir() {
@ -65,7 +67,7 @@ func LoadFiles(paths ...string) (FileMap, error) {
} }
b, err := os.ReadFile(expandedPath) b, err := os.ReadFile(expandedPath)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to read file: %v", err) return nil, fmt.Errorf("failed to read file: %w", err)
} }
outputs[expandedPath] = b outputs[expandedPath] = b
@ -81,19 +83,19 @@ func LoadPlugin(path string) (Generator, error) {
if isDir, err := util.IsDirectory(path); err == nil && isDir { if isDir, err := util.IsDirectory(path); err == nil && isDir {
return nil, nil return nil, nil
} else if err != nil { } else if err != nil {
return nil, fmt.Errorf("failed to test if path is directory: %v", err) return nil, fmt.Errorf("failed to test if plugin path is directory: %w", err)
} }
// try and open the plugin // try and open the plugin
p, err := plugin.Open(path) p, err := plugin.Open(path)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to open plugin: %v", err) return nil, fmt.Errorf("failed to open plugin: %w", err)
} }
// load the "Generator" symbol from plugin // load the "Generator" symbol from plugin
symbol, err := p.Lookup("Generator") symbol, err := p.Lookup("Generator")
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to look up symbol at path '%s': %v", path, err) return nil, fmt.Errorf("failed to look up symbol at path '%s': %w", path, err)
} }
// assert that the plugin loaded has a valid generator // assert that the plugin loaded has a valid generator
@ -111,45 +113,123 @@ func LoadPlugin(path string) (Generator, error) {
func LoadPlugins(dirpath string, opts ...util.Option) (map[string]Generator, error) { func LoadPlugins(dirpath string, opts ...util.Option) (map[string]Generator, error) {
// check if verbose option is supplied // check if verbose option is supplied
var ( var (
gens = make(map[string]Generator) generators = make(map[string]Generator)
params = util.GetParams(opts...) params = util.ToDict(opts...)
) )
items, _ := os.ReadDir(dirpath) //
for _, item := range items { err := filepath.Walk(dirpath, func(path string, info fs.FileInfo, err error) error {
if item.IsDir() { // skip trying to load generator plugin if directory or error
subitems, _ := os.ReadDir(item.Name()) if info.IsDir() || err != nil {
for _, subitem := range subitems { return nil
if !subitem.IsDir() { }
gen, err := LoadPlugin(subitem.Name())
if err != nil { // load the generator plugin from current path
fmt.Printf("failed to load generator in directory '%s': %v\n", item.Name(), err) gen, err := LoadPlugin(path)
continue if err != nil {
} return fmt.Errorf("failed to load generator in directory '%s': %w", path, err)
if verbose, ok := params["verbose"].(bool); ok { }
if verbose {
fmt.Printf("-- found plugin '%s'\n", item.Name()) // show the plugins found if verbose flag is set
} if params.GetVerbose() {
} fmt.Printf("-- found plugin '%s'\n", gen.GetName())
gens[gen.GetName()] = gen }
}
// map each generator plugin by name for lookup
generators[gen.GetName()] = gen
return nil
})
if err != nil {
return nil, fmt.Errorf("failed to walk directory: %w", err)
}
// items, _ := os.ReadDir(dirpath)
// for _, item := range items {
// if item.IsDir() {
// subitems, _ := os.ReadDir(item.Name())
// for _, subitem := range subitems {
// if !subitem.IsDir() {
// 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())
// }
// }
// gens[gen.GetName()] = gen
// }
// }
// } else {
// gen, err := LoadPlugin(dirpath + item.Name())
// if err != nil {
// fmt.Printf("failed to load plugin: %v\n", err)
// continue
// }
// if verbose, ok := params["verbose"].(bool); ok {
// if verbose {
// fmt.Printf("-- found plugin '%s'\n", dirpath+item.Name())
// }
// }
// gens[gen.GetName()] = gen
// }
// }
return generators, nil
}
func LoadTemplate(path string) (Template, error) {
// skip loading template if path is a directory with no error
if isDir, err := util.IsDirectory(path); err == nil && isDir {
return nil, nil
} else if err != nil {
return nil, fmt.Errorf("failed to test if template path is directory: %w", err)
}
// try and read the contents of the file
// NOTE: we don't care if this is actually a Jinja template
// or not...at least for now.
return os.ReadFile(path)
}
func LoadTemplates(paths []string, opts ...util.Option) (map[string]Template, error) {
var (
templates = make(map[string]Template)
params = util.ToDict(opts...)
)
for _, path := range paths {
err := filepath.Walk(path, func(path string, info fs.FileInfo, err error) error {
// skip trying to load generator plugin if directory or error
if info.IsDir() || err != nil {
return nil
} }
} else {
gen, err := LoadPlugin(dirpath + item.Name()) // load the contents of the template
template, err := LoadTemplate(path)
if err != nil { if err != nil {
fmt.Printf("failed to load plugin: %v\n", err) return fmt.Errorf("failed to load generator in directory '%s': %w", path, err)
continue
} }
if verbose, ok := params["verbose"].(bool); ok {
if verbose { // show the templates loaded if verbose flag is set
fmt.Printf("-- found plugin '%s'\n", dirpath+item.Name()) if params.GetVerbose() {
} fmt.Printf("-- loaded tempalte '%s'\n", path)
} }
gens[gen.GetName()] = gen
// map each template by the path it was loaded from
templates[path] = template
return nil
})
if err != nil {
return nil, fmt.Errorf("failed to walk directory: %w", err)
} }
} }
return gens, nil return templates, nil
} }
// Option to specify "target" in parameter map. This is used to set which generator // Option to specify "target" in parameter map. This is used to set which generator
@ -171,6 +251,31 @@ func WithType(_type string) util.Option {
} }
} }
// Option to the plugin to load
func WithPlugin(path string) util.Option {
return func(p util.Params) {
if p != nil {
plugin, err := LoadPlugin(path)
if err != nil {
return
}
p["plugin"] = plugin
}
}
}
func WithTemplates(paths []string) util.Option {
return func(p util.Params) {
if p != nil {
templates, err := LoadTemplates(paths)
if err != nil {
}
p["templates"] = templates
}
}
}
// Option to a specific client to include in implementing plugin generator.Generate(). // Option to a specific client to include in implementing plugin generator.Generate().
// //
// NOTE: This may be changed to pass some kind of client interface as an argument in // NOTE: This may be changed to pass some kind of client interface as an argument in
@ -217,13 +322,13 @@ func ApplyTemplates(mappings Mappings, contents ...[]byte) (FileList, error) {
// load jinja template from file // load jinja template from file
t, err := gonja.FromBytes(b) t, err := gonja.FromBytes(b)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to read template from file: %v", err) return nil, fmt.Errorf("failed to read template from file: %w", err)
} }
// execute/render jinja template // execute/render jinja template
b := bytes.Buffer{} b := bytes.Buffer{}
if err = t.Execute(&b, data); err != nil { if err = t.Execute(&b, data); err != nil {
return nil, fmt.Errorf("failed to execute: %v", err) return nil, fmt.Errorf("failed to execute: %w", err)
} }
outputs = append(outputs, b.Bytes()) outputs = append(outputs, b.Bytes())
} }
@ -243,13 +348,13 @@ func ApplyTemplateFromFiles(mappings Mappings, paths ...string) (FileMap, error)
// load jinja template from file // load jinja template from file
t, err := gonja.FromFile(path) t, err := gonja.FromFile(path)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to read template from file: %v", err) return nil, fmt.Errorf("failed to read template from file: %w", err)
} }
// execute/render jinja template // execute/render jinja template
b := bytes.Buffer{} b := bytes.Buffer{}
if err = t.Execute(&b, data); err != nil { if err = t.Execute(&b, data); err != nil {
return nil, fmt.Errorf("failed to execute: %v", err) return nil, fmt.Errorf("failed to execute: %w", err)
} }
outputs[path] = b.Bytes() outputs[path] = b.Bytes()
} }
@ -257,6 +362,23 @@ func ApplyTemplateFromFiles(mappings Mappings, paths ...string) (FileMap, error)
return outputs, nil return outputs, nil
} }
// Generate() is the main function to generate a collection of files and returns them as a map.
// This function only expects a path to a plugin and paths to a collection of templates to
// be used. This function will only load the plugin on-demand and fetch resources as needed.
func Generate(config *configurator.Config, params Params) (FileMap, error) {
var (
gen Generator
client = configurator.NewSmdClient()
)
return gen.Generate(
config,
WithPlugin(params.PluginPath),
WithTemplates(params.TemplatePaths),
WithClient(client),
)
}
// Main function to generate a collection of files as a map with the path as the key and // Main function to generate a collection of files as a map with the path as the key and
// the contents of the file as the value. This function currently expects a list of plugin // the contents of the file as the value. This function currently expects a list of plugin
// paths to load all plugins within a directory. Then, each plugin's generator.GenerateWithTarget() // paths to load all plugins within a directory. Then, each plugin's generator.GenerateWithTarget()
@ -269,8 +391,7 @@ func ApplyTemplateFromFiles(mappings Mappings, paths ...string) (FileMap, error)
func GenerateWithTarget(config *configurator.Config, params Params) (FileMap, error) { func GenerateWithTarget(config *configurator.Config, params Params) (FileMap, error) {
// load generator plugins to generate configs or to print // load generator plugins to generate configs or to print
var ( var (
generators = make(map[string]Generator) client = configurator.NewSmdClient(
client = configurator.NewSmdClient(
configurator.WithHost(config.SmdClient.Host), configurator.WithHost(config.SmdClient.Host),
configurator.WithPort(config.SmdClient.Port), configurator.WithPort(config.SmdClient.Port),
configurator.WithAccessToken(config.AccessToken), configurator.WithAccessToken(config.AccessToken),
@ -278,41 +399,32 @@ func GenerateWithTarget(config *configurator.Config, params Params) (FileMap, er
) )
) )
// load all plugins from supplied arguments // check if a target is supplied
for _, path := range params.PluginPaths { if len(params.Args) == 0 && params.Target == "" {
if params.Verbose { return nil, fmt.Errorf("must specify a target")
fmt.Printf("loading plugins from '%s'\n", path)
}
plugins, 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, plugins)
} }
// copy all generators supplied from arguments // load target information from config
maps.Copy(generators, params.Generators) target, ok := config.Targets[params.Target]
if !ok {
return nil, fmt.Errorf("target not found in config")
}
// show available targets then exit // if plugin path specified from CLI, use that instead
if len(params.Args) == 0 && params.Target == "" { if params.PluginPath != "" {
for g := range generators { target.PluginPath = params.PluginPath
fmt.Printf("-- found generator plugin \"%s\"\n", g) }
}
return nil, nil // only load the plugin needed for this target
generator, err := LoadPlugin(target.PluginPath)
if err != nil {
return nil, fmt.Errorf("failed to load plugin: %w", err)
} }
// run the generator plugin from target passed // run the generator plugin from target passed
gen := generators[params.Target] return generator.Generate(
if gen == nil {
return nil, fmt.Errorf("invalid generator target (%s)", params.Target)
}
return gen.Generate(
config, config,
WithTarget(gen.GetName()), WithTarget(generator.GetName()),
WithClient(client), WithClient(client),
) )
} }