implements list command

This commit is contained in:
Dennis Schoepf 2025-08-28 16:57:06 +02:00
parent b54f62378b
commit 09f4b2a838
5 changed files with 127 additions and 5 deletions

View file

@ -14,7 +14,7 @@ go install ...
### Add a RSS feed or youtube channel ### Add a RSS feed or youtube channel
Currently RSS feeds, Atom feeds and Youtube channel links are supported. Currently only RSS and Atom feeds and are supported.
```sh ```sh
freed add "https://feed.url" freed add "https://feed.url"

View file

@ -31,8 +31,9 @@ var addCmd = &cobra.Command{
Long: `Validates and stores a feed in the application's database. Depending on the feed type, articles, videos, or updates are fetched right away. Long: `Validates and stores a feed in the application's database. Depending on the feed type, articles, videos, or updates are fetched right away.
Supported types currently are: Supported types currently are:
- RSS - RSS feeds
- Youtube Channel links`, - Atom feeds
`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
feedName, articlesCount, err := internal.AddFeed(args[0]) feedName, articlesCount, err := internal.AddFeed(args[0])

58
cmd/list.go Normal file
View file

@ -0,0 +1,58 @@
/*
Copyright © 2025 Dennis Schoepf <dev@dnsc.io>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cmd
import (
"freed/internal"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
// listCmd represents the list command
var listCmd = &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "Lists all currently stored feeds",
Long: `This command lists all currently stored feeds with some metadata and their IDs. In the future it is possible to manipulate these feeds by referencing their ID.`,
Example: `freed list
freed ls`,
Run: func(cmd *cobra.Command, args []string) {
tableData, err := internal.GetAllFeedsAsTable()
if err != nil {
pterm.Error.Printf("Error listing feeds: %v\n", err)
return
}
pterm.DefaultTable.WithHasHeader().WithBoxed().WithData(tableData).Render()
},
}
func init() {
rootCmd.AddCommand(listCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// listCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// listCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

View file

@ -1,16 +1,24 @@
package database package database
import "time" import (
"time"
)
type FeedType string type FeedType string
type Feed struct { type Feed struct {
ID int64
Name string Name string
Url string Url string
AddedAt *time.Time CreatedAt *time.Time
LastSyncedAt *time.Time LastSyncedAt *time.Time
} }
type FeedWithArticleCount struct {
Feed
ArticleCount int
}
func (f Feed) Insert() (int64, error) { func (f Feed) Insert() (int64, error) {
result, err := db.Exec("INSERT INTO feed (name, url) VALUES (?,?)", f.Name, f.Url) result, err := db.Exec("INSERT INTO feed (name, url) VALUES (?,?)", f.Name, f.Url)
@ -26,3 +34,28 @@ func (f Feed) Insert() (int64, error) {
return id, nil return id, nil
} }
func FindAllFeedsWithArticleCount() (*[]FeedWithArticleCount, error) {
rows, err := db.Query("SELECT f.id, f.name, f.url, f.createdAt, f.lastSyncedAt, COUNT(a.id) FROM feed as f LEFT JOIN article as a ON a.feedId = f.id;")
if err != nil {
return nil, err
}
defer rows.Close()
var feeds []FeedWithArticleCount
for rows.Next() {
var feed FeedWithArticleCount
err := rows.Scan(&feed.ID, &feed.Name, &feed.Url, &feed.CreatedAt, &feed.LastSyncedAt, &feed.ArticleCount)
if err != nil {
return nil, err
}
feeds = append(feeds, feed)
}
if err = rows.Err(); err != nil {
return nil, err
}
return &feeds, nil
}

View file

@ -4,8 +4,10 @@ import (
"fmt" "fmt"
"freed/internal/database" "freed/internal/database"
"net/url" "net/url"
"strconv"
"github.com/mmcdole/gofeed" "github.com/mmcdole/gofeed"
"github.com/pterm/pterm"
) )
func AddFeed(feedUrl string) (string, int, error) { func AddFeed(feedUrl string) (string, int, error) {
@ -50,6 +52,34 @@ func AddFeed(feedUrl string) (string, int, error) {
return feed.Title, len(articles), nil return feed.Title, len(articles), nil
} }
func GetAllFeedsAsTable() (pterm.TableData, error) {
feeds, err := database.FindAllFeedsWithArticleCount()
if err != nil {
return nil, err
}
tableData := pterm.TableData{}
headerRow := []string{"ID", "Name", "Url", "Item Count", "Created At", "Last Synced At"}
tableData = append(tableData, headerRow)
for _, feed := range *feeds {
rowData := []string{
fmt.Sprintf("[%d]", feed.ID),
feed.Name,
feed.Url,
strconv.Itoa(feed.ArticleCount),
pterm.Gray(feed.CreatedAt),
pterm.Gray(feed.LastSyncedAt),
}
tableData = append(tableData, rowData)
}
return tableData, nil
}
func parseByUrl(u string) (*gofeed.Feed, error) { func parseByUrl(u string) (*gofeed.Feed, error) {
fp := gofeed.NewParser() fp := gofeed.NewParser()