Simplify plugin loader code.

* use one place to build plugin file names,
 * use one loader instance,
 * test for plugin enabled flag in just one place to
   avoid errors and reduce if statements,
 * don't return private objects,
 * factor goplugin loading to a method,
 * fix a related test that was commented out.
This commit is contained in:
jregan
2019-04-21 16:12:55 -07:00
committed by Jeffrey Regan
parent c9bf70fd4b
commit 76a3179868
13 changed files with 169 additions and 154 deletions

View File

@@ -22,38 +22,19 @@ import (
"sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/transformers"
"sigs.k8s.io/kustomize/pkg/types"
)
type generatorLoader struct {
pc *types.PluginConfig
ldr ifc.Loader
rf *resmap.Factory
}
func NewGeneratorLoader(
pc *types.PluginConfig,
ldr ifc.Loader, rf *resmap.Factory) generatorLoader {
return generatorLoader{pc: pc, ldr: ldr, rf: rf}
}
func (l generatorLoader) Load(
rm resmap.ResMap) ([]transformers.Generator, error) {
if len(rm) == 0 {
return nil, nil
}
if !l.pc.GoEnabled {
return nil, fmt.Errorf("plugins not enabled")
}
func (l *Loader) LoadGenerators(
ldr ifc.Loader, rm resmap.ResMap) ([]transformers.Generator, error) {
var result []transformers.Generator
for id, res := range rm {
c, err := loadAndConfigurePlugin(l.pc.DirectoryPath, id, l.ldr, l.rf, res)
for _, res := range rm {
c, err := l.loadAndConfigurePlugin(ldr, res)
if err != nil {
return nil, err
}
g, ok := c.(transformers.Generator)
if !ok {
return nil, fmt.Errorf("plugin %s not a generator", id.String())
return nil, fmt.Errorf("plugin %s not a generator", res.Id())
}
result = append(result, g)
}