add support for env variables to the DSL
This commit is contained in:
@ -7,6 +7,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
@ -115,28 +116,58 @@ func (ti *TemplateInterpreter) Interpret(masonryInput string, tmplText string) (
|
||||
"getHost": func(settings []lang.ServerSetting) string {
|
||||
for _, s := range settings {
|
||||
if s.Host != nil {
|
||||
return *s.Host
|
||||
if s.Host.Literal != nil {
|
||||
return "\"" + *s.Host.Literal + "\""
|
||||
}
|
||||
return fmt.Sprintf(`func() string { if v := os.Getenv("%s"); v != "" { return v }; return "%s" }()`, s.Host.EnvVar.Name, func() string {
|
||||
if s.Host.EnvVar.Default != nil {
|
||||
return *s.Host.EnvVar.Default
|
||||
}
|
||||
return "localhost"
|
||||
}())
|
||||
}
|
||||
}
|
||||
return "localhost"
|
||||
},
|
||||
"getPort": func(settings []lang.ServerSetting) int {
|
||||
"getPort": func(settings []lang.ServerSetting) string {
|
||||
for _, s := range settings {
|
||||
if s.Port != nil {
|
||||
return *s.Port
|
||||
if s.Port.Literal != nil {
|
||||
return strconv.Itoa(*s.Port.Literal)
|
||||
}
|
||||
return fmt.Sprintf(`func() string { if v := os.Getenv("%s"); v != "" { return v }; return "%s" }()`, s.Port.EnvVar.Name, func() string {
|
||||
if s.Port.EnvVar.Default != nil {
|
||||
return *s.Port.EnvVar.Default
|
||||
}
|
||||
return "8080"
|
||||
}())
|
||||
}
|
||||
}
|
||||
return 8080
|
||||
return "8080"
|
||||
},
|
||||
"getServerHostPort": func(settings []lang.ServerSetting) string {
|
||||
host := "localhost"
|
||||
port := 8080
|
||||
for _, s := range settings {
|
||||
if s.Host != nil {
|
||||
host = *s.Host
|
||||
if s.Host.Literal != nil {
|
||||
host = *s.Host.Literal
|
||||
}
|
||||
if s.Host.EnvVar != nil && s.Host.EnvVar.Default != nil {
|
||||
host = *s.Host.EnvVar.Default
|
||||
}
|
||||
// If it's an env var, keep the default
|
||||
}
|
||||
if s.Port != nil {
|
||||
port = *s.Port
|
||||
if s.Port.Literal != nil {
|
||||
port = *s.Port.Literal
|
||||
}
|
||||
if s.Port.EnvVar != nil && s.Port.EnvVar.Default != nil {
|
||||
if p, err := strconv.Atoi(*s.Port.EnvVar.Default); err == nil {
|
||||
port = p
|
||||
}
|
||||
}
|
||||
// If it's an env var, keep the default
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("%s:%d", host, port)
|
||||
@ -234,28 +265,58 @@ func NewTemplateRegistry() *TemplateRegistry {
|
||||
"getHost": func(settings []lang.ServerSetting) string {
|
||||
for _, s := range settings {
|
||||
if s.Host != nil {
|
||||
return *s.Host
|
||||
if s.Host.Literal != nil {
|
||||
return "\"" + *s.Host.Literal + "\""
|
||||
}
|
||||
return fmt.Sprintf(`func() string { if v := os.Getenv("%s"); v != "" { return v }; return "%s" }()`, s.Host.EnvVar.Name, func() string {
|
||||
if s.Host.EnvVar.Default != nil {
|
||||
return *s.Host.EnvVar.Default
|
||||
}
|
||||
return "localhost"
|
||||
}())
|
||||
}
|
||||
}
|
||||
return "localhost"
|
||||
},
|
||||
"getPort": func(settings []lang.ServerSetting) int {
|
||||
"getPort": func(settings []lang.ServerSetting) string {
|
||||
for _, s := range settings {
|
||||
if s.Port != nil {
|
||||
return *s.Port
|
||||
if s.Port.Literal != nil {
|
||||
return strconv.Itoa(*s.Port.Literal)
|
||||
}
|
||||
return fmt.Sprintf(`func() string { if v := os.Getenv("%s"); v != "" { return v }; return "%s" }()`, s.Port.EnvVar.Name, func() string {
|
||||
if s.Port.EnvVar.Default != nil {
|
||||
return *s.Port.EnvVar.Default
|
||||
}
|
||||
return "8080"
|
||||
}())
|
||||
}
|
||||
}
|
||||
return 8080
|
||||
return "8080"
|
||||
},
|
||||
"getServerHostPort": func(settings []lang.ServerSetting) string {
|
||||
host := "localhost"
|
||||
port := 8080
|
||||
for _, s := range settings {
|
||||
if s.Host != nil {
|
||||
host = *s.Host
|
||||
if s.Host.Literal != nil {
|
||||
host = *s.Host.Literal
|
||||
}
|
||||
if s.Host.EnvVar != nil && s.Host.EnvVar.Default != nil {
|
||||
host = *s.Host.EnvVar.Default
|
||||
}
|
||||
// If it's an env var, keep the default
|
||||
}
|
||||
if s.Port != nil {
|
||||
port = *s.Port
|
||||
if s.Port.Literal != nil {
|
||||
port = *s.Port.Literal
|
||||
}
|
||||
if s.Port.EnvVar != nil && s.Port.EnvVar.Default != nil {
|
||||
if p, err := strconv.Atoi(*s.Port.EnvVar.Default); err == nil {
|
||||
port = p
|
||||
}
|
||||
}
|
||||
// If it's an env var, keep the default
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("%s:%d", host, port)
|
||||
|
Reference in New Issue
Block a user