summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsiddharth <s@ricketyspace.net>2021-11-27 16:32:48 -0500
committersiddharth <s@ricketyspace.net>2021-11-27 16:32:48 -0500
commitf40528802fa5deba941a04746e70a81f72ccf280 (patch)
tree8513377ecdc637fb89a134dd7718cd37b455a7e8
parentd33063ad277017e1ae6ac56cd93b3c27fcf15f9e (diff)
add feed package
Move feed type and function to its own package.
-rw-r--r--config/config.go40
-rw-r--r--feed/feed.go55
2 files changed, 60 insertions, 35 deletions
diff --git a/config/config.go b/config/config.go
index 3a5920c..5f0e1fd 100644
--- a/config/config.go
+++ b/config/config.go
@@ -10,19 +10,14 @@ import (
"path"
"strings"
+ "ricketyspace.net/fern/feed"
"ricketyspace.net/fern/file"
)
-type Feed struct {
- Id string `json:"id"`
- Source string `json:"source"`
- Schema string `json:"schema"`
-}
-
type FernConfig struct {
- YDLPath string `json:"ydl-path"`
- DumpDir string `json:"dump-dir"`
- Feeds []Feed `json:"feeds"`
+ YDLPath string `json:"ydl-path"`
+ DumpDir string `json:"dump-dir"`
+ Feeds []feed.Feed `json:"feeds"`
}
func Read() (*FernConfig, error) {
@@ -92,7 +87,7 @@ func (config *FernConfig) validate() error {
return fmt.Errorf("'feeds' not set in config")
}
for _, feed := range config.Feeds {
- err = feed.validate()
+ err = feed.Validate(config.DumpDir)
if err != nil {
return err
}
@@ -100,28 +95,3 @@ func (config *FernConfig) validate() error {
return nil
}
-
-func (feed *Feed) validate() error {
- // Check 'id'
- if len(feed.Id) == 0 {
- return fmt.Errorf("'id' not set in a feed")
- }
-
- // Check 'source'
- if len(feed.Source) == 0 {
- return fmt.Errorf("'source' not set in a feed '%s'", feed.Id)
- }
-
- // Check 'schema'
- schemaOK := false
- for _, schema := range []string{"npr", "youtube"} {
- if feed.Schema == schema {
- schemaOK = true
- }
- }
- if !schemaOK {
- return fmt.Errorf("schema '%s' for feed '%s' is not valid",
- feed.Schema, feed.Id)
- }
- return nil
-}
diff --git a/feed/feed.go b/feed/feed.go
new file mode 100644
index 0000000..13de66d
--- /dev/null
+++ b/feed/feed.go
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: ISC
+// Copyright © 2021 siddharth <s@ricketyspace.net>
+
+package feed
+
+import (
+ "fmt"
+ "os"
+ "path"
+)
+
+type Feed struct {
+ Id string `json:"id"`
+ Source string `json:"source"`
+ Schema string `json:"schema"`
+ DumpDir string
+}
+
+func (feed *Feed) Validate(baseDumpDir string) error {
+ _, err := os.Stat(baseDumpDir)
+ if err != nil {
+ return err
+ }
+
+ // Check 'id'
+ if len(feed.Id) == 0 {
+ return fmt.Errorf("'id' not set in a feed")
+ }
+
+ // Check 'source'
+ if len(feed.Source) == 0 {
+ return fmt.Errorf("'source' not set in a feed '%s'", feed.Id)
+ }
+
+ // Check 'schema'
+ schemaOK := false
+ for _, schema := range []string{"npr", "youtube"} {
+ if feed.Schema == schema {
+ schemaOK = true
+ }
+ }
+ if !schemaOK {
+ return fmt.Errorf("schema '%s' for feed '%s' is not valid",
+ feed.Schema, feed.Id)
+ }
+
+ // Set dump directory for feed and ensure it exists.
+ feed.DumpDir = path.Join(baseDumpDir, feed.Id)
+ err = os.MkdirAll(feed.DumpDir, 0755)
+ if err != nil {
+ return err
+ }
+
+ return nil
+}