Skip to content

Commit

Permalink
Merge pull request #853 from hairyhenderson/deprecate-legacy-config
Browse files Browse the repository at this point in the history
Deprecate legacy config struct
  • Loading branch information
hairyhenderson authored May 22, 2020
2 parents cea6c6e + d954aa2 commit 8a45304
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 65 deletions.
2 changes: 1 addition & 1 deletion cmd/gomplate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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{})
Expand Down
6 changes: 3 additions & 3 deletions data/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
},
Expand All @@ -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{
Expand Down
8 changes: 5 additions & 3 deletions gomplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
80 changes: 37 additions & 43 deletions internal/config/configfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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"`
Expand All @@ -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.
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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,
}
Expand All @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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
}
24 changes: 12 additions & 12 deletions internal/config/configfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
},
Expand All @@ -56,7 +56,7 @@ pluginTimeout: 2s
},
},
},
Context: map[string]DSConfig{
Context: map[string]DataSource{
".": {
URL: mustURL("file:///data.json"),
},
Expand Down Expand Up @@ -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"),
},
Expand All @@ -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{
Expand All @@ -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"},
Expand All @@ -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{
Expand All @@ -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{
Expand Down Expand Up @@ -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")},
},
}
Expand All @@ -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{
Expand All @@ -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{
Expand Down

0 comments on commit 8a45304

Please sign in to comment.