diff options
Diffstat (limited to '')
-rw-r--r-- | internal/feed/feed.go | 12 | ||||
-rw-r--r-- | internal/feed/state.go | 13 | ||||
-rw-r--r-- | main.go | 4 | ||||
-rw-r--r-- | pkg/config/config.go | 6 |
4 files changed, 21 insertions, 14 deletions
diff --git a/internal/feed/feed.go b/internal/feed/feed.go index 4e84443..de28ef8 100644 --- a/internal/feed/feed.go +++ b/internal/feed/feed.go @@ -1,6 +1,7 @@ package feed import ( + "fmt" "strings" "time" @@ -58,3 +59,14 @@ func (feed *Feed) MarkSuccess() { feed.cached.Commit() } } + +func Create(parsedFeed *config.Feed, global config.GlobalOptions) (*Feed, error) { + var itemFilter *filter.Filter + var err error + if parsedFeed.ItemFilter != "" { + if itemFilter, err = filter.New(parsedFeed.ItemFilter); err != nil { + return nil, fmt.Errorf("Feed %s: Parsing item-filter: %w", parsedFeed.Name, err) + } + } + return &Feed{Feed: parsedFeed, Global: global, filter: itemFilter}, nil +} diff --git a/internal/feed/state.go b/internal/feed/state.go index 41d8751..364616f 100644 --- a/internal/feed/state.go +++ b/internal/feed/state.go @@ -2,12 +2,10 @@ package feed import ( "encoding/json" - "fmt" "sync" "github.com/mmcdole/gofeed" - "github.com/Necoro/feed2imap-go/internal/feed/filter" "github.com/Necoro/feed2imap-go/pkg/config" "github.com/Necoro/feed2imap-go/pkg/log" ) @@ -163,14 +161,11 @@ func NewState(cfg *config.Config) (*State, error) { } for name, parsedFeed := range cfg.Feeds { - var itemFilter *filter.Filter - var err error - if parsedFeed.ItemFilter != "" { - if itemFilter, err = filter.New(parsedFeed.ItemFilter); err != nil { - return nil, fmt.Errorf("Feed %s: Parsing item-filter: %w", parsedFeed.Name, err) - } + feed, err := Create(parsedFeed, cfg.GlobalOptions) + if err != nil { + return nil, err } - state.feeds[name] = &Feed{Feed: parsedFeed, Global: cfg.GlobalOptions, filter: itemFilter} + state.feeds[name] = feed } return &state, nil @@ -81,10 +81,6 @@ func run() error { return err } - if err = cfg.Validate(); err != nil { - return fmt.Errorf("Configuration invalid: %w", err) - } - state, err := feed.NewState(cfg) if err != nil { return err diff --git a/pkg/config/config.go b/pkg/config/config.go index 31012a1..bfaefdf 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -104,7 +104,7 @@ func (opt GlobalOptions) WithPartHtml() bool { return util.StrContains(opt.Parts, "html") } -// Load configuration from file +// Load configuration from file and validate it func Load(path string) (*Config, error) { log.Printf("Reading configuration file '%s'", path) @@ -118,6 +118,10 @@ func Load(path string) (*Config, error) { return nil, fmt.Errorf("while parsing: %w", err) } + if err = cfg.Validate(); err != nil { + return nil, fmt.Errorf("Configuration invalid: %w", err) + } + return cfg, nil } |