intake/cmd/feed.go

71 lines
1.6 KiB
Go
Raw Normal View History

2025-01-17 15:05:57 +00:00
package cmd
import (
"fmt"
"log"
"github.com/Jaculabilis/intake/core"
"github.com/spf13/cobra"
)
var feedCmd = &cobra.Command{
Use: "feed",
2025-01-21 16:42:59 +00:00
Short: "Display the item feed",
2025-01-23 21:22:38 +00:00
Long: fmt.Sprintf(`Display the intake item feed in various formats.
2025-01-17 15:05:57 +00:00
The default format is "headlines".
2025-01-23 21:22:38 +00:00
%s`, makeFormatHelpText()),
2025-01-17 15:05:57 +00:00
Run: func(cmd *cobra.Command, args []string) {
feed()
},
}
var feedFormat string
var feedSource string
var feedChannel string
var feedShowInactive bool
2025-01-17 15:05:57 +00:00
func init() {
rootCmd.AddCommand(feedCmd)
feedCmd.Flags().StringVarP(&feedFormat, "format", "f", "headlines", "Feed format")
feedCmd.Flags().StringVarP(&feedSource, "source", "s", "", "Limit to items from source")
feedCmd.Flags().StringVarP(&feedChannel, "channel", "c", "", "Limit to items from channel")
feedCmd.MarkFlagsMutuallyExclusive("source", "channel")
feedCmd.Flags().BoolVar(&feedShowInactive, "all", false, "Show inactive items")
2025-01-17 15:05:57 +00:00
}
func feed() {
2025-01-23 21:22:38 +00:00
formatter := formatAs(feedFormat)
2025-01-17 15:05:57 +00:00
2025-01-23 16:36:25 +00:00
db := openAndMigrateDb()
2025-01-17 15:05:57 +00:00
var items []core.Item
2025-01-23 21:22:38 +00:00
var err error
2025-01-17 15:05:57 +00:00
if feedSource != "" {
if feedShowInactive {
items, err = core.GetAllItemsForSource(db, feedSource)
} else {
items, err = core.GetActiveItemsForSource(db, feedSource)
}
2025-01-17 15:05:57 +00:00
if err != nil {
log.Fatalf("error: failed to fetch items from %s", feedSource)
}
} else if feedChannel != "" {
log.Fatal("error: unimplemented")
} else {
if feedShowInactive {
items, err = core.GetAllItems(db)
} else {
items, err = core.GetAllActiveItems(db)
}
2025-01-17 15:05:57 +00:00
if err != nil {
log.Fatal("error: failed to fetch items")
}
}
for _, item := range items {
fmt.Println(formatter(item))
2025-01-17 15:05:57 +00:00
}
}