diff options
author | René 'Necoro' Neumann <necoro@necoro.eu> | 2020-04-17 20:57:01 +0200 |
---|---|---|
committer | René 'Necoro' Neumann <necoro@necoro.eu> | 2020-04-17 20:57:01 +0200 |
commit | c96ca310a019a892a717373677ded36dc0b9e2b3 (patch) | |
tree | 436da15c2011f980b61ec0dc356671e3c259a1a7 | |
parent | f9abafd3dd6745bffcd99efe1573e7d9e4c5c31c (diff) | |
download | feed2imap-go-c96ca310a019a892a717373677ded36dc0b9e2b3.tar.gz feed2imap-go-c96ca310a019a892a717373677ded36dc0b9e2b3.tar.bz2 feed2imap-go-c96ca310a019a892a717373677ded36dc0b9e2b3.zip |
Read and organize feeds
-rw-r--r-- | go.mod | 2 | ||||
-rw-r--r-- | go.sum | 4 | ||||
-rw-r--r-- | main.go | 116 |
3 files changed, 103 insertions, 19 deletions
@@ -2,4 +2,4 @@ module github.com/Necoro/feed2imap-go go 1.13 -require gopkg.in/yaml.v2 v2.2.8 +require gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c @@ -1,4 +1,4 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= -gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= @@ -5,50 +5,134 @@ import ( "fmt" "io/ioutil" "log" + "os" - "gopkg.in/yaml.v2" + "gopkg.in/yaml.v3" ) var cfg = flag.String("f", "config.yml", "configuration file") type ConfigMap map[string]interface{} -type Feed struct { +type configFeed struct { Name string Url string MinFreq int `yaml:"min-frequency"` ConfigMap `yaml:",inline"` } -type Group struct { +type configGroup struct { Group string - Feeds []GroupFeed + Feeds []configGroupFeed } -type GroupFeed struct { - Target string - Feed `yaml:",inline"` - Group `yaml:",inline"` +type configGroupFeed struct { + Target *string + configFeed `yaml:",inline"` + configGroup `yaml:",inline"` } -type Yaml struct { +func (grpFeed *configGroupFeed) isGroup() bool { + return grpFeed.configGroup.Group != "" +} + +func (grpFeed *configGroupFeed) isFeed() bool { + return grpFeed.configFeed.Name != "" +} + +func (grpFeed *configGroupFeed) target() string { + if grpFeed.Target != nil { + return *grpFeed.Target + } + if grpFeed.Name != "" { + return grpFeed.Name + } + + return grpFeed.Group +} + +type config struct { GlobalConfig ConfigMap `yaml:",inline"` - Feeds []GroupFeed + Feeds []configGroupFeed } -func main() { +type feed struct { + name string + target string + url string + minFreq int + config ConfigMap +} + +var feeds = make(map[string]*feed) + +func appTarget(target, app string) string { + if target == "" { + return app + } + + if app == "" { + return target + } + + return target + "/" + app +} + +func buildFeeds(cfg []configGroupFeed, target string) error { + for _, f := range cfg { + target := appTarget(target, f.target()) + switch { + case f.isFeed() && f.isGroup(): + return fmt.Errorf("Entry with Target %s is both a feed and a group", target) + case f.isFeed(): + name := f.Name + if _, ok := feeds[name]; !ok { + return fmt.Errorf("Duplicate feed name '%s'", name) + } + feeds[name] = &feed{ + name: name, + target: target, + url: f.Url, + minFreq: f.MinFreq, + config: f.ConfigMap, + } + case f.isGroup(): + if err := buildFeeds(f.Feeds, target); err != nil { + return err + } + } + } + + return nil +} + +func run() error { log.Print("Starting up...") flag.Parse() log.Printf("Reading configuration file '%s'", *cfg) buf, err := ioutil.ReadFile(*cfg) if err != nil { - msg := fmt.Sprint("No file found: ", *cfg) - panic(msg) + return fmt.Errorf("while reading '%s': %w", *cfg, err) } - var m Yaml - yaml.Unmarshal(buf, &m) - fmt.Printf("--- m:\n%+v\n\n", m) + var parsedCfg config + if err := yaml.Unmarshal(buf, &parsedCfg); err != nil { + return fmt.Errorf("while unmarshalling: %w", err) + } + fmt.Printf("--- parsedCfg:\n%+v\n\n", parsedCfg) + + if err := buildFeeds(parsedCfg.Feeds, ""); err != nil { + return fmt.Errorf("while parsing: %w", err) + } + return nil +} + +func main() { + if err := run(); err != nil { + log.SetOutput(os.Stderr) + log.Print("Error: ", err) + os.Exit(1) + } } |