intake/cmd/source.go

66 lines
1.7 KiB
Go

package cmd
import (
"fmt"
"log"
"os"
"github.com/Jaculabilis/intake/core"
"github.com/spf13/cobra"
)
var sourceCmd = &cobra.Command{
Use: "source {--add name | --delete name [--yes]}",
GroupID: sourceGroup.ID,
Short: "Manage sources",
Long: `Manage sources.
A source represents a single content feed that generates discrete feed items.
The command defined in the "fetch" action is used to check for new items to
update the feed.
`,
Run: func(cmd *cobra.Command, args []string) {
source(stringArg(cmd, "add"), stringArg(cmd, "delete"), boolArg(cmd, "yes"))
},
DisableFlagsInUseLine: true,
}
func init() {
rootCmd.AddCommand(sourceCmd)
sourceCmd.Flags().String("add", "", "Add source")
sourceCmd.Flags().String("delete", "", "Delete source")
sourceCmd.MarkFlagsMutuallyExclusive("add", "delete")
sourceCmd.MarkFlagsOneRequired("add", "delete")
sourceCmd.Flags().BoolP("yes", "y", false, "Do not ask for confirmation")
}
func source(add, delete string, yes bool) {
if add == "" && delete == "" {
log.Fatal("error: either --add or --delete is required")
}
db := openAndMigrateDb()
if add != "" {
if err := core.AddSource(db, add); err != nil {
log.Fatalf("error: failed to add source: %v", err)
}
log.Printf("Added source %s", add)
} else if delete != "" {
if !yes {
var response string
fmt.Printf("Are you sure you want to delete source %s? (y/N): ", delete)
_, err := fmt.Scanln(&response)
if err != nil || (response != "y" && response != "Y") {
os.Exit(1)
}
}
if err := core.DeleteSource(db, delete); err != nil {
log.Fatalf("error: failed to delete source: %v", err)
}
log.Printf("Deleted source %s", delete)
}
}