Skip to content

Commit

Permalink
Allow empty values if environment keys are defined. Should fix #15.
Browse files Browse the repository at this point in the history
  • Loading branch information
yawn committed Jul 12, 2015
1 parent 0ca2468 commit 33899b1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ release:
github-release upload --user $(USER) --repo $(REPO) --tag $(VERSION) -s $(TOKEN) --name ep-linux --file out/linux/ep

test:
DATABASE=db.example.com MODE=debug go test -cover
DATABASE=db.example.com NULL= MODE=debug go test -cover
54 changes: 36 additions & 18 deletions envplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"os"
"path/filepath"
"regexp"
"strings"
)

const (
NoDefaultDefined = ""
NoKeyDefined = ""
NotAnEscapeSequence = ""
)

Expand Down Expand Up @@ -83,6 +83,7 @@ func createBackup(file string) error {

func parse(file string) error {

env := envmap()
content, err := ioutil.ReadFile(file)

if err != nil {
Expand All @@ -94,8 +95,8 @@ func parse(file string) error {
parsed := exp.ReplaceAllStringFunc(string(content), func(match string) string {

var (
esc, key, def = capture(match)
value = os.Getenv(key)
esc, key, def = capture(match)
value, keyDefined = env[key]
)

if len(esc)%2 == 1 {
Expand All @@ -112,7 +113,7 @@ func parse(file string) error {

}

if value == NoKeyDefined {
if !keyDefined {

if def == NoDefaultDefined {
Log(ERROR, "'%s' requires undeclared environment variable '%s', no default is given", file, key)
Expand All @@ -132,9 +133,7 @@ func parse(file string) error {
}

if len(esc) > 0 {

value = esc[:len(esc)/2] + value

}

return value
Expand Down Expand Up @@ -175,18 +174,6 @@ func capture(s string) (esc, key, def string) {

}

func filemode(file string) os.FileMode {

fileinfo, err := os.Stat(file)

if err != nil {
Log(ERROR, "Cannot stat '%s': %v", file, err)
}

return fileinfo.Mode()

}

func escape(s string) (escaped string) {

expEscaped := regexp.MustCompile(`(\\+)(.*)`)
Expand Down Expand Up @@ -215,3 +202,34 @@ func escape(s string) (escaped string) {
return escaped

}

func envmap() (m map[string]string) {

m = make(map[string]string)

for _, e := range os.Environ() {

s := strings.Split(e, "=")

key := s[0]
val := strings.Join(s[1:], "=")

m[key] = val

}

return

}

func filemode(file string) os.FileMode {

fileinfo, err := os.Stat(file)

if err != nil {
Log(ERROR, "Cannot stat '%s': %v", file, err)
}

return fileinfo.Mode()

}
2 changes: 2 additions & 0 deletions envplate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func _template(t *testing.T) (string, string) {

tpl := `Database1=${DATABASE}
Mode=${MODE}
Null=${NULL}
Database2=${DATABASE}
Database3=$NOT_A_VARIABLE
Database4=${ANOTHER_DATABASE:-db2.example.com}
Expand Down Expand Up @@ -175,6 +176,7 @@ func TestFullParse(t *testing.T) {
assert.True(_exists(backup))
assert.Equal(`Database1=db.example.com
Mode=debug
Null=
Database2=db.example.com
Database3=$NOT_A_VARIABLE
Database4=db2.example.com
Expand Down

0 comments on commit 33899b1

Please sign in to comment.