Skip to content

Commit

Permalink
chore: split out commands/ into smaller files (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
guyfedwards authored Jun 12, 2024
1 parent 02d80fa commit 8074b69
Show file tree
Hide file tree
Showing 8 changed files with 1,024 additions and 646 deletions.
150 changes: 0 additions & 150 deletions internal/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,87 +121,6 @@ func GetWslHostName() string {
return strings.TrimSpace(string(out))
}

func (c Commands) CleanFeeds() error {
urls, err := c.store.GetAllFeedURLs()
if err != nil {
return fmt.Errorf("[commands.go]: %w", err)
}

var urlsToRemove []string

for _, u := range urls {
inFeeds := false
for _, f := range c.config.Feeds {
if f.URL == u {
inFeeds = true
}
}

if !inFeeds {
urlsToRemove = append(urlsToRemove, u)
}
}

for _, url := range urlsToRemove {
err := c.store.DeleteByFeedURL(url, false)
if err != nil {
return fmt.Errorf("[commands.go]: %w", err)
}
}

return nil
}

func (c Commands) TUI() error {
debug := os.Getenv("DEBUGNOM")
if debug != "" {
f, err := tea.LogToFile(debug, "debug")
if err != nil {
fmt.Println("fatal:", err)
os.Exit(1)
}
defer f.Close()
}

its, err := c.GetAllFeeds()
if err != nil {
return fmt.Errorf("commands List: %w", err)
}

var errorItems []ErrorItem
// if no feeds in store, fetchAllFeeds, which will return previews
if len(c.config.PreviewFeeds) > 0 {
its, errorItems, err = c.fetchAllFeeds()
if err != nil {
return fmt.Errorf("[commands.go] TUI: %w", err)
}
// if no items, fetchAllFeeds and GetAllFeeds
} else if len(its) == 0 {
_, errorItems, err = c.fetchAllFeeds()
if err != nil {
return fmt.Errorf("[commands.go] TUI: %w", err)
}
// refetch for consistent data across calls
its, err = c.GetAllFeeds()
if err != nil {
return fmt.Errorf("[commands.go] TUI: %w", err)
}
}

items := convertItems(its)

es := []string{}
for _, e := range errorItems {
es = append(es, fmt.Sprintf("Error fetching %s: %s", e.FeedURL, e.Err))
}

if err := Render(items, c, es, c.config); err != nil {
return fmt.Errorf("commands.TUI: %w", err)
}

return nil
}

func (c Commands) List(numResults int) error {
its, err := c.GetAllFeeds()
if err != nil {
Expand Down Expand Up @@ -310,75 +229,6 @@ func includes[T comparable](arr []T, item T) bool {
return false
}

func (c Commands) GetAllFeeds() ([]store.Item, error) {
err := c.CleanFeeds()
if err != nil {
return []store.Item{}, fmt.Errorf("[commands.go] GetAllFeeds: %w", err)
}

is, err := c.store.GetAllItems()
if err != nil {
return []store.Item{}, fmt.Errorf("commands.go: GetAllFeeds %w", err)
}

if c.config.ShowFavourites {
is = onlyFavourites(is)
} else if c.config.ShowRead {
is = showRead(is)
} else {
is = defaultView(is)
}

// add FeedName from config for custom names
for i := 0; i < len(is); i++ {
for _, f := range c.config.Feeds {
if f.URL == is[i].FeedURL {
is[i].FeedName = f.Name
}
}
}

return is, nil
}

func onlyFavourites(items []store.Item) (is []store.Item) {
for _, v := range items {
if v.Favourite {
is = append(is, v)
}
}

return is
}

// currently showRead shows all items
func showRead(items []store.Item) (is []store.Item) {
return items
}

func defaultView(items []store.Item) (is []store.Item) {
for _, v := range items {
if !v.Read() {
is = append(is, v)
}
}

return is
}

func fetchFeed(ch chan FetchResultError, wg *sync.WaitGroup, feed config.Feed, version string) {
defer wg.Done()

r, err := rss.Fetch(feed, version)

if err != nil {
ch <- FetchResultError{res: rss.RSS{}, err: err, url: feed.URL}
return
}

ch <- FetchResultError{res: r, err: nil, url: feed.URL}
}

func (c Commands) GetGlamourisedArticle(ID int) (string, error) {
article, err := c.store.GetItemByID(ID)
if err != nil {
Expand Down
110 changes: 110 additions & 0 deletions internal/commands/feeds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package commands

import (
"fmt"
"sync"

"github.com/guyfedwards/nom/v2/internal/config"
"github.com/guyfedwards/nom/v2/internal/rss"
"github.com/guyfedwards/nom/v2/internal/store"
)

func (c Commands) CleanFeeds() error {
urls, err := c.store.GetAllFeedURLs()
if err != nil {
return fmt.Errorf("[commands.go]: %w", err)
}

var urlsToRemove []string

for _, u := range urls {
inFeeds := false
for _, f := range c.config.Feeds {
if f.URL == u {
inFeeds = true
}
}

if !inFeeds {
urlsToRemove = append(urlsToRemove, u)
}
}

for _, url := range urlsToRemove {
err := c.store.DeleteByFeedURL(url, false)
if err != nil {
return fmt.Errorf("[commands.go]: %w", err)
}
}

return nil
}

func (c Commands) GetAllFeeds() ([]store.Item, error) {
err := c.CleanFeeds()
if err != nil {
return []store.Item{}, fmt.Errorf("[commands.go] GetAllFeeds: %w", err)
}

is, err := c.store.GetAllItems()
if err != nil {
return []store.Item{}, fmt.Errorf("commands.go: GetAllFeeds %w", err)
}

if c.config.ShowFavourites {
is = onlyFavourites(is)
} else if c.config.ShowRead {
is = showRead(is)
} else {
is = defaultView(is)
}

// add FeedName from config for custom names
for i := 0; i < len(is); i++ {
for _, f := range c.config.Feeds {
if f.URL == is[i].FeedURL {
is[i].FeedName = f.Name
}
}
}

return is, nil
}

func onlyFavourites(items []store.Item) (is []store.Item) {
for _, v := range items {
if v.Favourite {
is = append(is, v)
}
}

return is
}

// currently showRead shows all items
func showRead(items []store.Item) (is []store.Item) {
return items
}

func defaultView(items []store.Item) (is []store.Item) {
for _, v := range items {
if !v.Read() {
is = append(is, v)
}
}

return is
}

func fetchFeed(ch chan FetchResultError, wg *sync.WaitGroup, feed config.Feed, version string) {
defer wg.Done()

r, err := rss.Fetch(feed, version)

if err != nil {
ch <- FetchResultError{res: rss.RSS{}, err: err, url: feed.URL}
return
}

ch <- FetchResultError{res: r, err: nil, url: feed.URL}
}
Loading

0 comments on commit 8074b69

Please sign in to comment.