go-tsviewer/config/config.go

162 lines
3.2 KiB
Go
Raw Normal View History

2019-01-14 20:07:11 +00:00
package config
import (
"encoding/json"
2019-01-31 21:07:07 +00:00
"flag"
2019-01-14 20:07:11 +00:00
"io/ioutil"
"log"
"os"
2019-01-31 22:00:42 +00:00
"strconv"
2019-01-14 20:07:11 +00:00
)
type Config struct {
2019-01-17 00:35:51 +00:00
User User `json:"user"`
ServerTS ServerTS `json:"serverTS"`
ServerWeb ServerWeb `json:"serverWeb"`
2019-01-14 20:07:11 +00:00
}
type User struct {
2019-02-03 19:08:48 +00:00
Nickname string `json:"nickname"`
2019-01-14 20:07:11 +00:00
Name string `json:"name"`
Password string `json:"password"`
}
2019-01-17 00:35:51 +00:00
type ServerTS struct {
IP string `json:"ip"`
PortServer uint16 `json:"portServer"`
PortQuery uint16 `json:"portQuery"`
}
type ServerWeb struct {
2019-01-15 00:46:39 +00:00
Port uint16 `json:"port"`
}
2019-01-14 20:07:11 +00:00
const FileName = "config.json"
func New() (*Config, error) {
config := defaults()
configFile, err := os.Open(FileName)
if err != nil {
if err := config.createFile(); err != nil {
return nil, err
}
log.Println(config)
return &config, nil
}
defer configFile.Close()
jsonParser := json.NewDecoder(configFile)
jsonParser.Decode(&config)
2019-01-31 22:00:42 +00:00
config.overrideWithEnv()
2019-01-31 21:07:07 +00:00
config.overrideWithFlags()
2019-01-14 20:07:11 +00:00
return &config, nil
}
func (config Config) createFile() error {
configJSON, err := json.MarshalIndent(config, "", " ")
if err != nil {
return err
}
return ioutil.WriteFile(FileName, configJSON, 0644)
}
func defaults() Config {
return Config{
User: User{
2019-02-03 19:08:48 +00:00
Nickname: "serveradmin",
2019-01-14 20:07:11 +00:00
Name: "serveradmin",
Password: "",
},
2019-01-17 00:35:51 +00:00
ServerTS: ServerTS{
IP: "127.0.0.1",
PortServer: 9987,
PortQuery: 10011,
},
ServerWeb: ServerWeb{
Port: 80,
2019-01-15 00:46:39 +00:00
},
2019-01-14 20:07:11 +00:00
}
}
2019-01-31 21:07:07 +00:00
2019-01-31 22:00:42 +00:00
func (config *Config) overrideWithEnv() {
2019-02-03 19:08:48 +00:00
nickname := os.Getenv("TS3_NICKNAME")
2019-02-03 18:22:59 +00:00
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"))
2019-01-31 22:00:42 +00:00
2019-02-03 19:08:48 +00:00
if nickname != "" {
config.User.Nickname = nickname
}
2019-01-31 22:00:42 +00:00
if username != "" {
config.User.Name = username
}
if password != "" {
config.User.Password = password
}
if tsIP != "" {
config.ServerTS.IP = tsIP
}
2019-02-03 19:08:48 +00:00
if tsPort <= 65535 && tsPort != 0 {
2019-01-31 22:00:42 +00:00
config.ServerTS.PortServer = uint16(tsPort)
}
2019-02-03 19:08:48 +00:00
if tsQuery <= 65535 && tsQuery != 0 {
2019-01-31 22:00:42 +00:00
config.ServerTS.PortQuery = uint16(tsQuery)
}
2019-02-03 19:08:48 +00:00
if webPort <= 65535 && webPort != 0 {
2019-01-31 22:00:42 +00:00
config.ServerWeb.Port = uint16(webPort)
}
}
2019-01-31 21:07:07 +00:00
func (config *Config) overrideWithFlags() {
2019-02-03 19:08:48 +00:00
nickname := flag.String("ts3_nickname", "", "TS3Query nickname")
2019-01-31 21:07:07 +00:00
username := flag.String("ts3_name", "", "TS3Query username")
password := flag.String("ts3_pw", "", "TS3Query user password")
tsIP := flag.String("ts3_ip", "", "TS3 IP address")
tsPort := flag.Uint64("ts3_port", 65536, "TS3 port")
tsQuery := flag.Uint64("ts3_query", 65536, "TS3 query port")
webPort := flag.Uint64("web_port", 65536, "Webserver port")
flag.Parse()
2019-02-03 19:08:48 +00:00
if *nickname != "" {
config.User.Nickname = *nickname
}
2019-01-31 21:07:07 +00:00
if *username != "" {
config.User.Name = *username
}
if *password != "" {
config.User.Password = *password
}
if *tsIP != "" {
config.ServerTS.IP = *tsIP
}
2019-02-03 19:08:48 +00:00
if *tsPort <= 65535 && *tsPort != 0 {
2019-01-31 21:07:07 +00:00
config.ServerTS.PortServer = uint16(*tsPort)
}
2019-02-03 19:08:48 +00:00
if *tsQuery <= 65535 && *tsQuery != 0 {
2019-01-31 21:07:07 +00:00
config.ServerTS.PortQuery = uint16(*tsQuery)
}
2019-02-03 19:08:48 +00:00
if *webPort <= 65535 && *webPort != 0 {
2019-01-31 21:07:07 +00:00
config.ServerWeb.Port = uint16(*webPort)
}
}