Skip to content

Commit

Permalink
supporting env var for config.toml hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
UlricQin committed Jun 9, 2022
1 parent 78d2ff6 commit 7263332
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net"
"os"
"path"
"strings"
"time"
Expand All @@ -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"`
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7263332

Please sign in to comment.