From f40528802fa5deba941a04746e70a81f72ccf280 Mon Sep 17 00:00:00 2001 From: siddharth Date: Sat, 27 Nov 2021 16:32:48 -0500 Subject: add feed package Move feed type and function to its own package. --- feed/feed.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 feed/feed.go (limited to 'feed') 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 + +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 +} -- cgit v1.2.3