87 lines
2.1 KiB
Go
87 lines
2.1 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: fmt.Sprintf(`Display the intake item feed in various formats.
|
|
The default format is "headlines".
|
|
|
|
%s`, makeFormatHelpText()),
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
feed(
|
|
stringArg(cmd, "format"),
|
|
stringArg(cmd, "source"),
|
|
stringArg(cmd, "channel"),
|
|
boolArg(cmd, "all"),
|
|
)
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(feedCmd)
|
|
|
|
feedCmd.Flags().StringP("format", "f", "headlines", "Feed format")
|
|
feedCmd.Flags().StringP("source", "s", "", "Limit to items from source")
|
|
feedCmd.Flags().StringP("channel", "c", "", "Limit to items from channel")
|
|
feedCmd.MarkFlagsMutuallyExclusive("source", "channel")
|
|
feedCmd.Flags().Bool("all", false, "Show inactive items")
|
|
}
|
|
|
|
func feed(
|
|
format string,
|
|
source string,
|
|
channel string,
|
|
showInactive bool,
|
|
) {
|
|
formatter := formatAs(format)
|
|
|
|
db := openAndMigrateDb()
|
|
|
|
var items []core.Item
|
|
var err error
|
|
if source != "" {
|
|
if exists, err := core.SourceExists(db, source); !exists || err != nil {
|
|
log.Fatalf("error: no such source %s", source)
|
|
}
|
|
|
|
if showInactive {
|
|
items, err = core.GetAllItemsForSource(db, source, 0, core.DefaultFeedLimit)
|
|
} else {
|
|
items, err = core.GetActiveItemsForSource(db, source, 0, core.DefaultFeedLimit)
|
|
}
|
|
if err != nil {
|
|
log.Fatalf("error: failed to fetch items from %s:, %v", source, err)
|
|
}
|
|
} else if channel != "" {
|
|
if showInactive {
|
|
items, err = core.GetAllItemsForChannel(db, channel, 0, core.DefaultFeedLimit)
|
|
} else {
|
|
items, err = core.GetActiveItemsForChannel(db, channel, 0, core.DefaultFeedLimit)
|
|
}
|
|
if err != nil {
|
|
log.Fatalf("error: failed to fetch items from %s:, %v", channel, err)
|
|
}
|
|
} else {
|
|
if showInactive {
|
|
items, err = core.GetAllItems(db, 0, core.DefaultFeedLimit)
|
|
} else {
|
|
items, err = core.GetAllActiveItems(db, 0, core.DefaultFeedLimit)
|
|
}
|
|
if err != nil {
|
|
log.Fatalf("error: failed to fetch items: %v", err)
|
|
}
|
|
}
|
|
|
|
for _, item := range items {
|
|
fmt.Println(formatter(item))
|
|
}
|
|
}
|