summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2020-05-21 22:14:10 -0400
committerGitHub <noreply@github.com>2020-05-21 22:14:10 -0400
commit8a4530400a5f0aaec4f12dcd10cc1ea34b176894 (patch)
treee373b2a70045db608777cfd2accae6a4c36ec77d
parentcea6c6ec234198ece9155077672193229d5093b7 (diff)
parentd954aa2edae91009fcc0b0eea5e622958a8d8b67 (diff)
Merge pull request #853 from hairyhenderson/deprecate-legacy-config
Deprecate legacy config struct
-rw-r--r--cmd/gomplate/main.go2
-rw-r--r--config.go3
-rw-r--r--context.go2
-rw-r--r--context_test.go4
-rw-r--r--data/datasource_test.go6
-rw-r--r--gomplate.go8
-rw-r--r--internal/config/configfile.go80
-rw-r--r--internal/config/configfile_test.go24
8 files changed, 64 insertions, 65 deletions
diff --git a/cmd/gomplate/main.go b/cmd/gomplate/main.go
index 110a0503..cbd3f744 100644
--- a/cmd/gomplate/main.go
+++ b/cmd/gomplate/main.go
@@ -85,7 +85,7 @@ func newGomplateCmd() *cobra.Command {
Str("build", version.GitCommit).
Msgf("config is:\n%v", cfg)
- err = gomplate.RunTemplatesWithContext(ctx, cfg)
+ err = gomplate.Run(ctx, cfg)
cmd.SilenceErrors = true
cmd.SilenceUsage = true
diff --git a/config.go b/config.go
index 913370a5..49629248 100644
--- a/config.go
+++ b/config.go
@@ -9,6 +9,9 @@ import (
// Config - values necessary for rendering templates with gomplate.
// Mainly for use by the CLI
+//
+// Deprecated: this type will be phased out, internal/config.Config is used
+// everywhere else, and will be exposed as API in a future version
type Config struct {
Input string
InputFiles []string
diff --git a/context.go b/context.go
index a2b8b002..3d8bebe5 100644
--- a/context.go
+++ b/context.go
@@ -22,7 +22,7 @@ func (c *tmplctx) Env() map[string]string {
return env
}
-func createTmplContext(ctx context.Context, contexts config.DSources, d *data.Data) (interface{}, error) {
+func createTmplContext(ctx context.Context, contexts map[string]config.DataSource, d *data.Data) (interface{}, error) {
var err error
tctx := &tmplctx{}
for a := range contexts {
diff --git a/context_test.go b/context_test.go
index 94f2b51b..287376cb 100644
--- a/context_test.go
+++ b/context_test.go
@@ -43,7 +43,7 @@ func TestCreateContext(t *testing.T) {
}
os.Setenv("foo", "foo: bar")
defer os.Unsetenv("foo")
- c, err = createTmplContext(ctx, map[string]config.DSConfig{"foo": {URL: uf}}, d)
+ c, err = createTmplContext(ctx, map[string]config.DataSource{"foo": {URL: uf}}, d)
assert.NoError(t, err)
assert.IsType(t, &tmplctx{}, c)
tctx := c.(*tmplctx)
@@ -52,7 +52,7 @@ func TestCreateContext(t *testing.T) {
os.Setenv("bar", "bar: baz")
defer os.Unsetenv("bar")
- c, err = createTmplContext(ctx, map[string]config.DSConfig{".": {URL: ub}}, d)
+ c, err = createTmplContext(ctx, map[string]config.DataSource{".": {URL: ub}}, d)
assert.NoError(t, err)
assert.IsType(t, map[string]interface{}{}, c)
ds = c.(map[string]interface{})
diff --git a/data/datasource_test.go b/data/datasource_test.go
index 83a59496..737a2539 100644
--- a/data/datasource_test.go
+++ b/data/datasource_test.go
@@ -336,7 +336,7 @@ func TestFromConfig(t *testing.T) {
assert.EqualValues(t, expected, FromConfig(cfg))
cfg = &config.Config{
- DataSources: map[string]config.DSConfig{
+ DataSources: map[string]config.DataSource{
"foo": {
URL: mustParseURL("http://example.com"),
},
@@ -353,12 +353,12 @@ func TestFromConfig(t *testing.T) {
assert.EqualValues(t, expected, FromConfig(cfg))
cfg = &config.Config{
- DataSources: map[string]config.DSConfig{
+ DataSources: map[string]config.DataSource{
"foo": {
URL: mustParseURL("http://foo.com"),
},
},
- Context: map[string]config.DSConfig{
+ Context: map[string]config.DataSource{
"bar": {
URL: mustParseURL("http://bar.com"),
Header: http.Header{
diff --git a/gomplate.go b/gomplate.go
index fe59c2ac..62bd02ab 100644
--- a/gomplate.go
+++ b/gomplate.go
@@ -111,16 +111,18 @@ func parseTemplateArg(templateArg string, ta templateAliases) error {
}
// RunTemplates - run all gomplate templates specified by the given configuration
+//
+// Deprecated: use Run instead
func RunTemplates(o *Config) error {
cfg, err := o.toNewConfig()
if err != nil {
return err
}
- return RunTemplatesWithContext(context.Background(), cfg)
+ return Run(context.Background(), cfg)
}
-// RunTemplatesWithContext - run all gomplate templates specified by the given configuration
-func RunTemplatesWithContext(ctx context.Context, cfg *config.Config) error {
+// Run all gomplate templates specified by the given configuration
+func Run(ctx context.Context, cfg *config.Config) error {
log := zerolog.Ctx(ctx)
Metrics = newMetrics()
diff --git a/internal/config/configfile.go b/internal/config/configfile.go
index 55897fa4..19105633 100644
--- a/internal/config/configfile.go
+++ b/internal/config/configfile.go
@@ -17,11 +17,6 @@ import (
"gopkg.in/yaml.v3"
)
-var (
- // PluginTimeoutKey - context key for PluginTimeout - temporary!
- PluginTimeoutKey = struct{}{}
-)
-
// Parse a config file
func Parse(in io.Reader) (*Config, error) {
out := &Config{}
@@ -33,7 +28,7 @@ func Parse(in io.Reader) (*Config, error) {
return out, nil
}
-// Config -
+// Config - configures the gomplate execution
type Config struct {
Input string `yaml:"in,omitempty"`
InputFiles []string `yaml:"inputFiles,omitempty,flow"`
@@ -47,14 +42,14 @@ type Config struct {
ExecPipe bool `yaml:"execPipe,omitempty"`
PostExec []string `yaml:"postExec,omitempty,flow"`
- OutMode string `yaml:"chmod,omitempty"`
- LDelim string `yaml:"leftDelim,omitempty"`
- RDelim string `yaml:"rightDelim,omitempty"`
- DataSources DSources `yaml:"datasources,omitempty"`
- Context DSources `yaml:"context,omitempty"`
- Plugins map[string]string `yaml:"plugins,omitempty"`
- PluginTimeout time.Duration `yaml:"pluginTimeout,omitempty"`
- Templates []string `yaml:"templates,omitempty"`
+ OutMode string `yaml:"chmod,omitempty"`
+ LDelim string `yaml:"leftDelim,omitempty"`
+ RDelim string `yaml:"rightDelim,omitempty"`
+ DataSources map[string]DataSource `yaml:"datasources,omitempty"`
+ Context map[string]DataSource `yaml:"context,omitempty"`
+ Plugins map[string]string `yaml:"plugins,omitempty"`
+ PluginTimeout time.Duration `yaml:"pluginTimeout,omitempty"`
+ Templates []string `yaml:"templates,omitempty"`
// Extra HTTP headers not attached to pre-defined datsources. Potentially
// used by datasources defined in the template.
@@ -65,10 +60,8 @@ type Config struct {
OutWriter io.Writer `yaml:"-"`
}
-// DSources - map of datasource configs
-type DSources map[string]DSConfig
-
-func (d DSources) mergeFrom(o DSources) DSources {
+// mergeDataSources - use d as defaults, and override with values from o
+func mergeDataSources(d, o map[string]DataSource) map[string]DataSource {
for k, v := range o {
c, ok := d[k]
if ok {
@@ -80,15 +73,15 @@ func (d DSources) mergeFrom(o DSources) DSources {
return d
}
-// DSConfig - datasource config
-type DSConfig struct {
+// DataSource - datasource configuration
+type DataSource struct {
URL *url.URL `yaml:"-"`
Header http.Header `yaml:"header,omitempty,flow"`
}
// UnmarshalYAML - satisfy the yaml.Umarshaler interface - URLs aren't
// well supported, and anyway we need to do some extra parsing
-func (d *DSConfig) UnmarshalYAML(value *yaml.Node) error {
+func (d *DataSource) UnmarshalYAML(value *yaml.Node) error {
type raw struct {
URL string
Header http.Header
@@ -102,7 +95,7 @@ func (d *DSConfig) UnmarshalYAML(value *yaml.Node) error {
if err != nil {
return fmt.Errorf("could not parse datasource URL %q: %w", r.URL, err)
}
- *d = DSConfig{
+ *d = DataSource{
URL: u,
Header: r.Header,
}
@@ -111,7 +104,7 @@ func (d *DSConfig) UnmarshalYAML(value *yaml.Node) error {
// MarshalYAML - satisfy the yaml.Marshaler interface - URLs aren't
// well supported, and anyway we need to do some extra parsing
-func (d DSConfig) MarshalYAML() (interface{}, error) {
+func (d DataSource) MarshalYAML() (interface{}, error) {
type raw struct {
URL string
Header http.Header
@@ -123,7 +116,8 @@ func (d DSConfig) MarshalYAML() (interface{}, error) {
return r, nil
}
-func (d DSConfig) mergeFrom(o DSConfig) DSConfig {
+// mergeFrom - use this as default, and override with values from o
+func (d DataSource) mergeFrom(o DataSource) DataSource {
if o.URL != nil {
d.URL = o.URL
}
@@ -197,8 +191,8 @@ func (c *Config) MergeFrom(o *Config) *Config {
if !isZero(o.Templates) {
c.Templates = o.Templates
}
- c.DataSources.mergeFrom(o.DataSources)
- c.Context.mergeFrom(o.Context)
+ mergeDataSources(c.DataSources, o.DataSources)
+ mergeDataSources(c.Context, o.Context)
if len(o.Plugins) > 0 {
for k, v := range o.Plugins {
c.Plugins[k] = v
@@ -217,7 +211,7 @@ func (c *Config) ParseDataSourceFlags(datasources, contexts, headers []string) e
return err
}
if c.DataSources == nil {
- c.DataSources = DSources{}
+ c.DataSources = map[string]DataSource{}
}
c.DataSources[k] = ds
}
@@ -227,7 +221,7 @@ func (c *Config) ParseDataSourceFlags(datasources, contexts, headers []string) e
return err
}
if c.Context == nil {
- c.Context = DSources{}
+ c.Context = map[string]DataSource{}
}
c.Context[k] = ds
}
@@ -271,7 +265,7 @@ func (c *Config) ParsePluginFlags(plugins []string) error {
return nil
}
-func parseDatasourceArg(value string) (key string, ds DSConfig, err error) {
+func parseDatasourceArg(value string) (key string, ds DataSource, err error) {
parts := strings.SplitN(value, "=", 2)
if len(parts) == 1 {
f := parts[0]
@@ -446,6 +440,20 @@ func (c *Config) ApplyDefaults() {
}
}
+// GetMode - parse an os.FileMode out of the string, and let us know if it's an override or not...
+func (c *Config) GetMode() (os.FileMode, bool, error) {
+ modeOverride := c.OutMode != ""
+ m, err := strconv.ParseUint("0"+c.OutMode, 8, 32)
+ if err != nil {
+ return 0, false, err
+ }
+ mode := os.FileMode(m)
+ if mode == 0 && c.Input != "" {
+ mode = 0644
+ }
+ return mode, modeOverride, nil
+}
+
// String -
func (c *Config) String() string {
out := &strings.Builder{}
@@ -526,17 +534,3 @@ func absFileURL(value string) (*url.URL, error) {
}
return resolved, nil
}
-
-// GetMode - parse an os.FileMode out of the string, and let us know if it's an override or not...
-func (c *Config) GetMode() (os.FileMode, bool, error) {
- modeOverride := c.OutMode != ""
- m, err := strconv.ParseUint("0"+c.OutMode, 8, 32)
- if err != nil {
- return 0, false, err
- }
- mode := os.FileMode(m)
- if mode == 0 && c.Input != "" {
- mode = 0644
- }
- return mode, modeOverride, nil
-}
diff --git a/internal/config/configfile_test.go b/internal/config/configfile_test.go
index 9d6d8004..039467e3 100644
--- a/internal/config/configfile_test.go
+++ b/internal/config/configfile_test.go
@@ -45,7 +45,7 @@ pluginTimeout: 2s
expected = &Config{
Input: "hello world",
OutputFiles: []string{"out.txt"},
- DataSources: map[string]DSConfig{
+ DataSources: map[string]DataSource{
"data": {
URL: mustURL("file:///data.json"),
},
@@ -56,7 +56,7 @@ pluginTimeout: 2s
},
},
},
- Context: map[string]DSConfig{
+ Context: map[string]DataSource{
".": {
URL: mustURL("file:///data.json"),
},
@@ -177,7 +177,7 @@ func TestMergeFrom(t *testing.T) {
t.Parallel()
cfg := &Config{
Input: "hello world",
- DataSources: map[string]DSConfig{
+ DataSources: map[string]DataSource{
"data": {
URL: mustURL("file:///data.json"),
},
@@ -188,7 +188,7 @@ func TestMergeFrom(t *testing.T) {
},
},
},
- Context: map[string]DSConfig{
+ Context: map[string]DataSource{
"foo": {
URL: mustURL("https://example.com/foo.yaml"),
Header: http.Header{
@@ -200,14 +200,14 @@ func TestMergeFrom(t *testing.T) {
}
other := &Config{
OutputFiles: []string{"out.txt"},
- DataSources: map[string]DSConfig{
+ DataSources: map[string]DataSource{
"data": {
Header: http.Header{
"Accept": {"foo/bar"},
},
},
},
- Context: map[string]DSConfig{
+ Context: map[string]DataSource{
"foo": {
Header: http.Header{
"Accept": {"application/json"},
@@ -219,7 +219,7 @@ func TestMergeFrom(t *testing.T) {
expected := &Config{
Input: "hello world",
OutputFiles: []string{"out.txt"},
- DataSources: map[string]DSConfig{
+ DataSources: map[string]DataSource{
"data": {
URL: mustURL("file:///data.json"),
Header: http.Header{
@@ -233,7 +233,7 @@ func TestMergeFrom(t *testing.T) {
},
},
},
- Context: map[string]DSConfig{
+ Context: map[string]DataSource{
"foo": {
URL: mustURL("https://example.com/foo.yaml"),
Header: http.Header{
@@ -335,7 +335,7 @@ func TestParseDataSourceFlags(t *testing.T) {
err = cfg.ParseDataSourceFlags([]string{"baz=foo/bar/baz.json"}, nil, nil)
assert.NoError(t, err)
expected := &Config{
- DataSources: DSources{
+ DataSources: map[string]DataSource{
"baz": {URL: mustURL("foo/bar/baz.json")},
},
}
@@ -348,7 +348,7 @@ func TestParseDataSourceFlags(t *testing.T) {
[]string{"baz=Accept: application/json"})
assert.NoError(t, err)
assert.EqualValues(t, &Config{
- DataSources: DSources{
+ DataSources: map[string]DataSource{
"baz": {
URL: mustURL("foo/bar/baz.json"),
Header: http.Header{
@@ -366,10 +366,10 @@ func TestParseDataSourceFlags(t *testing.T) {
"bar=Authorization: Basic xxxxx"})
assert.NoError(t, err)
assert.EqualValues(t, &Config{
- DataSources: DSources{
+ DataSources: map[string]DataSource{
"baz": {URL: mustURL("foo/bar/baz.json")},
},
- Context: DSources{
+ Context: map[string]DataSource{
"foo": {
URL: mustURL("http://example.com"),
Header: http.Header{