You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.1 KiB
Go
61 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"flag"
|
|
"log"
|
|
"os"
|
|
|
|
"github.com/BurntSushi/toml"
|
|
)
|
|
|
|
var cfg *ConfigWrapper
|
|
var flg map[string]string
|
|
|
|
type appConfig struct {
|
|
AuthToken string
|
|
ApplicationID string
|
|
CommandPrefix string
|
|
CommandTimeout int64
|
|
LogChannelID string
|
|
}
|
|
|
|
type databaseConfig struct {
|
|
DatabaseUser string
|
|
DatabasePassword string
|
|
}
|
|
|
|
type ConfigWrapper struct {
|
|
AppConfig appConfig
|
|
DatabaseConfig databaseConfig
|
|
}
|
|
|
|
//Loads config from
|
|
func LoadConfig() (*ConfigWrapper, error) {
|
|
if cfg == nil {
|
|
config_file := flg["config"]
|
|
_, err := os.Stat(config_file)
|
|
if err != nil {
|
|
log.Fatal("Unable to load config file at ", config_file)
|
|
return nil, err
|
|
}
|
|
|
|
_, err = toml.DecodeFile(config_file, &cfg)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
return nil, err
|
|
} else if *cfg == (ConfigWrapper{}) {
|
|
log.Fatal("Improperly configured config file.")
|
|
return nil, errors.New("Imporperly configured config file")
|
|
}
|
|
}
|
|
return cfg, nil
|
|
}
|
|
|
|
func commandLineFlags() {
|
|
flg = make(map[string]string)
|
|
configFlag := flag.String("config", "config.ini", "Path to a config file.")
|
|
flg["config"] = *configFlag
|
|
flag.Parse()
|
|
}
|