added env vars

This commit is contained in:
Hendrik Schlehlein 2019-01-31 23:00:42 +01:00
parent 8bd0e85e17
commit eddc32a157

View file

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