Skip to content

Commit

Permalink
feat: added loading and saving the rss structure
Browse files Browse the repository at this point in the history
  • Loading branch information
TypicalAM committed Jan 3, 2023
1 parent b5713e1 commit f355ef0
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 61 deletions.
10 changes: 8 additions & 2 deletions internal/backend/cache/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Backend struct {
}

// New creates a new Cache Backend
func New() backend.Backend {
func New(urlFilePath string) backend.Backend {
// TODO: Make the path configurable
cache := newCache()

Expand All @@ -30,7 +30,7 @@ func New() backend.Backend {
// Return the backend
return Backend{
Cache: cache,
rss: rss.New("config.yml"),
rss: rss.New(urlFilePath),
}
}

Expand Down Expand Up @@ -120,5 +120,11 @@ func (b Backend) FetchArticles(feedName string) tea.Cmd {

// Close closes the backend
func (b Backend) Close() error {
// Try to save the rss
if err := b.rss.Save(); err != nil {
return err
}

// Try to save the cache
return b.Cache.Save()
}
14 changes: 7 additions & 7 deletions internal/backend/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Item struct {
// newCache creates a new cache
func newCache() Cache {
// Get the path to the cache file
path, err := getCachePath()
path, err := getDefaultPath()
// TODO: Handle error
if err != nil {
panic(err)
Expand Down Expand Up @@ -62,7 +62,7 @@ func (c *Cache) Load() error {

// Save writes the cache to disk
func (c *Cache) Save() error {
f, err := os.Create(c.path)
file, err := os.Create(c.path)
if err != nil {
// Try to create the directory
err = os.MkdirAll(filepath.Dir(c.path), 0755)
Expand All @@ -71,12 +71,12 @@ func (c *Cache) Save() error {
}

// Try to create the file again
f, err = os.Create(c.path)
file, err = os.Create(c.path)
if err != nil {
return err
}
}
defer f.Close()
defer file.Close()

// Try to encode the cache
content, err := json.Marshal(c.Content)
Expand All @@ -85,7 +85,7 @@ func (c *Cache) Save() error {
}

// Try to write the cache to disk
_, err = f.Write(content)
_, err = file.Write(content)
if err != nil {
return err
}
Expand Down Expand Up @@ -152,8 +152,8 @@ func fetchArticle(url string) (Item, error) {
}, nil
}

// getCachedPath returns the path to the cache file
func getCachePath() (string, error) {
// getDefaultPath returns the default path to the cache file
func getDefaultPath() (string, error) {
// Get the temporary directory
dir, err := os.UserCacheDir()
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions internal/backend/web/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ type Backend struct {
}

// New returns a new WebBackend
func New() Backend {
func New(urlFilePath string) Backend {
// FIXME: This should be configurable
return Backend{rss: rss.New("config.yml")}
return Backend{rss: rss.New(urlFilePath)}
}

// Name returns the name of the backend
Expand Down Expand Up @@ -107,5 +107,6 @@ func (b Backend) FetchArticles(feedName string) tea.Cmd {

// Close closes the backend
func (b Backend) Close() error {
return nil
// Try to save the rss
return b.rss.Save()
}
19 changes: 4 additions & 15 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package config

import (
"fmt"
"os"
"path/filepath"

"github.com/TypicalAM/goread/internal/backend"
"github.com/TypicalAM/goread/internal/backend/cache"
Expand All @@ -29,27 +27,18 @@ func New(backendType string, urlPath string) (Config, error) {
// Create a new config
config := Config{}

// If the config path is not supplied, use the default
if urlPath == "" {
// Get the default config path
configDir, err := os.UserConfigDir()
if err != nil {
return Config{}, err
}

// Create the config path
config.urlPath = filepath.Join(configDir, "goread", "urls.yml")
}
// Set the url path
config.urlPath = urlPath

// Determine the backend
var backend backend.Backend
switch backendType {
case BackendFake:
backend = fake.New()
case BackendWeb:
backend = web.New()
backend = web.New(config.urlPath)
case BackendCache:
backend = cache.New()
backend = cache.New(config.urlPath)
default:
return Config{}, fmt.Errorf("invalid backend type: %s", backendType)
}
Expand Down
111 changes: 77 additions & 34 deletions internal/rss/rss.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rss
import (
"errors"
"os"
"path/filepath"
"sort"
"strings"

Expand Down Expand Up @@ -36,47 +37,32 @@ type Feed struct {
// ErrNotFound is returned when a feed or category is not found
var ErrNotFound = errors.New("not found")

// New will create a new Rss structure, it fills it with basic items for now
func New(filePath string) Rss {
rss := Rss{filePath: "rss.yml"}
// New will create a new Rss structure
func New(urlFilePath string) Rss {
rss := Rss{filePath: urlFilePath}
err := rss.loadFromFile()
if err == nil {
return rss
}

rss.Categories = append(rss.Categories, Category{
Name: "News",
Description: "News from around the world",
})

rss.Categories = append(rss.Categories, Category{
Name: "Tech",
Description: "Tech news",
})

rss.Categories[0].Subscriptions = append(rss.Categories[0].Subscriptions, Feed{
Name: "BBC",
Description: "News from the BBC",
URL: "http://feeds.bbci.co.uk/news/rss.xml",
})

rss.Categories[1].Subscriptions = append(rss.Categories[1].Subscriptions, Feed{
Name: "Hacker News",
Description: "News from Hacker News",
URL: "https://news.ycombinator.com/rss",
})

rss.Categories[1].Subscriptions = append(rss.Categories[1].Subscriptions, Feed{
Name: "Golang subreddit",
Description: "News from the Golang subreddit",
URL: "https://www.reddit.com/r/golang/.rss",
})

rss.Categories = append(rss.Categories, createBasicCategories()...)
return rss
}

// loadFromFile will load the Rss structure from a file
func (rss *Rss) loadFromFile() error {
// Check if the path is valid
if rss.filePath == "" {
// Get the default path
path, err := getDefaultPath()
if err != nil {
return err
}

// Set the path
rss.filePath = path
}

// Try to open the file
fileContent, err := os.ReadFile(rss.filePath)
if err != nil {
Expand All @@ -93,8 +79,8 @@ func (rss *Rss) loadFromFile() error {
return nil
}

// WriteToFile will write the Rss structure to a file
func (rss Rss) WriteToFile() error {
// Save will write the Rss structure to a file
func (rss *Rss) Save() error {
// Try to marshall the data
yamlData, err := yaml.Marshal(rss)
if err != nil {
Expand All @@ -104,7 +90,17 @@ func (rss Rss) WriteToFile() error {
// Try to open the file, if it doesn't exist, create it
file, err := os.OpenFile(rss.filePath, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return err
// Try to create the directory
err = os.MkdirAll(filepath.Dir(rss.filePath), 0755)
if err != nil {
return err
}

// Try to create the file again
file, err = os.Create(rss.filePath)
if err != nil {
return err
}
}
defer file.Close()

Expand Down Expand Up @@ -220,3 +216,50 @@ func HTMLToText(content string) string {
// Return the text
return doc.Text()
}

// getDefaultPath will return the default path for the urls file
func getDefaultPath() (string, error) {
// Get the default config path
configDir, err := os.UserConfigDir()
if err != nil {
return "", err
}

// Create the config path
return filepath.Join(configDir, "goread", "urls.yml"), nil
}

// createBasicCategories will create some basic categories
func createBasicCategories() []Category {
var categories []Category

categories = append(categories, Category{
Name: "News",
Description: "News from around the world",
})

categories = append(categories, Category{
Name: "Tech",
Description: "Tech news",
})

categories[0].Subscriptions = append(categories[0].Subscriptions, Feed{
Name: "BBC",
Description: "News from the BBC",
URL: "http://feeds.bbci.co.uk/news/rss.xml",
})

categories[1].Subscriptions = append(categories[1].Subscriptions, Feed{
Name: "Hacker News",
Description: "News from Hacker News",
URL: "https://news.ycombinator.com/rss",
})

categories[1].Subscriptions = append(categories[1].Subscriptions, Feed{
Name: "Golang subreddit",
Description: "News from the Golang subreddit",
URL: "https://www.reddit.com/r/golang/.rss",
})

return categories
}

0 comments on commit f355ef0

Please sign in to comment.