aboutsummaryrefslogtreecommitdiff
path: root/internal/feed/parse.go
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.eu>2020-05-25 20:33:06 +0200
committerRené 'Necoro' Neumann <necoro@necoro.eu>2020-05-25 20:33:06 +0200
commitdae31bb0192e6b519111d3cb80ddd4312cda306c (patch)
tree132f610d3e2d71ec396f6a3cd960ac33aad35df2 /internal/feed/parse.go
parent3cbf95d38b6f8bd17b4312371ed07e6847ff0f5c (diff)
downloadfeed2imap-go-dae31bb0192e6b519111d3cb80ddd4312cda306c.tar.gz
feed2imap-go-dae31bb0192e6b519111d3cb80ddd4312cda306c.tar.bz2
feed2imap-go-dae31bb0192e6b519111d3cb80ddd4312cda306c.zip
'Exec' as an alternative to 'Url'
Diffstat (limited to 'internal/feed/parse.go')
-rw-r--r--internal/feed/parse.go47
1 files changed, 39 insertions, 8 deletions
diff --git a/internal/feed/parse.go b/internal/feed/parse.go
index a8f705a..77dfe69 100644
--- a/internal/feed/parse.go
+++ b/internal/feed/parse.go
@@ -2,6 +2,8 @@ package feed
import (
"fmt"
+ "io"
+ "os/exec"
"github.com/google/uuid"
"github.com/mmcdole/gofeed"
@@ -13,15 +15,44 @@ import (
func (feed *Feed) parse() error {
fp := gofeed.NewParser()
- // we do not use the http support in gofeed, so that we can control the behavior of http requests
- // and ensure it to be the same in all places
- resp, cancel, err := http.Get(feed.Url, feed.Global.Timeout, feed.NoTLS)
- if err != nil {
- return fmt.Errorf("while fetching %s from %s: %w", feed.Name, feed.Url, err)
+ var reader io.Reader
+ var cleanup func() error
+
+ if feed.Url != "" {
+ // we do not use the http support in gofeed, so that we can control the behavior of http requests
+ // and ensure it to be the same in all places
+ resp, cancel, err := http.Get(feed.Url, feed.Global.Timeout, feed.NoTLS)
+ if err != nil {
+ return fmt.Errorf("while fetching %s from %s: %w", feed.Name, feed.Url, err)
+ }
+ defer cancel() // includes resp.Body.Close
+
+ reader = resp.Body
+ cleanup = func() error { return nil }
+ } else { // exec
+ // we use the same context as for HTTP
+ ctx, cancel := http.Context(feed.Global.Timeout)
+ cmd := exec.CommandContext(ctx, feed.Exec[0], feed.Exec[1:]...)
+ defer func() {
+ cancel()
+ // cmd.Wait might have already been called -- but call it again to be sure
+ _ = cmd.Wait()
+ }()
+
+ stdout, err := cmd.StdoutPipe()
+ if err != nil {
+ return fmt.Errorf("preparing exec for feed '%s': %w", feed.Name, err)
+ }
+
+ if err = cmd.Start(); err != nil {
+ return fmt.Errorf("starting exec for feed '%s: %w", feed.Name, err)
+ }
+
+ reader = stdout
+ cleanup = cmd.Wait
}
- defer cancel() // includes resp.Body.Close
- parsedFeed, err := fp.Parse(resp.Body)
+ parsedFeed, err := fp.Parse(reader)
if err != nil {
return fmt.Errorf("parsing feed '%s': %w", feed.Name, err)
}
@@ -31,7 +62,7 @@ func (feed *Feed) parse() error {
for idx, feedItem := range parsedFeed.Items {
feed.items[idx] = item{Feed: parsedFeed, Item: feedItem, itemId: uuid.New(), feed: feed}
}
- return nil
+ return cleanup()
}
func handleFeed(feed *Feed) {
2021-02-16Issue #46: Fix semantics of `n` resultRené 'Necoro' Neumann2-9/+15 Per contract, the returned number of bytes written should be the number of bytes _from the input_. Therefore, the added bytes (`\r` or `\n`) shall not count into that number. 2021-02-16Issue #46: Move and rename writer; add commentsRené 'Necoro' Neumann3-12/+21 2021-02-15Issue #46: Improvements; add testsRené 'Necoro' Neumann2-1/+48 2021-02-15Bump github.com/google/uuid from 1.1.4 to 1.2.0dependabot[bot]2-3/+3 Bumps [github.com/google/uuid](https://github.com/google/uuid) from 1.1.4 to 1.2.0. - [Release notes](https://github.com/google/uuid/releases) - [Commits](https://github.com/google/uuid/compare/v1.1.4...v1.2.0) Signed-off-by: dependabot[bot] <support@github.com> 2021-02-15Issue #46: Make the resulting email body not to include single \r or \n. ↵René 'Necoro' Neumann2-2/+66 This now pleases Cyrus IMAP. 2021-01-20Bump github.com/PuerkitoBio/goquery from 1.6.0 to 1.6.1dependabot[bot]2-3/+3 Bumps [github.com/PuerkitoBio/goquery](https://github.com/PuerkitoBio/goquery) from 1.6.0 to 1.6.1. - [Release notes](https://github.com/PuerkitoBio/goquery/releases) - [Commits](https://github.com/PuerkitoBio/goquery/compare/v1.6.0...v1.6.1) Signed-off-by: dependabot[bot] <support@github.com> 2021-01-09Bump github.com/google/uuid from 1.1.2 to 1.1.4dependabot[bot]2-3/+3 Bumps [github.com/google/uuid](https://github.com/google/uuid) from 1.1.2 to 1.1.4. - [Release notes](https://github.com/google/uuid/releases) - [Commits](https://github.com/google/uuid/compare/v1.1.2...v1.1.4) Signed-off-by: dependabot[bot] <support@github.com> 2021-01-09Bump github.com/emersion/go-message from 0.14.0 to 0.14.1 (#42)dependabot[bot]2-3/+3 Bumps [github.com/emersion/go-message](https://github.com/emersion/go-message) from 0.14.0 to 0.14.1. - [Release notes](https://github.com/emersion/go-message/releases) - [Commits](https://github.com/emersion/go-message/compare/v0.14.0...v0.14.1) Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> 2020-11-28Bump github.com/emersion/go-message from 0.13.0 to 0.14.0 (#38)dependabot[bot]2-3/+9 Bumps [github.com/emersion/go-message](https://github.com/emersion/go-message) from 0.13.0 to 0.14.0. - [Release notes](https://github.com/emersion/go-message/releases) - [Commits](https://github.com/emersion/go-message/compare/v0.13.0...v0.14.0) Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> 2020-11-28Bump github.com/google/go-cmp from 0.5.2 to 0.5.4 (#37)dependabot[bot]2-3/+3 Bumps [github.com/google/go-cmp](https://github.com/google/go-cmp) from 0.5.2 to 0.5.4. - [Release notes](https://github.com/google/go-cmp/releases) - [Commits](https://github.com/google/go-cmp/compare/v0.5.2...v0.5.4) Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> 2020-11-23Fix release.ymlv0.5.2René 'Necoro' Neumann1-3/+10 2020-11-23Prepare v0.5.2René 'Necoro' Neumann3-3/+8 2020-11-20Bump github.com/gabriel-vasile/mimetype from 1.1.1 to 1.1.2dependabot[bot]2-3/+3 Bumps [github.com/gabriel-vasile/mimetype](https://github.com/gabriel-vasile/mimetype) from 1.1.1 to 1.1.2. - [Release notes](https://github.com/gabriel-vasile/mimetype/releases) - [Changelog](https://github.com/gabriel-vasile/mimetype/blob/master/CHANGELOG.md) - [Commits](https://github.com/gabriel-vasile/mimetype/compare/v1.1.1...v1.1.2) Signed-off-by: dependabot[bot] <support@github.com> 2020-11-04Clean dependabot.ymlRené 'Necoro' Neumann1-4/+0