aboutsummaryrefslogtreecommitdiff
path: root/internal/config
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.eu>2020-04-17 21:53:15 +0200
committerRené 'Necoro' Neumann <necoro@necoro.eu>2020-04-17 21:53:15 +0200
commitb01a98682ad7881c997fe5fc0770ef984013111f (patch)
tree9b52a17c17390d2738ec862de3799e8ccd9348c7 /internal/config
parentc96ca310a019a892a717373677ded36dc0b9e2b3 (diff)
downloadfeed2imap-go-b01a98682ad7881c997fe5fc0770ef984013111f.tar.gz
feed2imap-go-b01a98682ad7881c997fe5fc0770ef984013111f.tar.bz2
feed2imap-go-b01a98682ad7881c997fe5fc0770ef984013111f.zip
Split up main.go
Diffstat (limited to 'internal/config')
-rw-r--r--internal/config/config.go52
-rw-r--r--internal/config/yaml.go71
2 files changed, 123 insertions, 0 deletions
diff --git a/internal/config/config.go b/internal/config/config.go
new file mode 100644
index 0000000..2df7d98
--- /dev/null
+++ b/internal/config/config.go
@@ -0,0 +1,52 @@
+package config
+
+import (
+ "fmt"
+ "gopkg.in/yaml.v3"
+ "io/ioutil"
+)
+
+type Map map[string]interface{}
+type Feeds map[string]*Feed
+
+type config struct {
+ GlobalConfig Map `yaml:",inline"`
+ Feeds []configGroupFeed
+}
+
+type Config struct {
+ GlobalConfig Map `yaml:",inline"`
+ Feeds Feeds
+}
+
+type Feed struct {
+ Name string
+ Target string
+ Url string
+ MinFreq int
+ Config Map
+}
+
+func Load(path string) (*Config, error) {
+ buf, err := ioutil.ReadFile(path)
+ if err != nil {
+ return nil, fmt.Errorf("while reading '%s': %w", path, err)
+ }
+
+ var parsedCfg config
+ if err := yaml.Unmarshal(buf, &parsedCfg); err != nil {
+ return nil, fmt.Errorf("while unmarshalling: %w", err)
+ }
+ fmt.Printf("--- parsedCfg:\n%+v\n\n", parsedCfg)
+
+ var finishedCfg = Config{
+ GlobalConfig: parsedCfg.GlobalConfig,
+ Feeds: make(Feeds),
+ }
+
+ if err := buildFeeds(parsedCfg.Feeds, "", finishedCfg.Feeds); err != nil {
+ return nil, fmt.Errorf("while parsing: %w", err)
+ }
+
+ return &finishedCfg, nil
+}
diff --git a/internal/config/yaml.go b/internal/config/yaml.go
new file mode 100644
index 0000000..335dede
--- /dev/null
+++ b/internal/config/yaml.go
@@ -0,0 +1,71 @@
+package config
+
+import "fmt"
+
+type Group struct {
+ Group string
+ Feeds []configGroupFeed
+}
+
+type configGroupFeed struct {
+ Target *string
+ Feed `yaml:",inline"`
+ Group `yaml:",inline"`
+}
+
+func (grpFeed *configGroupFeed) isGroup() bool {
+ return grpFeed.Group.Group != ""
+}
+
+func (grpFeed *configGroupFeed) isFeed() bool {
+ return grpFeed.Name != ""
+}
+
+func (grpFeed *configGroupFeed) target() string {
+ if grpFeed.Target != nil {
+ return *grpFeed.Target
+ }
+ if grpFeed.Name != "" {
+ return grpFeed.Name
+ }
+
+ return grpFeed.Group.Group
+}
+
+func appTarget(target, app string) string {
+ if target == "" {
+ return app
+ }
+
+ if app == "" {
+ return target
+ }
+
+ return target + "/" + app
+}
+
+// Parse the group structure and populate the `Target` fields in the feeds
+func buildFeeds(cfg []configGroupFeed, target string, feeds Feeds) 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.Feed.Name
+ if _, ok := feeds[name]; !ok {
+ return fmt.Errorf("Duplicate Feed Name '%s'", name)
+ }
+ f.Feed.Target = target
+ feeds[name] = &f.Feed
+
+ case f.isGroup():
+ if err := buildFeeds(f.Group.Feeds, target, feeds); err != nil {
+ return err
+ }
+ }
+ }
+
+ return nil
+}
10:31:51 +0000'>2008-01-14 r603@Devoty: necoro | 2008-01-14 11:30:26 +0100Necoro4-26/+38 2008-01-11 r598@Devoty: necoro | 2008-01-10 16:36:29 +0100Necoro4-10/+50 2008-01-11 r597@Devoty: necoro | 2008-01-10 14:12:35 +0100Necoro1-3/+3 2008-01-10 r595@Devoty: necoro | 2008-01-10 04:04:15 +0100Necoro1-52/+3 2008-01-09 r586@Devoty: necoro | 2008-01-09 14:54:18 +0100Necoro1-1/+1 2007-12-06 r577@Devoty: necoro | 2007-12-06 20:37:36 +0100Necoro1-1/+1 2007-12-06 r572@Devoty: necoro | 2007-11-28 08:48:15 +0100Necoro1-0/+1 2007-11-27 r570@Devoty: necoro | 2007-11-27 02:08:21 +0100Necoro3-84/+108