intake/cmd/feed.go

81 lines
1.8 KiB
Go

package cmd
import (
"database/sql"
"fmt"
"log"
"github.com/Jaculabilis/intake/core"
"github.com/spf13/cobra"
)
var feedCmd = &cobra.Command{
Use: "feed",
Short: "Print item feeds",
Long: `Display the intake item feed in various formats.
The default format is "headlines".
Available formats:
headlines Only item titles
json Full item JSON
short Item source and id
`,
Run: func(cmd *cobra.Command, args []string) {
feed()
},
}
var feedFormat string
var feedSource string
var feedChannel string
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")
}
func feed() {
var formatter func(core.Item) string
switch feedFormat {
case "headlines":
formatter = core.FormatAsHeadline
case "json":
formatter = core.FormatAsJson
case "short":
formatter = core.FormatAsShort
default:
log.Fatalf("error: invalid format %s", feedFormat)
}
db, err := sql.Open("sqlite3", getDbPath())
if err != nil {
log.Fatalf("error: failed to open %s", dbPath)
}
core.InitDatabase(db)
core.MigrateDatabase(db)
var items []core.Item
if feedSource != "" {
items, err = core.GetActiveItemsForSource(db, feedSource)
if err != nil {
log.Fatalf("error: failed to fetch items from %s", feedSource)
}
} else if feedChannel != "" {
log.Fatal("error: unimplemented")
} else {
items, err = core.GetAllActiveItems(db)
if err != nil {
log.Fatal("error: failed to fetch items")
}
}
for _, item := range items {
fmt.Println(formatter(item))
}
}