refactor: added default plugins and check before loading
This commit is contained in:
parent
0bbd22a558
commit
9f6a8ac428
1 changed files with 50 additions and 25 deletions
|
|
@ -14,28 +14,45 @@ import (
|
||||||
"github.com/nikolalohinski/gonja/v2/exec"
|
"github.com/nikolalohinski/gonja/v2/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Mappings map[string]any
|
type (
|
||||||
type FileMap map[string][]byte
|
Mappings map[string]any
|
||||||
type FileList [][]byte
|
FileMap map[string][]byte
|
||||||
type Template []byte
|
FileList [][]byte
|
||||||
|
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.
|
||||||
type Generator interface {
|
Generator interface {
|
||||||
GetName() string
|
GetName() string
|
||||||
GetVersion() string
|
GetVersion() string
|
||||||
GetDescription() string
|
GetDescription() string
|
||||||
Generate(config *configurator.Config, opts ...util.Option) (FileMap, error)
|
Generate(config *configurator.Config, opts ...util.Option) (FileMap, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Params defined and used by the "generate" subcommand.
|
// Params defined and used by the "generate" subcommand.
|
||||||
type Params struct {
|
Params struct {
|
||||||
Args []string
|
Args []string
|
||||||
Generators map[string]Generator
|
TemplatePaths []string
|
||||||
TemplatePaths []string
|
PluginPath string
|
||||||
PluginPath string
|
Target string
|
||||||
Target string
|
Verbose bool
|
||||||
Verbose bool
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
var DefaultGenerators = createDefaultGenerators()
|
||||||
|
|
||||||
|
func createDefaultGenerators() map[string]Generator {
|
||||||
|
var (
|
||||||
|
generatorMap = map[string]Generator{}
|
||||||
|
generators = []Generator{
|
||||||
|
&Conman{}, &DHCPd{}, &DNSMasq{}, &Hostfile{},
|
||||||
|
&Powerman{}, &Syslog{}, &Warewulf{},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
for _, g := range generators {
|
||||||
|
generatorMap[g.GetName()] = g
|
||||||
|
}
|
||||||
|
return generatorMap
|
||||||
}
|
}
|
||||||
|
|
||||||
// Converts the file outputs from map[string][]byte to map[string]string.
|
// Converts the file outputs from map[string][]byte to map[string]string.
|
||||||
|
|
@ -397,6 +414,10 @@ func GenerateWithTarget(config *configurator.Config, params Params) (FileMap, er
|
||||||
configurator.WithAccessToken(config.AccessToken),
|
configurator.WithAccessToken(config.AccessToken),
|
||||||
configurator.WithCertPoolFile(config.CertPath),
|
configurator.WithCertPoolFile(config.CertPath),
|
||||||
)
|
)
|
||||||
|
target configurator.Target
|
||||||
|
generator Generator
|
||||||
|
err error
|
||||||
|
ok bool
|
||||||
)
|
)
|
||||||
|
|
||||||
// check if a target is supplied
|
// check if a target is supplied
|
||||||
|
|
@ -405,7 +426,7 @@ func GenerateWithTarget(config *configurator.Config, params Params) (FileMap, er
|
||||||
}
|
}
|
||||||
|
|
||||||
// load target information from config
|
// load target information from config
|
||||||
target, ok := config.Targets[params.Target]
|
target, ok = config.Targets[params.Target]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("target not found in config")
|
return nil, fmt.Errorf("target not found in config")
|
||||||
}
|
}
|
||||||
|
|
@ -415,10 +436,14 @@ func GenerateWithTarget(config *configurator.Config, params Params) (FileMap, er
|
||||||
target.PluginPath = params.PluginPath
|
target.PluginPath = params.PluginPath
|
||||||
}
|
}
|
||||||
|
|
||||||
// only load the plugin needed for this target
|
// check if generator is built-in first before loading
|
||||||
generator, err := LoadPlugin(target.PluginPath)
|
generator, ok = DefaultGenerators[params.Target]
|
||||||
if err != nil {
|
if !ok {
|
||||||
return nil, fmt.Errorf("failed to load plugin: %w", err)
|
// only load the plugin needed for this target if we don't find default
|
||||||
|
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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue