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