Commit 72c0527b authored by Michael Banzon's avatar Michael Banzon

Added support for env vars in Caddyfile

This is work in progress for #304
parent 7f7a6aba
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"net" "net"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"strings" "strings"
) )
...@@ -71,6 +72,8 @@ func (p *parser) addresses() error { ...@@ -71,6 +72,8 @@ func (p *parser) addresses() error {
for { for {
tkn := p.Val() tkn := p.Val()
tkn = getValFromEnv(tkn)
// special case: import directive replaces tokens during parse-time // special case: import directive replaces tokens during parse-time
if tkn == "import" && p.isNewLine() { if tkn == "import" && p.isNewLine() {
err := p.doImport() err := p.doImport()
...@@ -241,6 +244,7 @@ func (p *parser) directive() error { ...@@ -241,6 +244,7 @@ func (p *parser) directive() error {
} else if p.Val() == "}" && nesting == 0 { } else if p.Val() == "}" && nesting == 0 {
return p.Err("Unexpected '}' because no matching opening brace") 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]) p.block.Tokens[dir] = append(p.block.Tokens[dir], p.tokens[p.cursor])
} }
...@@ -327,3 +331,14 @@ func (sb serverBlock) HostList() []string { ...@@ -327,3 +331,14 @@ func (sb serverBlock) HostList() []string {
} }
return sbHosts 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
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment