diff options
| author | Dave Henderson <dhenderson@gmail.com> | 2022-05-29 10:58:39 -0400 |
|---|---|---|
| committer | Dave Henderson <dhenderson@gmail.com> | 2022-05-29 11:12:11 -0400 |
| commit | b2df8f62bc47adb58889b273434c9f16c03301d6 (patch) | |
| tree | 40aada3f3fb0c90124a11c8ee71a25f04f369ef1 /gomplate.go | |
| parent | d9b8de8065d05ccd46e5a323f5675a46acfa183c (diff) | |
Simplify template processing
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'gomplate.go')
| -rw-r--r-- | gomplate.go | 32 |
1 files changed, 14 insertions, 18 deletions
diff --git a/gomplate.go b/gomplate.go index 91d1e026..15190d62 100644 --- a/gomplate.go +++ b/gomplate.go @@ -31,22 +31,18 @@ type gomplate struct { } // runTemplate - -func (g *gomplate) runTemplate(_ context.Context, t *tplate) error { - tmpl, err := t.toGoTemplate(g) +func (g *gomplate) runTemplate(ctx context.Context, t *tplate) error { + tmpl, err := t.toGoTemplate(ctx, g) if err != nil { return err } - // nolint: gocritic - switch t.target.(type) { - case io.Closer: - if t.target != os.Stdout { - // nolint: errcheck - defer t.target.(io.Closer).Close() - } + wr, ok := t.target.(io.Closer) + if ok && wr != os.Stdout { + defer wr.Close() } - err = tmpl.Execute(t.target, g.tmplctx) - return err + + return tmpl.Execute(t.target, g.tmplctx) } type templateAliases map[string]string @@ -165,7 +161,7 @@ func Run(ctx context.Context, cfg *config.Config) error { func (g *gomplate) runTemplates(ctx context.Context, cfg *config.Config) error { start := time.Now() - tmpl, err := gatherTemplates(cfg, chooseNamer(cfg, g)) + tmpl, err := gatherTemplates(ctx, cfg, chooseNamer(cfg, g)) Metrics.GatherDuration = time.Since(start) if err != nil { Metrics.Errors++ @@ -187,29 +183,29 @@ func (g *gomplate) runTemplates(ctx context.Context, cfg *config.Config) error { return nil } -func chooseNamer(cfg *config.Config, g *gomplate) func(string) (string, error) { +func chooseNamer(cfg *config.Config, g *gomplate) func(context.Context, string) (string, error) { if cfg.OutputMap == "" { return simpleNamer(cfg.OutputDir) } return mappingNamer(cfg.OutputMap, g) } -func simpleNamer(outDir string) func(inPath string) (string, error) { - return func(inPath string) (string, error) { +func simpleNamer(outDir string) func(ctx context.Context, inPath string) (string, error) { + return func(_ context.Context, inPath string) (string, error) { outPath := filepath.Join(outDir, inPath) return filepath.Clean(outPath), nil } } -func mappingNamer(outMap string, g *gomplate) func(string) (string, error) { - return func(inPath string) (string, error) { +func mappingNamer(outMap string, g *gomplate) 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(g) + tpl, err := t.toGoTemplate(ctx, g) if err != nil { return "", err } |
