github.com/go-swagger/go-swagger@v0.31.0/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  	if err != nil {
    38  		return err
    39  	}
    40  
    41  	f, err := p.Lookup("AddFuncs")
    42  	if err != nil {
    43  		return err
    44  	}
    45  
    46  	f.(func(template.FuncMap))(t.funcs)
    47  	return nil
    48  }