forked from Cliffbreak/tsviewer
changed config to yaml; removed flag support
This commit is contained in:
parent
a454dbc947
commit
59bd3d7139
1 changed files with 44 additions and 121 deletions
165
config/config.go
165
config/config.go
|
@ -1,161 +1,84 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"flag"
|
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
User User `json:"user"`
|
User struct {
|
||||||
ServerTS ServerTS `json:"serverTS"`
|
Nickname string
|
||||||
ServerWeb ServerWeb `json:"serverWeb"`
|
Name string
|
||||||
}
|
Password string
|
||||||
|
}
|
||||||
type User struct {
|
ServerTS struct {
|
||||||
Nickname string `json:"nickname"`
|
IP string
|
||||||
Name string `json:"name"`
|
PortServer uint16
|
||||||
Password string `json:"password"`
|
PortQuery uint16
|
||||||
}
|
}
|
||||||
|
ServerWeb struct {
|
||||||
type ServerTS struct {
|
Port uint16
|
||||||
IP string `json:"ip"`
|
}
|
||||||
PortServer uint16 `json:"portServer"`
|
|
||||||
PortQuery uint16 `json:"portQuery"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type ServerWeb struct {
|
|
||||||
Port uint16 `json:"port"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const FileName = "config.json"
|
const FileName = "config.json"
|
||||||
|
|
||||||
func New() (*Config, error) {
|
func New() (*Config, error) {
|
||||||
config := defaults()
|
viper.SetConfigName("config")
|
||||||
|
viper.SetConfigFile("yaml")
|
||||||
configFile, err := os.Open(FileName)
|
viper.AddConfigPath(".")
|
||||||
if err != nil {
|
if err := viper.ReadInConfig(); err != nil {
|
||||||
if err := config.createFile(); err != nil {
|
return nil, err
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Println(config)
|
|
||||||
return &config, nil
|
|
||||||
}
|
|
||||||
defer configFile.Close()
|
|
||||||
|
|
||||||
jsonParser := json.NewDecoder(configFile)
|
|
||||||
jsonParser.Decode(&config)
|
|
||||||
|
|
||||||
config.overrideWithEnv()
|
|
||||||
config.overrideWithFlags()
|
|
||||||
|
|
||||||
return &config, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (config Config) createFile() error {
|
|
||||||
configJSON, err := json.MarshalIndent(config, "", " ")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ioutil.WriteFile(FileName, configJSON, 0644)
|
setDefaults()
|
||||||
|
|
||||||
|
var config Config
|
||||||
|
err := viper.Unmarshal(&config)
|
||||||
|
|
||||||
|
config.overrideWithEnvironmentVars()
|
||||||
|
|
||||||
|
return &config, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func defaults() Config {
|
func setDefaults() {
|
||||||
return Config{
|
viper.SetDefault("User.Nickname", "serveradmin")
|
||||||
User: User{
|
viper.SetDefault("User.Name", "serveradmin")
|
||||||
Nickname: "serveradmin",
|
viper.SetDefault("User.Password", "<changeMe>")
|
||||||
Name: "serveradmin",
|
viper.SetDefault("ServerTS.IP", "127.0.0.1")
|
||||||
Password: "",
|
viper.SetDefault("ServerTS.PortServer", "9987")
|
||||||
},
|
viper.SetDefault("ServerTS.PortQuery", "10011")
|
||||||
ServerTS: ServerTS{
|
viper.SetDefault("ServerWeb.Port", "80")
|
||||||
IP: "127.0.0.1",
|
|
||||||
PortServer: 9987,
|
|
||||||
PortQuery: 10011,
|
|
||||||
},
|
|
||||||
ServerWeb: ServerWeb{
|
|
||||||
Port: 80,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (config *Config) overrideWithEnv() {
|
func (config *Config) overrideWithEnvironmentVars() {
|
||||||
nickname := os.Getenv("TS3_NICKNAME")
|
if nickname := os.Getenv("TS3_NICKNAME"); nickname != "" {
|
||||||
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 nickname != "" {
|
|
||||||
config.User.Nickname = nickname
|
config.User.Nickname = nickname
|
||||||
}
|
}
|
||||||
|
|
||||||
if username != "" {
|
if username := os.Getenv("TS3_NAME"); username != "" {
|
||||||
config.User.Name = username
|
config.User.Name = username
|
||||||
}
|
}
|
||||||
|
|
||||||
if password != "" {
|
if password := os.Getenv("TS3_PW"); password != "" {
|
||||||
config.User.Password = password
|
config.User.Password = password
|
||||||
}
|
}
|
||||||
|
|
||||||
if tsIP != "" {
|
if tsIP := os.Getenv("TS3_IP"); tsIP != "" {
|
||||||
config.ServerTS.IP = tsIP
|
config.ServerTS.IP = tsIP
|
||||||
}
|
}
|
||||||
|
|
||||||
if tsPort <= 65535 && tsPort != 0 {
|
if tsPort, err := strconv.Atoi(os.Getenv("TS3_PORT")); tsPort <= 65535 && tsPort >= 0 && err == nil {
|
||||||
config.ServerTS.PortServer = uint16(tsPort)
|
config.ServerTS.PortServer = uint16(tsPort)
|
||||||
}
|
}
|
||||||
|
|
||||||
if tsQuery <= 65535 && tsQuery != 0 {
|
if tsQuery, err := strconv.Atoi(os.Getenv("TS3_QUERY")); tsQuery <= 65535 && tsQuery >= 0 && err == nil {
|
||||||
config.ServerTS.PortQuery = uint16(tsQuery)
|
config.ServerTS.PortQuery = uint16(tsQuery)
|
||||||
}
|
}
|
||||||
|
|
||||||
if webPort <= 65535 && webPort != 0 {
|
if webPort, err := strconv.Atoi(os.Getenv("WEB_PORT")); webPort <= 65535 && webPort >= 0 && err == nil {
|
||||||
config.ServerWeb.Port = uint16(webPort)
|
config.ServerWeb.Port = uint16(webPort)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (config *Config) overrideWithFlags() {
|
|
||||||
nickname := flag.String("ts3_nickname", "", "TS3Query nickname")
|
|
||||||
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()
|
|
||||||
|
|
||||||
if *nickname != "" {
|
|
||||||
config.User.Nickname = *nickname
|
|
||||||
}
|
|
||||||
|
|
||||||
if *username != "" {
|
|
||||||
config.User.Name = *username
|
|
||||||
}
|
|
||||||
|
|
||||||
if *password != "" {
|
|
||||||
config.User.Password = *password
|
|
||||||
}
|
|
||||||
|
|
||||||
if *tsIP != "" {
|
|
||||||
config.ServerTS.IP = *tsIP
|
|
||||||
}
|
|
||||||
|
|
||||||
if *tsPort <= 65535 && *tsPort != 0 {
|
|
||||||
config.ServerTS.PortServer = uint16(*tsPort)
|
|
||||||
}
|
|
||||||
|
|
||||||
if *tsQuery <= 65535 && *tsQuery != 0 {
|
|
||||||
config.ServerTS.PortQuery = uint16(*tsQuery)
|
|
||||||
}
|
|
||||||
|
|
||||||
if *webPort <= 65535 && *webPort != 0 {
|
|
||||||
config.ServerWeb.Port = uint16(*webPort)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue