aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.eu>2020-04-17 20:57:01 +0200
committerRené 'Necoro' Neumann <necoro@necoro.eu>2020-04-17 20:57:01 +0200
commitc96ca310a019a892a717373677ded36dc0b9e2b3 (patch)
tree436da15c2011f980b61ec0dc356671e3c259a1a7
parentf9abafd3dd6745bffcd99efe1573e7d9e4c5c31c (diff)
downloadfeed2imap-go-c96ca310a019a892a717373677ded36dc0b9e2b3.tar.gz
feed2imap-go-c96ca310a019a892a717373677ded36dc0b9e2b3.tar.bz2
feed2imap-go-c96ca310a019a892a717373677ded36dc0b9e2b3.zip
Read and organize feeds
-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)
+ }
}
ers/cgit.git/commit/Makefile?h=v0.8.3.1&id=a581ed8d6c15b0734b082fbadf0a907c2b170423&follow=1'>Let 'make install' clear all cachefilesLars Hjemli1-0/+2 Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2006-12-11Fix cache algorithm loopholeLars Hjemli3-11/+16 This closes the door for unneccessary calls to cgit_fill_cache(). Noticed by Linus. Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2006-12-10Add version identifier in generated filesLars Hjemli2-9/+14 Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2006-12-10Add license file and copyright noticesLars Hjemli5-0/+372 Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2006-12-10Add caching infrastructureLars Hjemli9-28/+353 This enables internal caching of page output. Page requests are split into four groups: 1) repo listing (front page) 2) repo summary 3) repo pages w/symbolic references in query string 4) repo pages w/constant sha1's in query string Each group has a TTL specified in minutes. When a page is requested, a cached filename is stat(2)'ed and st_mtime is compared to time(2). If TTL has expired (or the file didn't exist), the cached file is regenerated. When generating a cached file, locking is used to avoid parallell processing of the request. If multiple processes tries to aquire the same lock, the ones who fail to get the lock serves the (expired) cached file. If the cached file don't exist, the process instead calls sched_yield(2) before restarting the request processing. Signed-off-by: Lars Hjemli <hjemli@gmail.com>