package config import ( "regexp" "strings" "github.com/pelletier/go-toml" "go.jolheiser.com/beaver" ) type Config struct { Reddit RedditConfig `toml:"reddit"` Twitter TwitterConfig `toml:"twitter"` } func (c *Config) loadReddit() { for _, sub := range c.Reddit.SubReddits { c.Reddit.Map[strings.ToLower(sub.Name)] = sub if sub.TitleLimit == 0 || sub.TitleLimit > 253 { sub.TitleLimit = 253 } if sub.BodyLimit == 0 || sub.BodyLimit > 2045 { sub.BodyLimit = 2045 } sub.FlairWhitelistRe = make([]*regexp.Regexp, len(sub.FlairWhitelist)) for idx, f := range sub.FlairWhitelist { sub.FlairWhitelistRe[idx] = regexp.MustCompile(f) } sub.FlairBlacklistRe = make([]*regexp.Regexp, len(sub.FlairBlacklist)) for idx, f := range sub.FlairBlacklist { sub.FlairBlacklistRe[idx] = regexp.MustCompile(f) } sub.TitleWhitelistRe = make([]*regexp.Regexp, len(sub.TitleWhitelist)) for idx, t := range sub.TitleWhitelist { sub.TitleWhitelistRe[idx] = regexp.MustCompile(t) } sub.TitleBlacklistRe = make([]*regexp.Regexp, len(sub.TitleBlacklist)) for idx, t := range sub.TitleBlacklist { sub.TitleBlacklistRe[idx] = regexp.MustCompile(t) } sub.BodyWhitelistRe = make([]*regexp.Regexp, len(sub.BodyWhitelist)) for idx, b := range sub.BodyWhitelist { sub.BodyWhitelistRe[idx] = regexp.MustCompile(b) } sub.BodyBlacklistRe = make([]*regexp.Regexp, len(sub.BodyBlacklist)) for idx, b := range sub.BodyBlacklist { sub.BodyBlacklistRe[idx] = regexp.MustCompile(b) } } } func Load(configPath string) (*Config, error) { cfg := Config{ Reddit: RedditConfig{ Map: make(map[string]*SubReddit), }, } tree, err := toml.LoadFile(configPath) if err != nil { return nil, err } if err := tree.Unmarshal(&cfg); err != nil { return nil, err } cfg.loadReddit() beaver.Debug(cfg) return &cfg, nil }