Unified source command

This commit is contained in:
Tim Van Baak 2025-03-07 10:19:30 -08:00
parent 92ce484dea
commit 672591bafe
3 changed files with 43 additions and 81 deletions

View File

@ -1,9 +1,11 @@
/*
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"log"
"os"
"github.com/Jaculabilis/intake/core"
"github.com/spf13/cobra"
)
@ -17,8 +19,46 @@ 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"))
},
}
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)
}
}

View File

@ -1,39 +0,0 @@
package cmd
import (
"log"
"github.com/Jaculabilis/intake/core"
"github.com/spf13/cobra"
)
var sourceAddCmd = &cobra.Command{
Use: "add",
Short: "Create a source",
Long: `Create a source.
`,
Run: func(cmd *cobra.Command, args []string) {
sourceAdd(stringArg(cmd, "source"))
},
}
func init() {
sourceCmd.AddCommand(sourceAddCmd)
sourceAddCmd.Flags().StringP("source", "s", "", "Source name")
sourceAddCmd.MarkFlagRequired("source")
}
func sourceAdd(source string) {
if source == "" {
log.Fatal("error: --source is empty")
}
db := openAndMigrateDb()
if err := core.AddSource(db, source); err != nil {
log.Fatalf("error: failed to add source: %v", err)
}
log.Printf("Added source %s", source)
}

View File

@ -1,39 +0,0 @@
package cmd
import (
"log"
"github.com/Jaculabilis/intake/core"
"github.com/spf13/cobra"
)
var sourceDeleteCmd = &cobra.Command{
Use: "delete",
Aliases: []string{"rm"},
Short: "Delete a source",
Long: `Delete a source.
`,
Run: func(cmd *cobra.Command, args []string) {
sourceDelete(stringArg(cmd, "source"))
},
}
func init() {
sourceCmd.AddCommand(sourceDeleteCmd)
sourceDeleteCmd.Flags().StringP("source", "s", "", "Source to delete")
}
func sourceDelete(source string) {
if source == "" {
log.Fatal("error: --source is empty")
}
db := openAndMigrateDb()
if err := core.DeleteSource(db, source); err != nil {
log.Fatalf("error: failed to delete source: %v", err)
}
log.Printf("Deleted source %s", source)
}