From 672591bafe907220c055655a0af27e1d832fdf28 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Fri, 7 Mar 2025 10:19:30 -0800 Subject: [PATCH] Unified source command --- cmd/source.go | 46 ++++++++++++++++++++++++++++++++++++++++++--- cmd/sourceAdd.go | 39 -------------------------------------- cmd/sourceDelete.go | 39 -------------------------------------- 3 files changed, 43 insertions(+), 81 deletions(-) delete mode 100644 cmd/sourceAdd.go delete mode 100644 cmd/sourceDelete.go diff --git a/cmd/source.go b/cmd/source.go index 19c75cf..4825086 100644 --- a/cmd/source.go +++ b/cmd/source.go @@ -1,9 +1,11 @@ -/* -Copyright © 2025 NAME HERE -*/ 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) + } + } diff --git a/cmd/sourceAdd.go b/cmd/sourceAdd.go deleted file mode 100644 index 072d944..0000000 --- a/cmd/sourceAdd.go +++ /dev/null @@ -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) -} diff --git a/cmd/sourceDelete.go b/cmd/sourceDelete.go deleted file mode 100644 index 72d3a5d..0000000 --- a/cmd/sourceDelete.go +++ /dev/null @@ -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) -}