summaryrefslogtreecommitdiff
path: root/gomplate.go
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2022-05-28 09:44:56 -0400
committerDave Henderson <dhenderson@gmail.com>2022-06-12 16:56:00 -0400
commit13b0d86d7630a89dc94b0a172b2d04ba8a236875 (patch)
tree5c5a39d70efdc30f8df19a71edab828244701e73 /gomplate.go
parent0158ecc5e479c514898f406db63454e3570c56c6 (diff)
New gomplate.Renderer interface
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'gomplate.go')
-rw-r--r--gomplate.go114
1 files changed, 29 insertions, 85 deletions
diff --git a/gomplate.go b/gomplate.go
index a0d37b91..585c9acb 100644
--- a/gomplate.go
+++ b/gomplate.go
@@ -6,8 +6,6 @@ import (
"bytes"
"context"
"fmt"
- "io"
- "os"
"path/filepath"
"strings"
"text/template"
@@ -16,47 +14,11 @@ import (
"github.com/hairyhenderson/gomplate/v3/data"
"github.com/hairyhenderson/gomplate/v3/internal/config"
"github.com/pkg/errors"
- "github.com/rs/zerolog"
)
-// gomplate -
-type gomplate struct {
- tmplctx interface{}
- funcMap template.FuncMap
- nestedTemplates config.Templates
-
- leftDelim, rightDelim string
-}
-
-// runTemplate -
-func (g *gomplate) runTemplate(ctx context.Context, t *tplate) error {
- tmpl, err := t.toGoTemplate(ctx, g)
- if err != nil {
- return err
- }
-
- wr, ok := t.target.(io.Closer)
- if ok && wr != os.Stdout {
- defer wr.Close()
- }
-
- return tmpl.Execute(t.target, g.tmplctx)
-}
-
-// newGomplate -
-func newGomplate(funcMap template.FuncMap, leftDelim, rightDelim string, nested config.Templates, tctx interface{}) *gomplate {
- return &gomplate{
- leftDelim: leftDelim,
- rightDelim: rightDelim,
- funcMap: funcMap,
- nestedTemplates: nested,
- tmplctx: tctx,
- }
-}
-
// RunTemplates - run all gomplate templates specified by the given configuration
//
-// Deprecated: use Run instead
+// Deprecated: use the Renderer interface instead
func RunTemplates(o *Config) error {
cfg, err := o.toNewConfig()
if err != nil {
@@ -67,8 +29,6 @@ func RunTemplates(o *Config) error {
// Run all gomplate templates specified by the given configuration
func Run(ctx context.Context, cfg *config.Config) error {
- log := zerolog.Ctx(ctx)
-
Metrics = newMetrics()
defer runCleanupHooks()
@@ -80,59 +40,43 @@ func Run(ctx context.Context, cfg *config.Config) error {
return fmt.Errorf("failed to validate config: %w\n%+v", err, cfg)
}
- d := data.FromConfig(ctx, cfg)
- log.Debug().Str("data", fmt.Sprintf("%+v", d)).Msg("created data from config")
-
- addCleanupHook(d.Cleanup)
-
- aliases := []string{}
- for k := range cfg.Context {
- aliases = append(aliases, k)
- }
- c, err := createTmplContext(ctx, aliases, d)
- if err != nil {
- return err
- }
-
- funcMap := CreateFuncs(ctx, d)
+ funcMap := template.FuncMap{}
err = bindPlugins(ctx, cfg, funcMap)
if err != nil {
return err
}
- g := newGomplate(funcMap, cfg.LDelim, cfg.RDelim, cfg.Templates, c)
- return g.runTemplates(ctx, cfg)
-}
+ // if a custom Stdin is set in the config, inject it into the context now
+ ctx = data.ContextWithStdin(ctx, cfg.Stdin)
+
+ opts := optionsFromConfig(cfg)
+ opts.Funcs = funcMap
+ tr := NewRenderer(opts)
-func (g *gomplate) runTemplates(ctx context.Context, cfg *config.Config) error {
start := time.Now()
- tmpl, err := gatherTemplates(ctx, cfg, chooseNamer(cfg, g))
+
+ namer := chooseNamer(cfg, tr)
+ tmpl, err := gatherTemplates(ctx, cfg, namer)
Metrics.GatherDuration = time.Since(start)
if err != nil {
Metrics.Errors++
return fmt.Errorf("failed to gather templates for rendering: %w", err)
}
Metrics.TemplatesGathered = len(tmpl)
- start = time.Now()
- defer func() { Metrics.TotalRenderDuration = time.Since(start) }()
- for _, t := range tmpl {
- tstart := time.Now()
- err := g.runTemplate(ctx, t)
- Metrics.RenderDuration[t.name] = time.Since(tstart)
- if err != nil {
- Metrics.Errors++
- return fmt.Errorf("failed to render template %s: %w", t.name, err)
- }
- Metrics.TemplatesProcessed++
+
+ err = tr.RenderTemplates(ctx, tmpl)
+ if err != nil {
+ return err
}
+
return nil
}
-func chooseNamer(cfg *config.Config, g *gomplate) func(context.Context, string) (string, error) {
+func chooseNamer(cfg *config.Config, tr *Renderer) func(context.Context, string) (string, error) {
if cfg.OutputMap == "" {
return simpleNamer(cfg.OutputDir)
}
- return mappingNamer(cfg.OutputMap, g)
+ return mappingNamer(cfg.OutputMap, tr)
}
func simpleNamer(outDir string) func(ctx context.Context, inPath string) (string, error) {
@@ -142,21 +86,19 @@ func simpleNamer(outDir string) func(ctx context.Context, inPath string) (string
}
}
-func mappingNamer(outMap string, g *gomplate) func(context.Context, string) (string, error) {
+func mappingNamer(outMap string, tr *Renderer) func(context.Context, string) (string, error) {
return func(ctx context.Context, inPath string) (string, error) {
- out := &bytes.Buffer{}
- t := &tplate{
- name: "<OutputMap>",
- contents: outMap,
- target: out,
- }
- tpl, err := t.toGoTemplate(ctx, g)
+ tr.data.Ctx = ctx
+ tcontext, err := createTmplContext(ctx, tr.tctxAliases, tr.data)
if err != nil {
return "", err
}
+
+ // add '.in' to the template context and preserve the original context
+ // in '.ctx'
tctx := &tmplctx{}
// nolint: gocritic
- switch c := g.tmplctx.(type) {
+ switch c := tcontext.(type) {
case *tmplctx:
for k, v := range *c {
if k != "in" && k != "ctx" {
@@ -164,10 +106,12 @@ func mappingNamer(outMap string, g *gomplate) func(context.Context, string) (str
}
}
}
- (*tctx)["ctx"] = g.tmplctx
+ (*tctx)["ctx"] = tcontext
(*tctx)["in"] = inPath
- err = tpl.Execute(t.target, tctx)
+ out := &bytes.Buffer{}
+ err = tr.renderTemplatesWithData(ctx,
+ []Template{{Name: "<OutputMap>", Text: outMap, Writer: out}}, tctx)
if err != nil {
return "", errors.Wrapf(err, "failed to render outputMap with ctx %+v and inPath %s", tctx, inPath)
}