summaryrefslogtreecommitdiff
path: root/render.go
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2024-01-22 09:06:33 -0500
committerGitHub <noreply@github.com>2024-01-22 09:06:33 -0500
commit0ac3aa24bf2e4ada9c26fd9ef5b7f0ae8c6b6cfb (patch)
tree9a95f27eec1e77ef8bfefcb2810f7e41681627a5 /render.go
parentf837061f953bda1e8b42095c6dba0496de11d993 (diff)
Use go-fsimpl to read from datasources (#1336)
* Use go-fsimpl to read from datasources Signed-off-by: Dave Henderson <dhenderson@gmail.com> * trying to fix windows bug Signed-off-by: Dave Henderson <dhenderson@gmail.com> * attempts to fix some of the path madness Signed-off-by: Dave Henderson <dhenderson@gmail.com> * remove 'HOME' from expected env vars Signed-off-by: Dave Henderson <dhenderson@gmail.com> * more tweaks Signed-off-by: Dave Henderson <dhenderson@gmail.com> * lint fix Signed-off-by: Dave Henderson <dhenderson@gmail.com> --------- Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'render.go')
-rw-r--r--render.go40
1 files changed, 36 insertions, 4 deletions
diff --git a/render.go b/render.go
index f3c99e54..8d79d80a 100644
--- a/render.go
+++ b/render.go
@@ -7,17 +7,25 @@ import (
"net/http"
"net/url"
"os"
+ "sync"
"text/template"
"time"
+ "github.com/hairyhenderson/go-fsimpl"
+ "github.com/hairyhenderson/go-fsimpl/autofs"
"github.com/hairyhenderson/gomplate/v4/data"
"github.com/hairyhenderson/gomplate/v4/internal/config"
+ "github.com/hairyhenderson/gomplate/v4/internal/datafs"
)
// Options for template rendering.
//
// Experimental: subject to breaking changes before the next major release
type Options struct {
+ // FSProvider - allows lookups of data source filesystems. Defaults to
+ // [DefaultFSProvider].
+ FSProvider fsimpl.FSProvider
+
// Datasources - map of datasources to be read on demand when the
// 'datasource'/'ds'/'include' functions are used.
Datasources map[string]Datasource
@@ -103,6 +111,7 @@ type Datasource struct {
type Renderer struct {
//nolint:staticcheck
data *data.Data
+ fsp fsimpl.FSProvider
nested config.Templates
funcs template.FuncMap
lDelim string
@@ -171,6 +180,10 @@ func NewRenderer(opts Options) *Renderer {
missingKey = "error"
}
+ if opts.FSProvider == nil {
+ opts.FSProvider = DefaultFSProvider
+ }
+
return &Renderer{
nested: nested,
data: d,
@@ -179,6 +192,7 @@ func NewRenderer(opts Options) *Renderer {
lDelim: opts.LDelim,
rDelim: opts.RDelim,
missingKey: missingKey,
+ fsp: opts.FSProvider,
}
}
@@ -202,10 +216,9 @@ type Template struct {
//
// Experimental: subject to breaking changes before the next major release
func (t *Renderer) RenderTemplates(ctx context.Context, templates []Template) error {
- // we need to inject the current context into the Data value, because
- // the Datasource method may need it
- // TODO: remove this in v4
- t.data.Ctx = ctx
+ if datafs.FSProviderFromContext(ctx) == nil {
+ ctx = datafs.ContextWithFSProvider(ctx, t.fsp)
+ }
// configure the template context with the refreshed Data value
// only done here because the data context may have changed
@@ -273,3 +286,22 @@ func (t *Renderer) Render(ctx context.Context, name, text string, wr io.Writer)
{Name: name, Text: text, Writer: wr},
})
}
+
+// DefaultFSProvider is the default filesystem provider used by gomplate
+var DefaultFSProvider = sync.OnceValue[fsimpl.FSProvider](
+ func() fsimpl.FSProvider {
+ fsp := fsimpl.NewMux()
+
+ // start with all go-fsimpl filesystems
+ fsp.Add(autofs.FS)
+
+ // override go-fsimpl's filefs with wdfs to handle working directories
+ fsp.Add(datafs.WdFS)
+
+ // gomplate-only filesystem
+ fsp.Add(datafs.EnvFS)
+ fsp.Add(datafs.StdinFS)
+ fsp.Add(datafs.MergeFS)
+
+ return fsp
+ })()