summaryrefslogtreecommitdiff
path: root/gomplate.go
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2019-11-11 16:03:16 -0500
committerDave Henderson <dhenderson@gmail.com>2020-05-03 22:12:08 -0400
commit7ff174a86a935191a684f0c63f9e2a48058fabfb (patch)
tree00f59ab63d0e581d821307df57abd5cb6f2986c0 /gomplate.go
parent8c8287777495dbb1e2b24e570db0bd504bf18372 (diff)
Support a config file to use instead of commandline arguments
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'gomplate.go')
-rw-r--r--gomplate.go46
1 files changed, 26 insertions, 20 deletions
diff --git a/gomplate.go b/gomplate.go
index d796c4dd..fe59c2ac 100644
--- a/gomplate.go
+++ b/gomplate.go
@@ -5,6 +5,7 @@ package gomplate
import (
"bytes"
"context"
+ "fmt"
"io"
"os"
"path"
@@ -14,7 +15,9 @@ import (
"time"
"github.com/hairyhenderson/gomplate/v3/data"
+ "github.com/hairyhenderson/gomplate/v3/internal/config"
"github.com/pkg/errors"
+ "github.com/rs/zerolog"
"github.com/spf13/afero"
)
@@ -109,42 +112,45 @@ func parseTemplateArg(templateArg string, ta templateAliases) error {
// RunTemplates - run all gomplate templates specified by the given configuration
func RunTemplates(o *Config) error {
- return RunTemplatesWithContext(context.Background(), o)
+ cfg, err := o.toNewConfig()
+ if err != nil {
+ return err
+ }
+ return RunTemplatesWithContext(context.Background(), cfg)
}
// RunTemplatesWithContext - run all gomplate templates specified by the given configuration
-func RunTemplatesWithContext(ctx context.Context, o *Config) error {
+func RunTemplatesWithContext(ctx context.Context, cfg *config.Config) error {
+ log := zerolog.Ctx(ctx)
+
Metrics = newMetrics()
defer runCleanupHooks()
- // make sure config is sane
- o.defaults()
- ds := append(o.DataSources, o.Contexts...)
- d, err := data.NewData(ds, o.DataSourceHeaders)
- if err != nil {
- return err
- }
+
+ d := data.FromConfig(cfg)
+ log.Debug().Str("data", fmt.Sprintf("%+v", d)).Msg("created data from config")
+
addCleanupHook(d.Cleanup)
- nested, err := parseTemplateArgs(o.Templates)
+ nested, err := parseTemplateArgs(cfg.Templates)
if err != nil {
return err
}
- c, err := createTmplContext(o.Contexts, d)
+ c, err := createTmplContext(ctx, cfg.Context, d)
if err != nil {
return err
}
funcMap := Funcs(d)
- err = bindPlugins(ctx, o.Plugins, funcMap)
+ err = bindPlugins(ctx, cfg, funcMap)
if err != nil {
return err
}
- g := newGomplate(funcMap, o.LDelim, o.RDelim, nested, c)
+ g := newGomplate(funcMap, cfg.LDelim, cfg.RDelim, nested, c)
- return g.runTemplates(ctx, o)
+ return g.runTemplates(ctx, cfg)
}
-func (g *gomplate) runTemplates(ctx context.Context, o *Config) error {
+func (g *gomplate) runTemplates(ctx context.Context, cfg *config.Config) error {
start := time.Now()
- tmpl, err := gatherTemplates(o, chooseNamer(o, g))
+ tmpl, err := gatherTemplates(cfg, chooseNamer(cfg, g))
Metrics.GatherDuration = time.Since(start)
if err != nil {
Metrics.Errors++
@@ -166,11 +172,11 @@ func (g *gomplate) runTemplates(ctx context.Context, o *Config) error {
return nil
}
-func chooseNamer(o *Config, g *gomplate) func(string) (string, error) {
- if o.OutputMap == "" {
- return simpleNamer(o.OutputDir)
+func chooseNamer(cfg *config.Config, g *gomplate) func(string) (string, error) {
+ if cfg.OutputMap == "" {
+ return simpleNamer(cfg.OutputDir)
}
- return mappingNamer(o.OutputMap, g)
+ return mappingNamer(cfg.OutputMap, g)
}
func simpleNamer(outDir string) func(inPath string) (string, error) {