0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00

Environment variables Windows style

Added the Windows style ({%%}) for environment variables in Caddyfiles
(for issue #304).
This commit is contained in:
Michael Banzon 2015-11-05 18:01:44 +01:00
parent 72c0527b7d
commit 01465932e7

View file

@ -8,6 +8,11 @@ import (
"strings"
)
var (
unixEnvRegEx = regexp.MustCompile("{\\$[^}]+}")
windowsEnvRegEx = regexp.MustCompile("{%[^}]+%}")
)
type parser struct {
Dispenser
block serverBlock // current server block being parsed
@ -333,12 +338,18 @@ func (sb serverBlock) HostList() []string {
}
func getValFromEnv(s string) string {
re := regexp.MustCompile("{\\$[^}]+}")
envRefs := re.FindAllString(s, -1)
envRefsUnix := unixEnvRegEx.FindAllString(s, -1)
for _, ref := range envRefs {
for _, ref := range envRefsUnix {
s = strings.Replace(s, ref, os.Getenv(ref[2:len(ref)-1]), -1)
}
envRefsWin := unixEnvRegEx.FindAllString(s, -1)
for _, ref := range envRefsWin {
s = strings.Replace(s, ref, os.Getenv(ref[2:len(ref)-2]), -1)
}
return s
}