From ed1e06e6d81645fb5fedd89018c30f95b7598f84 Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Sun, 19 Apr 2020 22:41:41 +0200 Subject: Rename package 'parse' to 'feed' --- internal/feed/parse.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ internal/parse/parse.go | 48 ------------------------------------------------ main.go | 4 ++-- 3 files changed, 50 insertions(+), 50 deletions(-) create mode 100644 internal/feed/parse.go delete mode 100644 internal/parse/parse.go diff --git a/internal/feed/parse.go b/internal/feed/parse.go new file mode 100644 index 0000000..53ceb00 --- /dev/null +++ b/internal/feed/parse.go @@ -0,0 +1,48 @@ +package feed + +import ( + ctxt "context" + "fmt" + "sync" + "time" + + "github.com/mmcdole/gofeed" + + "github.com/Necoro/feed2imap-go/internal/config" + "github.com/Necoro/feed2imap-go/internal/log" +) + +func context() (ctxt.Context, ctxt.CancelFunc) { + return ctxt.WithTimeout(ctxt.Background(), 60*time.Second) +} + +func parseFeed(feed *config.Feed) error { + ctx, cancel := context() + defer cancel() + fp := gofeed.NewParser() + if _, err := fp.ParseURLWithContext(feed.Url, ctx); err != nil { + return fmt.Errorf("while fetching %s from %s: %w", feed.Name, feed.Url, err) + } + + return nil +} + +func handleFeed(feed *config.Feed, wg *sync.WaitGroup) { + defer wg.Done() + log.Printf("Fetching %s from %s", feed.Name, feed.Url) + + if err := parseFeed(feed); err != nil { + log.Error(err) + } +} + +func Parse(feeds config.Feeds) { + var wg sync.WaitGroup + wg.Add(len(feeds)) + + for _, feed := range feeds { + go handleFeed(feed, &wg) + } + + wg.Wait() +} diff --git a/internal/parse/parse.go b/internal/parse/parse.go deleted file mode 100644 index ddf1f0f..0000000 --- a/internal/parse/parse.go +++ /dev/null @@ -1,48 +0,0 @@ -package parse - -import ( - ctxt "context" - "fmt" - "sync" - "time" - - "github.com/mmcdole/gofeed" - - "github.com/Necoro/feed2imap-go/internal/config" - "github.com/Necoro/feed2imap-go/internal/log" -) - -func context() (ctxt.Context, ctxt.CancelFunc) { - return ctxt.WithTimeout(ctxt.Background(), 60*time.Second) -} - -func parseFeed(feed *config.Feed) error { - ctx, cancel := context() - defer cancel() - fp := gofeed.NewParser() - if _, err := fp.ParseURLWithContext(feed.Url, ctx); err != nil { - return fmt.Errorf("while fetching %s from %s: %w", feed.Name, feed.Url, err) - } - - return nil -} - -func handleFeed(feed *config.Feed, wg *sync.WaitGroup) { - defer wg.Done() - log.Printf("Fetching %s from %s", feed.Name, feed.Url) - - if err := parseFeed(feed); err != nil { - log.Error(err) - } -} - -func Parse(feeds config.Feeds) { - var wg sync.WaitGroup - wg.Add(len(feeds)) - - for _, feed := range feeds { - go handleFeed(feed, &wg) - } - - wg.Wait() -} diff --git a/main.go b/main.go index f90cc50..373a491 100644 --- a/main.go +++ b/main.go @@ -7,9 +7,9 @@ import ( "os" "github.com/Necoro/feed2imap-go/internal/config" + "github.com/Necoro/feed2imap-go/internal/feed" "github.com/Necoro/feed2imap-go/internal/imap" "github.com/Necoro/feed2imap-go/internal/log" - "github.com/Necoro/feed2imap-go/internal/parse" ) var cfgFile = flag.String("f", "config.yml", "configuration file") @@ -27,7 +27,7 @@ func run() error { return err } - parse.Parse(cfg.Feeds) + feed.Parse(cfg.Feeds) imapUrl, err := url.Parse(cfg.GlobalConfig["target"].(string)) if err != nil { -- cgit v1.2.3-70-g09d2