Skip to content

Commit

Permalink
Merge pull request #1290 from DataDog/mdgreenfield/default-delimiters
Browse files Browse the repository at this point in the history
Allow overriding the default delimiter
  • Loading branch information
eikenb authored Apr 3, 2020
2 parents 72da05d + 1e6ee0d commit dc274bc
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 9 deletions.
16 changes: 16 additions & 0 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,16 @@ func (cli *CLI) ParseFlags(args []string) (
return nil
}), "dedup", "")

flags.Var((funcVar)(func(s string) error {
c.DefaultDelims.Left = config.String(s)
return nil
}), "default-left-delimiter", "")

flags.Var((funcVar)(func(s string) error {
c.DefaultDelims.Right = config.String(s)
return nil
}), "default-right-delimiter", "")

flags.BoolVar(&dry, "dry", false, "")

flags.Var((funcVar)(func(s string) error {
Expand Down Expand Up @@ -668,6 +678,12 @@ Options:
Enable de-duplication mode - reduces load on Consul when many instances of
Consul Template are rendering a common template
-default-left-delimiter
The default left delimiter for templating
-default-right-delimiter
The default right delimiter for templating
-dry
Print generated templates to stdout instead of rendering
Expand Down
33 changes: 26 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ type Config struct {
// Dedup is used to configure the dedup settings
Dedup *DedupConfig `mapstructure:"deduplicate"`

// DefaultDelims is used to configure the default delimiters for templates
DefaultDelims *DefaultDelims `mapstructure:"default_delimiters"`

// Exec is the configuration for exec/supervise mode.
Exec *ExecConfig `mapstructure:"exec"`

Expand Down Expand Up @@ -104,6 +107,10 @@ func (c *Config) Copy() *Config {
o.Dedup = c.Dedup.Copy()
}

if c.DefaultDelims != nil {
o.DefaultDelims = c.DefaultDelims.Copy()
}

if c.Exec != nil {
o.Exec = c.Exec.Copy()
}
Expand Down Expand Up @@ -163,6 +170,10 @@ func (c *Config) Merge(o *Config) *Config {
r.Dedup = r.Dedup.Merge(o.Dedup)
}

if o.DefaultDelims != nil {
r.DefaultDelims = r.DefaultDelims.Merge(o.DefaultDelims)
}

if o.Exec != nil {
r.Exec = r.Exec.Merge(o.Exec)
}
Expand Down Expand Up @@ -229,6 +240,7 @@ func Parse(s string) (*Config, error) {
"consul.ssl",
"consul.transport",
"deduplicate",
"default_delimiters",
"env",
"exec",
"exec.env",
Expand Down Expand Up @@ -383,6 +395,7 @@ func (c *Config) GoString() string {
return fmt.Sprintf("&Config{"+
"Consul:%#v, "+
"Dedup:%#v, "+
"DefaultDelims:%#v, "+
"Exec:%#v, "+
"KillSignal:%s, "+
"LogLevel:%s, "+
Expand All @@ -397,6 +410,7 @@ func (c *Config) GoString() string {
"}",
c.Consul,
c.Dedup,
c.DefaultDelims,
c.Exec,
SignalGoString(c.KillSignal),
StringGoString(c.LogLevel),
Expand Down Expand Up @@ -436,13 +450,14 @@ func (expected *Config) Diff(actual *Config) string {
// variables may be set which control the values for the default configuration.
func DefaultConfig() *Config {
return &Config{
Consul: DefaultConsulConfig(),
Dedup: DefaultDedupConfig(),
Exec: DefaultExecConfig(),
Syslog: DefaultSyslogConfig(),
Templates: DefaultTemplateConfigs(),
Vault: DefaultVaultConfig(),
Wait: DefaultWaitConfig(),
Consul: DefaultConsulConfig(),
Dedup: DefaultDedupConfig(),
DefaultDelims: DefaultDefaultDelims(),
Exec: DefaultExecConfig(),
Syslog: DefaultSyslogConfig(),
Templates: DefaultTemplateConfigs(),
Vault: DefaultVaultConfig(),
Wait: DefaultWaitConfig(),
}
}

Expand All @@ -465,6 +480,10 @@ func (c *Config) Finalize() {
}
c.Dedup.Finalize()

if c.DefaultDelims == nil {
c.DefaultDelims = DefaultDefaultDelims()
}

if c.Exec == nil {
c.Exec = DefaultExecConfig()
}
Expand Down
24 changes: 24 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,30 @@ func TestParse(t *testing.T) {
},
false,
},
{
"default_left_delimiter",
`default_delimiters {
left = "<"
}`,
&Config{
DefaultDelims: &DefaultDelims{
Left: String("<"),
},
},
false,
},
{
"default_right_delimiter",
`default_delimiters {
right = ">"
}`,
&Config{
DefaultDelims: &DefaultDelims{
Right: String(">"),
},
},
false,
},
{
"exec",
`exec {}`,
Expand Down
53 changes: 53 additions & 0 deletions config/default_delimiters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package config

// DefaultDelims is used to configure the default delimiters used for all templates
type DefaultDelims struct {
// Left is the left delimiter for templating
Left *string `mapstructure:"left"`

// Right is the right delimiter for templating
Right *string `mapstructure:"right"`
}

// DefaultDefaultDelims returns the default DefaultDelims
func DefaultDefaultDelims() *DefaultDelims {
return &DefaultDelims{}
}

// Copy returns a copy of the DefaultDelims
func (c *DefaultDelims) Copy() *DefaultDelims {
if c == nil {
return nil
}

return &DefaultDelims{
Left: c.Left,
Right: c.Right,
}
}

// Merge merges the DefaultDelims
func (c *DefaultDelims) Merge(o *DefaultDelims) *DefaultDelims {
if c == nil {
if o == nil {
return nil
}
return o.Copy()
}

if o == nil {
return c.Copy()
}

r := c.Copy()

if o.Left != nil {
r.Left = o.Left
}

if o.Right != nil {
r.Right = o.Right
}

return r
}
118 changes: 118 additions & 0 deletions config/default_delimiters_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package config

import (
"fmt"
"reflect"
"testing"
)

func TestDefaultDelims_Copy(t *testing.T) {
cases := []struct {
name string
a *DefaultDelims
}{
{
"nil",
nil,
},
{
"empty",
&DefaultDelims{},
},
{
"copy",
&DefaultDelims{
Left: String("<<"),
Right: String(">>"),
},
},
}

for i, tc := range cases {
t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) {
r := tc.a.Copy()
if !reflect.DeepEqual(tc.a, r) {
t.Errorf("\nexp: %#v\nact: %#v", tc.a, r)
}
})
}
}

func TestDefaultDelims_Merge(t *testing.T) {
cases := []struct {
name string
a *DefaultDelims
b *DefaultDelims
r *DefaultDelims
}{
{
"nil_a",
nil,
&DefaultDelims{},
&DefaultDelims{},
},
{
"nil_b",
&DefaultDelims{},
nil,
&DefaultDelims{},
},
{
"nil_both",
nil,
nil,
nil,
},
{
"empty",
&DefaultDelims{},
&DefaultDelims{},
&DefaultDelims{},
},
{
"left_delim_l",
&DefaultDelims{Left: String("<<")},
&DefaultDelims{},
&DefaultDelims{Left: String("<<")},
},
{
"left_delim_r",
&DefaultDelims{},
&DefaultDelims{Left: String("<<")},
&DefaultDelims{Left: String("<<")},
},
{
"left_delim_r2",
&DefaultDelims{Left: String(">>")},
&DefaultDelims{Left: String("<<")},
&DefaultDelims{Left: String("<<")},
},
{
"right_delim_l",
&DefaultDelims{Right: String(">>")},
&DefaultDelims{},
&DefaultDelims{Right: String(">>")},
},
{
"right_delim_r",
&DefaultDelims{},
&DefaultDelims{Right: String(">>")},
&DefaultDelims{Right: String(">>")},
},
{
"right_delim_r2",
&DefaultDelims{Right: String("<<")},
&DefaultDelims{Right: String(">>")},
&DefaultDelims{Right: String(">>")},
},
}

for i, tc := range cases {
t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) {
r := tc.a.Merge(tc.b)
if !reflect.DeepEqual(tc.r, r) {
t.Errorf("\nexp: %#v\nact: %#v", tc.r, r)
}
})
}
}
13 changes: 11 additions & 2 deletions manager/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -873,12 +873,21 @@ func (r *Runner) init() error {
// config templates is kept so templates can lookup their commands and output
// destinations.
for _, ctmpl := range *r.config.Templates {
leftDelim := config.StringVal(ctmpl.LeftDelim)
if leftDelim == "" {
leftDelim = config.StringVal(r.config.DefaultDelims.Left)
}
rightDelim := config.StringVal(ctmpl.RightDelim)
if rightDelim == "" {
rightDelim = config.StringVal(r.config.DefaultDelims.Right)
}

tmpl, err := template.NewTemplate(&template.NewTemplateInput{
Source: config.StringVal(ctmpl.Source),
Contents: config.StringVal(ctmpl.Contents),
ErrMissingKey: config.BoolVal(ctmpl.ErrMissingKey),
LeftDelim: config.StringVal(ctmpl.LeftDelim),
RightDelim: config.StringVal(ctmpl.RightDelim),
LeftDelim: leftDelim,
RightDelim: rightDelim,
FunctionBlacklist: ctmpl.FunctionBlacklist,
SandboxPath: config.StringVal(ctmpl.SandboxPath),
})
Expand Down

0 comments on commit dc274bc

Please sign in to comment.