This commit is contained in:
Moritz 2019-01-31 23:28:59 +01:00
commit 0d575286fa

View file

@ -6,6 +6,7 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"strconv"
) )
type Config struct { type Config struct {
@ -48,6 +49,7 @@ func New() (*Config, error) {
jsonParser := json.NewDecoder(configFile) jsonParser := json.NewDecoder(configFile)
jsonParser.Decode(&config) jsonParser.Decode(&config)
config.overrideWithEnv()
config.overrideWithFlags() config.overrideWithFlags()
return &config, nil 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() { func (config *Config) overrideWithFlags() {
username := flag.String("ts3_name", "", "TS3Query username") username := flag.String("ts3_name", "", "TS3Query username")
password := flag.String("ts3_pw", "", "TS3Query user password") password := flag.String("ts3_pw", "", "TS3Query user password")