summaryrefslogtreecommitdiff
path: root/gomplate.go
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2024-06-09 19:25:17 -0400
committerGitHub <noreply@github.com>2024-06-09 19:25:17 -0400
commit47b74a5505d4c9979d24a8bcffde711a60c5f23a (patch)
tree479b4331848c549d69d0e2ba1f228dddb2069402 /gomplate.go
parentdc41e375484759c09e0480b10a30f6f80318bb56 (diff)
chore(api)!: Overhauling config and rendering types (#2094)
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'gomplate.go')
-rw-r--r--gomplate.go49
1 files changed, 40 insertions, 9 deletions
diff --git a/gomplate.go b/gomplate.go
index b53a2629..586f0575 100644
--- a/gomplate.go
+++ b/gomplate.go
@@ -6,17 +6,17 @@ import (
"bytes"
"context"
"fmt"
+ "log/slog"
"path/filepath"
"strings"
"text/template"
"time"
- "github.com/hairyhenderson/gomplate/v4/internal/config"
"github.com/hairyhenderson/gomplate/v4/internal/datafs"
)
// Run all gomplate templates specified by the given configuration
-func Run(ctx context.Context, cfg *config.Config) error {
+func Run(ctx context.Context, cfg *Config) error {
Metrics = newMetrics()
// apply defaults before validation
@@ -27,6 +27,14 @@ func Run(ctx context.Context, cfg *config.Config) error {
return fmt.Errorf("failed to validate config: %w\n%+v", err, cfg)
}
+ if cfg.Experimental {
+ slog.SetDefault(slog.With("experimental", true))
+ slog.InfoContext(ctx, "experimental functions and features enabled!")
+
+ ctx = SetExperimental(ctx)
+ }
+
+ // bind plugins from the configuration to the funcMap
funcMap := template.FuncMap{}
err = bindPlugins(ctx, cfg, funcMap)
if err != nil {
@@ -36,14 +44,26 @@ func Run(ctx context.Context, cfg *config.Config) error {
// if a custom Stdin is set in the config, inject it into the context now
ctx = datafs.ContextWithStdin(ctx, cfg.Stdin)
+ // if a custom FSProvider is set in the context, use it, otherwise inject
+ // the default now - one is needed for the calls below to gatherTemplates
+ // as well as the rendering itself
+ if datafs.FSProviderFromContext(ctx) == nil {
+ ctx = datafs.ContextWithFSProvider(ctx, DefaultFSProvider)
+ }
+
+ // extract the rendering options from the config
opts := optionsFromConfig(cfg)
opts.Funcs = funcMap
tr := newRenderer(opts)
start := time.Now()
+ // figure out how to name output files (only relevant if we're dealing with an InputDir)
namer := chooseNamer(cfg, tr)
+
+ // prepare to render templates (read them in, open output writers, etc)
tmpl, err := gatherTemplates(ctx, cfg, namer)
+
Metrics.GatherDuration = time.Since(start)
if err != nil {
Metrics.Errors++
@@ -59,22 +79,33 @@ func Run(ctx context.Context, cfg *config.Config) error {
return nil
}
-func chooseNamer(cfg *config.Config, tr *renderer) func(context.Context, string) (string, error) {
+type outputNamer interface {
+ // Name the output file for the given input path
+ Name(ctx context.Context, inPath string) (string, error)
+}
+
+type outputNamerFunc func(context.Context, string) (string, error)
+
+func (f outputNamerFunc) Name(ctx context.Context, inPath string) (string, error) {
+ return f(ctx, inPath)
+}
+
+func chooseNamer(cfg *Config, tr *renderer) outputNamer {
if cfg.OutputMap == "" {
return simpleNamer(cfg.OutputDir)
}
return mappingNamer(cfg.OutputMap, tr)
}
-func simpleNamer(outDir string) func(ctx context.Context, inPath string) (string, error) {
- return func(_ context.Context, inPath string) (string, error) {
+func simpleNamer(outDir string) outputNamer {
+ return outputNamerFunc(func(_ context.Context, inPath string) (string, error) {
outPath := filepath.Join(outDir, inPath)
return filepath.Clean(outPath), nil
- }
+ })
}
-func mappingNamer(outMap string, tr *renderer) func(context.Context, string) (string, error) {
- return func(ctx context.Context, inPath string) (string, error) {
+func mappingNamer(outMap string, tr *renderer) outputNamer {
+ return outputNamerFunc(func(ctx context.Context, inPath string) (string, error) {
tcontext, err := createTmplContext(ctx, tr.tctxAliases, tr.sr)
if err != nil {
return "", err
@@ -103,5 +134,5 @@ func mappingNamer(outMap string, tr *renderer) func(context.Context, string) (st
}
return filepath.Clean(strings.TrimSpace(out.String())), nil
- }
+ })
}