From eddc32a1576e7828a31959afd595eaa4e7be848b Mon Sep 17 00:00:00 2001 From: Hendrik Schlehlein Date: Thu, 31 Jan 2019 23:00:42 +0100 Subject: [PATCH] added env vars --- config/config.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/config/config.go b/config/config.go index f6bec51..2cd7c25 100644 --- a/config/config.go +++ b/config/config.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "log" "os" + "strconv" ) type Config struct { @@ -48,6 +49,7 @@ func New() (*Config, error) { jsonParser := json.NewDecoder(configFile) jsonParser.Decode(&config) + config.overrideWithEnv() config.overrideWithFlags() return &config, nil @@ -79,6 +81,39 @@ func defaults() Config { } } +func (config *Config) overrideWithEnv() { + username := os.Getenv("ts3_name") + password := os.Getenv("ts3_pw") + tsIP := os.Getenv("ts3_ip") + tsPort, _ := strconv.Atoi(os.Getenv("ts3_port")) + tsQuery, _ := strconv.Atoi(os.Getenv("ts3_query")) + webPort, _ := strconv.Atoi(os.Getenv("web_port")) + + if username != "" { + config.User.Name = username + } + + if password != "" { + config.User.Password = password + } + + if tsIP != "" { + config.ServerTS.IP = tsIP + } + + if tsPort <= 65535 { + config.ServerTS.PortServer = uint16(tsPort) + } + + if tsQuery <= 65535 { + config.ServerTS.PortQuery = uint16(tsQuery) + } + + if webPort <= 65535 { + config.ServerWeb.Port = uint16(webPort) + } +} + func (config *Config) overrideWithFlags() { username := flag.String("ts3_name", "", "TS3Query username") password := flag.String("ts3_pw", "", "TS3Query user password")