Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow empty default values - solves #19 #20

Merged
merged 1 commit into from
Jan 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions envplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (
const (
NoDefaultDefined = ""
NotAnEscapeSequence = ""
DefaultValueSyntax = ":-"
)

var exp = regexp.MustCompile(`(\\*)\$\{(.+?)(?:\:\-(.+?))?\}`)
var exp = regexp.MustCompile(`(\\*)\$\{(.+?)(?:(\:\-)(.*?))?\}`)

func Apply(globs []string) {

Expand Down Expand Up @@ -95,7 +96,7 @@ func parse(file string) error {
parsed := exp.ReplaceAllStringFunc(string(content), func(match string) string {

var (
esc, key, def = capture(match)
esc, key, sep, def = capture(match)
value, keyDefined = env[key]
)

Expand All @@ -115,7 +116,7 @@ func parse(file string) error {

if !keyDefined {

if def == NoDefaultDefined {
if sep == NoDefaultDefined {
Log(ERROR, "'%s' requires undeclared environment variable '%s', no default is given", file, key)
} else {

Expand Down Expand Up @@ -162,15 +163,16 @@ func parse(file string) error {

}

func capture(s string) (esc, key, def string) {
func capture(s string) (esc, key, sep, def string) {

matches := exp.FindStringSubmatch(s)

esc = matches[1]
key = matches[2]
def = matches[3]
sep = matches[3]
def = matches[4]

return esc, key, def
return esc, key, sep, def

}

Expand Down
33 changes: 18 additions & 15 deletions envplate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ func _template_defaults(t *testing.T) (string, string) {

tpl := `Double1=${DATABASE} Double2=${DATABASE}
Double3=${DATABASE:-db2.example.com} Double4=${DATABASE:-db2.example.com}
DoubleDefault1=${ANOTHER_DATABASE:-db2-example.com} DoubleDefault2=${ANOTHER_DATABASE:-db2-example.com}`
DoubleDefault1=${ANOTHER_DATABASE:-db2-example.com} DoubleDefault2=${ANOTHER_DATABASE:-db2-example.com}
EmptyDefault=${UNDEFINED_VARIABLE:-}`

return _write(t, "parse.txt", tpl, 0644), tpl

Expand Down Expand Up @@ -206,7 +207,8 @@ func TestFullParseDefaults(t *testing.T) {
assert.True(_exists(backup))
assert.Equal(`Double1=db.example.com Double2=db.example.com
Double3=db.example.com Double4=db.example.com
DoubleDefault1=db2-example.com DoubleDefault2=db2-example.com`, _read(t, file))
DoubleDefault1=db2-example.com DoubleDefault2=db2-example.com
EmptyDefault=`, _read(t, file))

}

Expand Down Expand Up @@ -269,27 +271,28 @@ func TestCapture(t *testing.T) {
assert := assert.New(t)

var tt = []struct {
in, e, v, d string
in, e, v, s, d string
}{
{"${FOO}", "", "FOO", NoDefaultDefined},
{"${FOO:-bar}", "", "FOO", "bar"},
{"${FOO:-at the bar}", "", "FOO", "at the bar"},
{"${FOO_3000:-near the bar}", "", "FOO_3000", "near the bar"},
{"${FOO:--1}", "", "FOO", "-1"},
{"${FOO:-http://www.example.com/bar/gar/war?a=b}", "", "FOO", "http://www.example.com/bar/gar/war?a=b"},
{`\${FOO}`, `\`, "FOO", NoDefaultDefined},
{`\\${FOO:-bar}`, `\\`, "FOO", "bar"},
{`\\\${FOO:-bar}`, `\\\`, "FOO", "bar"},
{`\\\\${FOO:-bar}`, `\\\\`, "FOO", "bar"},
{"foo${FOO}", "", "FOO", NoDefaultDefined},
{"${FOO}", "", "FOO", NoDefaultDefined, NoDefaultDefined},
{"${FOO:-bar}", "", "FOO", ":-", "bar"},
{"${FOO:-at the bar}", "", "FOO", ":-", "at the bar"},
{"${FOO_3000:-near the bar}", "", "FOO_3000", ":-", "near the bar"},
{"${FOO:--1}", "", "FOO", ":-", "-1"},
{"${FOO:-http://www.example.com/bar/gar/war?a=b}", "", "FOO", ":-", "http://www.example.com/bar/gar/war?a=b"},
{`\${FOO}`, `\`, "FOO", NoDefaultDefined, NoDefaultDefined},
{`\\${FOO:-bar}`, `\\`, "FOO", ":-", "bar"},
{`\\\${FOO:-bar}`, `\\\`, "FOO", ":-", "bar"},
{`\\\\${FOO:-bar}`, `\\\\`, "FOO", ":-", "bar"},
{"foo${FOO}", "", "FOO", NoDefaultDefined, NoDefaultDefined},
}

for _, tt := range tt {

e, v, d := capture(tt.in)
e, v, s, d := capture(tt.in)

assert.Equal(tt.e, e)
assert.Equal(tt.v, v)
assert.Equal(tt.s, s)
assert.Equal(tt.d, d)

}
Expand Down