package main import ( "errors" "strings" log "github.com/sirupsen/logrus" flag "github.com/spf13/pflag" ) // global config, gets overwritten by main var _conf Config func init() { // overwrites unhelpful error message flag.ErrHelp = errors.New("") // we need to parse the config because of log level setting configFile := flag.StringP("config", "c", "", "path to config file") debug := flag.BoolP("debug", "d", false, "set log level to \"Debug\"") verbose := flag.BoolP("verbose", "v", false, "set log level to \"Debug\", same as --debug") silent := flag.BoolP("silent", "s", false, "suppress output except warnings") loglevel_f := flag.String("loglevel", "Warn", `set log level, can be "Warn", "Info" or "Debug"`) flag.BoolP("list-shops", "l", false, `list all crawlable shops`) shopids_f := flag.StringP("only-shop", "o", "", `comma separated list of shop ids, crawl only these`) not_shopids_f := flag.StringP("exclude-shop", "x", "", `comma separated list of shop ids, DO NOT crawl these`) user_agent_f := flag.StringP("user-agent", "u", "", "set user agent") delay_f := flag.Int("delay", 0, "enable and set delay in seconds between crawls (default 0)") ignore_robots_f := flag.Bool("ignore-robots-txt", true, "ignore robots.txt") flag.Parse() loglevel := strings.ToLower(*loglevel_f) if *debug || *verbose || loglevel == "debug" { log.SetLevel(log.DebugLevel) } else if loglevel == "info" { log.SetLevel(log.InfoLevel) } else { log.SetLevel(log.WarnLevel) } if *silent { log.SetLevel(log.WarnLevel) } _conf.parseConfig(*configFile) if *user_agent_f != "" { _conf.UserAgent = *user_agent_f } if *delay_f != 0 { _conf.Delay = *delay_f } if !*ignore_robots_f { _conf.IgnoreRobotsTXT = *ignore_robots_f } if _conf.Debug && !*silent { log.SetLevel(log.DebugLevel) } if "" != *shopids_f { _conf.ShopIDs = strings.Split(*shopids_f, ",") } if "" != *not_shopids_f { _conf.ExcludeShopIDs = strings.Split(*not_shopids_f, ",") } if "" != *shopids_f && "" != *not_shopids_f { log.Fatal("init.go: Config error: Cannot use both flags --exclude-shop and --only-shop at the same time.") } }