package main import ( log "github.com/Sirupsen/logrus" "github.com/spf13/viper" ) type Config struct { DBDriver string DBDBName string DBHost string DBPort string DBUser string DBPassword string DBOptions string DBPath string // for sqlite Polr_URL string Polr_API_Key string Debug bool } // Parses the configuration and sets the configuration struct. func (c *Config) parseConfig(configFile string) { viper.SetDefault("DB_Driver", "mysql") viper.SetDefault("DB_DBName", "alkobote") viper.SetDefault("DB_Host", "localhost") viper.SetDefault("DB_Port", "3306") viper.SetDefault("DB_Path", "./alkobote.db") viper.SetDefault("Debug", false) // Name of the configuration file viper.SetConfigName("config") // Where to find the config file if configFile == "" { viper.AddConfigPath("/etc/alkobote.de/") viper.AddConfigPath(".") viper.AddConfigPath("$HOME/.config/alkobote.de/") viper.AddConfigPath("$HOME/alkobote.de/") } else { viper.AddConfigPath(configFile) } // Env variables need to be prefixed with "ALKOBOTE_" viper.SetEnvPrefix("ALKOBOTE") // Parses automatic the matching env variables viper.AutomaticEnv() // Reads the config err := viper.ReadInConfig() if err != nil { log.WithFields( log.Fields{ "error": err.Error(), }, ).Fatal("Fatal error config file") } c.setsConfig() } // Actually sets the config struct func (c *Config) setsConfig() { c.DBDriver = viper.GetString("DB_Driver") c.DBHost = viper.GetString("DB_Host") c.DBPort = viper.GetString("DB_Port") c.DBUser = viper.GetString("DB_User") c.DBPassword = viper.GetString("DB_Password") c.DBDBName = viper.GetString("DB_Name") c.DBOptions = viper.GetString("DB_Options") c.DBPath = viper.GetString("DB_Path") c.Debug = viper.GetBool("Debug") c.Polr_URL = viper.GetString("Polr_URL") c.Polr_API_Key = viper.GetString("Polr_API_Key") }