From ff9790d2e5c3482ccd6109adac475a9868fc0ab6 Mon Sep 17 00:00:00 2001 From: horus Date: Tue, 20 Feb 2018 17:15:58 +0100 Subject: Refactoring + adds a more granular log level setting. (crawler) --- crawler/config.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'crawler/config.go') diff --git a/crawler/config.go b/crawler/config.go index 27aaa45..f342e30 100644 --- a/crawler/config.go +++ b/crawler/config.go @@ -1,6 +1,9 @@ package main import ( + "os" + + log "github.com/Sirupsen/logrus" "github.com/spf13/viper" ) @@ -18,7 +21,6 @@ type Config struct { Polr_URL string Polr_API_Key string - Debug bool FixDatabase bool ShopIDs []string } @@ -33,7 +35,6 @@ func (c *Config) parseConfig(configFile string) { viper.SetDefault("DB_Path", "./alkobote.db") - viper.SetDefault("Debug", false) viper.SetDefault("FixDatabase", false) viper.SetDefault("DisableURLShorter", false) viper.SetDefault("ShopIDs", []string{}) @@ -48,7 +49,14 @@ func (c *Config) parseConfig(configFile string) { viper.AddConfigPath("$HOME/.config/alkobote.de/") viper.AddConfigPath("$HOME/alkobote.de/") } else { - viper.AddConfigPath(configFile) + stat, err := os.Stat(configFile) + if stat.IsDir() || os.IsNotExist(err) { + // provided config file does not exist, so we add the path instead + viper.AddConfigPath(configFile) + } else { + // directly sets the config file + viper.SetConfigFile(configFile) + } } // Env variables need to be prefixed with "ALKOBOTE_" @@ -62,6 +70,7 @@ func (c *Config) parseConfig(configFile string) { if err != nil { Fatal(err, "Config: Error parsing config file.") } + log.Debug("Config: Config file used: " + viper.ConfigFileUsed()) c.setsConfig() } @@ -76,7 +85,6 @@ func (c *Config) setsConfig() { c.DBDBName = viper.GetString("DB_DBName") c.DBOptions = viper.GetString("DB_Options") c.DBPath = viper.GetString("DB_Path") - c.Debug = viper.GetBool("Debug") c.FixDatabase = viper.GetBool("FixDatabase") c.DisableURLShorter = viper.GetBool("DisableURLShorter") c.ShopIDs = viper.GetStringSlice("ShopIDs") -- cgit v1.2.3