/
genopts_nonwin.go
50 lines (39 loc) · 1.01 KB
/
genopts_nonwin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//go:build !windows
// +build !windows
package generator
import (
"log"
"plugin"
"text/template"
)
type GenOpts struct {
GenOptsCommon
TemplatePlugin string
}
func (g *GenOpts) setTemplates() error {
if g.TemplatePlugin != "" {
if err := g.templates.LoadPlugin(g.TemplatePlugin); err != nil {
return err
}
}
return g.GenOptsCommon.setTemplates()
}
// LoadPlugin will load the named plugin and inject its functions into the funcMap
//
// The plugin must implement a function matching the signature:
// `func AddFuncs(f template.FuncMap)`
// which can add any number of functions to the template repository funcMap.
// Any existing sprig or go-swagger templates with the same name will be overridden.
func (t *Repository) LoadPlugin(pluginPath string) error {
log.Printf("Attempting to load template plugin: %s", pluginPath)
p, err := plugin.Open(pluginPath)
if err != nil {
return err
}
f, err := p.Lookup("AddFuncs")
if err != nil {
return err
}
f.(func(template.FuncMap))(t.funcs)
return nil
}