73 lines
1.6 KiB
Go
73 lines
1.6 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
|
|
"github.com/Jaculabilis/intake/core"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var feedCmd = &cobra.Command{
|
|
Use: "feed",
|
|
Short: "Display the item feed",
|
|
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() {
|
|
formatter, err := core.FormatAs(feedFormat)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
db, err := core.OpenDb(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))
|
|
}
|
|
}
|