Added support for env vars in Caddyfile

This is work in progress for #304
This commit is contained in:
Michael Banzon 2015-11-05 08:26:10 +01:00
parent 7f7a6abafd
commit 72c0527b7d

View file

@ -4,6 +4,7 @@ import (
"net"
"os"
"path/filepath"
"regexp"
"strings"
)
@ -71,6 +72,8 @@ func (p *parser) addresses() error {
for {
tkn := p.Val()
tkn = getValFromEnv(tkn)
// special case: import directive replaces tokens during parse-time
if tkn == "import" && p.isNewLine() {
err := p.doImport()
@ -241,6 +244,7 @@ func (p *parser) directive() error {
} else if p.Val() == "}" && nesting == 0 {
return p.Err("Unexpected '}' because no matching opening brace")
}
p.tokens[p.cursor].text = getValFromEnv(p.tokens[p.cursor].text)
p.block.Tokens[dir] = append(p.block.Tokens[dir], p.tokens[p.cursor])
}
@ -327,3 +331,14 @@ func (sb serverBlock) HostList() []string {
}
return sbHosts
}
func getValFromEnv(s string) string {
re := regexp.MustCompile("{\\$[^}]+}")
envRefs := re.FindAllString(s, -1)
for _, ref := range envRefs {
s = strings.Replace(s, ref, os.Getenv(ref[2:len(ref)-1]), -1)
}
return s
}