From 70c0af82000fd07ea1b0f1ba519b06029f8055d1 Mon Sep 17 00:00:00 2001 From: Mariano Cano Date: Wed, 1 Jul 2020 18:30:04 -0700 Subject: [PATCH] Use different options to load a template from a string or file. --- x509util/options.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/x509util/options.go b/x509util/options.go index c508232c..9d855ede 100644 --- a/x509util/options.go +++ b/x509util/options.go @@ -25,7 +25,23 @@ func (o *Options) apply(opts []Option) (*Options, error) { type Option func(o *Options) error -func WithTemplate(path string, data map[string]interface{}) Option { +func WithTemplate(text string, data map[string]interface{}) Option { + return func(o *Options) error { + tmpl, err := template.New("template").Funcs(sprig.TxtFuncMap()).Parse(text) + if err != nil { + return errors.Wrapf(err, "error parsing template") + } + + buf := new(bytes.Buffer) + if err := tmpl.Execute(buf, data); err != nil { + return errors.Wrapf(err, "error executing template") + } + o.CertBuffer = buf + return nil + } +} + +func WithTemplateFile(path string, data map[string]interface{}) Option { return func(o *Options) error { filename := config.StepAbs(path) b, err := ioutil.ReadFile(filename)