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
|
2025-01-23 19:38:17 +00:00
|
|
|
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")
|
2025-01-23 19:38:17 +00:00
|
|
|
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 != "" {
|
2025-01-23 19:38:17 +00:00
|
|
|
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 {
|
2025-01-29 17:13:48 +00:00
|
|
|
log.Fatalf("error: failed to fetch items from %s:, %v", feedSource, err)
|
2025-01-17 15:05:57 +00:00
|
|
|
}
|
|
|
|
} else if feedChannel != "" {
|
|
|
|
log.Fatal("error: unimplemented")
|
|
|
|
} else {
|
2025-01-23 19:38:17 +00:00
|
|
|
if feedShowInactive {
|
|
|
|
items, err = core.GetAllItems(db)
|
|
|
|
} else {
|
|
|
|
items, err = core.GetAllActiveItems(db)
|
|
|
|
}
|
2025-01-17 15:05:57 +00:00
|
|
|
if err != nil {
|
2025-01-29 17:13:48 +00:00
|
|
|
log.Fatalf("error: failed to fetch items: %v", err)
|
2025-01-17 15:05:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range items {
|
2025-01-17 15:30:01 +00:00
|
|
|
fmt.Println(formatter(item))
|
2025-01-17 15:05:57 +00:00
|
|
|
}
|
|
|
|
}
|