package main import ( "os" 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 UserAgent string Delay int IgnoreRobotsTXT bool BasicAuthUsername string BasicAuthPassword string Debug bool // sets log level to debug } // Parses the configuration and sets the configuration struct. func (c *Config) parseConfig(configFile string) { viper.SetDefault("DB_Driver", "mysql") viper.SetDefault("DB_DBName", "ghrss") viper.SetDefault("DB_Host", "localhost") viper.SetDefault("DB_Port", "3306") viper.SetDefault("Debug", false) viper.SetDefault("Delay", 0) // needs some refactoring to truly respect robots.txt viper.SetDefault("IgnoreRobotsTXT", true) viper.SetDefault("UserAgent", "colly - a friendly crawler :)") // Name of the configuration file viper.SetConfigName("config") // Where to find the config file if configFile == "" { viper.AddConfigPath("/etc/ghrss/") viper.AddConfigPath(".") viper.AddConfigPath("$HOME/app/ghrss/") viper.AddConfigPath("$HOME/.config/ghrss/") viper.AddConfigPath("$HOME/ghrss/") } else { stat, err := os.Stat(configFile) if os.IsNotExist(err) { // provided config file does not exist, so we add the path instead viper.AddConfigPath(configFile) } else if err == nil && stat.IsDir() { // adds the path to look for the config file viper.AddConfigPath(configFile) } else if err == nil { // directly sets the config file viper.SetConfigFile(configFile) } else { // if we are here something went wrong Warn(err, "config.go: os.Stat("+configFile+") error") // adding the path nonetheless because it's not hurting 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 { Fatal(err, "Config: Error parsing config file.") } log.Debug("Config: Config file used: " + viper.ConfigFileUsed()) 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_DBName") c.DBOptions = viper.GetString("DB_Options") c.UserAgent = viper.GetString("UserAgent") c.Delay = viper.GetInt("Delay") c.IgnoreRobotsTXT = viper.GetBool("IgnoreRobotsTXT") c.BasicAuthUsername = viper.GetString("BasicAuthUsername") c.BasicAuthPassword = viper.GetString("BasicAuthPassword") c.Debug = viper.GetBool("Debug") }