From 461abe6cf45d911b3a07c3340d5786a66ce19134 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Wed, 11 Dec 2024 20:40:23 -0500 Subject: [PATCH] providers/import: Add ability for templates to read step.meta --- providers/import/import.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/providers/import/import.go b/providers/import/import.go index 29e91cc..084605d 100644 --- a/providers/import/import.go +++ b/providers/import/import.go @@ -2,15 +2,17 @@ package impor import "fmt" import "errors" +import "slices" import "strings" import "html/template" import "git.tebibyte.media/sashakoshka/step" var _ step.FuncProviderFor = new(Provider) +var _ step.Configurable = new(Provider) // Provider provides import functions. type Provider struct { - + config step.Meta } // Package fulfills the step.Provider interface. @@ -18,9 +20,14 @@ func (this *Provider) Package () string { return "import" } +func (this *Provider) Configure (config step.Meta) error { + return nil +} + // FuncMapFor fulfills the step.FuncProviderFor interface. func (this *Provider) FuncMapFor (document *step.Document) template.FuncMap { stat := &state { + config: this.config, document: document, } return template.FuncMap { @@ -31,10 +38,13 @@ func (this *Provider) FuncMapFor (document *step.Document) template.FuncMap { "execute": stat.funcExecute, "include": stat.funcInclude, "includeHTML": stat.funcIncludeHTML, + "config": stat.funcConfig, + "configList": stat.funcConfigList, } } type state struct { + config step.Meta document *step.Document } @@ -86,3 +96,14 @@ func (this *state) funcIncludeHTML (name string, data any) (template.HTML, error if err != nil { return "", err } return result.Body, nil } + +func (this *state) funcConfig (key string) string { + if this.config == nil { return "" } + return this.config.Get(key) +} + +func (this *state) funcConfigList (key string) []string { + if this.config == nil { return nil } + // config is considered immutable, time to enforce it + return slices.Clone(this.config[key]) +}