supporting env var for config.toml hostname

This commit is contained in:
Ulric Qin 2022-06-09 19:16:57 +08:00
parent 78d2ff6ec1
commit 7263332b1c
1 changed files with 12 additions and 0 deletions

View File

@ -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