diff options
| -rw-r--r-- | crawler/config.go | 16 | ||||
| -rw-r--r-- | crawler/init.go | 34 | ||||
| -rw-r--r-- | crawler/log.go | 19 | ||||
| -rw-r--r-- | crawler/main.go | 31 | ||||
| -rw-r--r-- | crawler/struct.go | 32 |
5 files changed, 78 insertions, 54 deletions
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") diff --git a/crawler/init.go b/crawler/init.go new file mode 100644 index 0000000..abf212b --- /dev/null +++ b/crawler/init.go @@ -0,0 +1,34 @@ +package main + +import ( + "flag" + "strings" + + log "github.com/Sirupsen/logrus" + //"github.com/spf13/viper" + //flag "github.com/spf13/pflag" +) + +// global config, gets overwritten by main +var _conf Config + +func init() { + // we need to parse the config because of log level setting + configFile := flag.String("config", "", "path to config file") + debug := flag.Bool("debug", false, "debug outputs") + loglevel_f := flag.String("loglevel", "Warn", `sets log level, can be "Warn", "Info" or "Debug"`) + loglevel := strings.ToLower(*loglevel_f) + + flag.Parse() + + if *debug || loglevel == "debug" { + log.SetLevel(log.DebugLevel) + } else if loglevel == "Info" { + log.SetLevel(log.InfoLevel) + } else { + log.SetLevel(log.WarnLevel) + } + + _conf.parseConfig(*configFile) + +} diff --git a/crawler/log.go b/crawler/log.go index 0cd681d..6288a29 100644 --- a/crawler/log.go +++ b/crawler/log.go @@ -1,28 +1,9 @@ package main import ( - "flag" - log "github.com/Sirupsen/logrus" ) -// global config, gets overwritten by main -var _conf Config - -func init() { - // we need to parse the config because of log level setting - configFile := flag.String("config", "", "path to config file") - flag.Parse() - - _conf.parseConfig(*configFile) - - if _conf.Debug { - log.SetLevel(log.DebugLevel) - } else { - log.SetLevel(log.WarnLevel) - } -} - func Fatal(err error, msg string) { if err != nil { log.WithFields( diff --git a/crawler/main.go b/crawler/main.go index b75c21a..8370db5 100644 --- a/crawler/main.go +++ b/crawler/main.go @@ -21,37 +21,6 @@ type App struct { Debug bool } -type Angebot struct { - Id int - Name string - Abv float32 - Volume float32 - Age int - Shop int - Url string - Short_url string - Original_price int - Discounted_price int - Base_price int - Image_url string - Spirit_type string - Website string - Valid_until int - - error_msg string - error_ctx string -} - -type Shop struct { - Id int - Name string - Url string - Short_url string - Logo_url string - Shipping_costs int - Free_shipping string -} - func main() { var err error diff --git a/crawler/struct.go b/crawler/struct.go new file mode 100644 index 0000000..1965c2e --- /dev/null +++ b/crawler/struct.go @@ -0,0 +1,32 @@ +package main + +type Angebot struct { + Id int + Name string + Abv float32 + Volume float32 + Age int + Shop int + Url string + Short_url string + Original_price int + Discounted_price int + Base_price int + Image_url string + Spirit_type string + Website string + Valid_until int + + error_msg string + error_ctx string +} + +type Shop struct { + Id int + Name string + Url string + Short_url string + Logo_url string + Shipping_costs int + Free_shipping string +} |
