From 7263332b1ca657b9ec7236e7d3302e9d86b5c344 Mon Sep 17 00:00:00 2001 From: Ulric Qin Date: Thu, 9 Jun 2022 19:16:57 +0800 Subject: [PATCH] supporting env var for config.toml hostname --- config/config.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/config/config.go b/config/config.go index 05f50559..e7d33270 100644 --- a/config/config.go +++ b/config/config.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "net" + "os" "path" "strings" "time" @@ -12,6 +13,11 @@ import ( "github.com/toolkits/pkg/file" ) +var envVarEscaper = strings.NewReplacer( + `"`, `\"`, + `\`, `\\`, +) + type Global struct { PrintConfigs bool `toml:"print_configs"` Hostname string `toml:"hostname"` @@ -108,10 +114,16 @@ func (c *ConfigType) GetHostname() string { ret = strings.Replace(ret, "$hostname", name, -1) ret = strings.Replace(ret, "$ip", c.Global.IP, -1) + ret = os.Expand(ret, getEnv) return ret } +func getEnv(key string) string { + v := os.Getenv(key) + return envVarEscaper.Replace(v) +} + func GetInterval() time.Duration { if Config.Global.Interval <= 0 { return time.Second * 15