diff --git a/Makefile b/Makefile index 3a8fc15..db5d08b 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/envplate.go b/envplate.go index 735ba59..b2da37d 100644 --- a/envplate.go +++ b/envplate.go @@ -7,11 +7,11 @@ import ( "os" "path/filepath" "regexp" + "strings" ) const ( NoDefaultDefined = "" - NoKeyDefined = "" NotAnEscapeSequence = "" ) @@ -83,6 +83,7 @@ func createBackup(file string) error { func parse(file string) error { + env := envmap() content, err := ioutil.ReadFile(file) if err != nil { @@ -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 { @@ -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) @@ -132,9 +133,7 @@ func parse(file string) error { } if len(esc) > 0 { - value = esc[:len(esc)/2] + value - } return value @@ -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(`(\\+)(.*)`) @@ -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() + +} diff --git a/envplate_test.go b/envplate_test.go index eaf2162..22e23fe 100644 --- a/envplate_test.go +++ b/envplate_test.go @@ -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} @@ -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