aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--go.mod2
-rw-r--r--go.sum4
-rw-r--r--main.go116
3 files changed, 103 insertions, 19 deletions
diff --git a/go.mod b/go.mod
index 590d5c2..63d186d 100644
--- a/go.mod
+++ b/go.mod
@@ -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
diff --git a/go.sum b/go.sum
index 74b18ac..756b14d 100644
--- a/go.sum
+++ b/go.sum
@@ -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=
diff --git a/main.go b/main.go
index 9f20203..b238ff0 100644
--- a/main.go
+++ b/main.go
@@ -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)
+ }
}