intake/cmd/deactivate.go

69 lines
1.7 KiB
Go

package cmd
import (
"fmt"
"log"
"os"
"github.com/Jaculabilis/intake/core"
_ "github.com/mattn/go-sqlite3"
"github.com/spf13/cobra"
)
var deactivateCmd = &cobra.Command{
Use: "deactivate --source source [--item item]",
Aliases: []string{"deac"},
Short: "Deactivate items",
Long: `Deactivate items, hiding them from feeds and marking them for deletion.
Deactivation is idempotent.`,
Run: func(cmd *cobra.Command, args []string) {
deactivate(stringArg(cmd, "source"), stringArg(cmd, "item"), boolArg(cmd, "yes"))
},
DisableFlagsInUseLine: true,
}
func init() {
rootCmd.AddCommand(deactivateCmd)
deactivateCmd.Flags().StringP("source", "s", "", "Source of the item")
deactivateCmd.MarkFlagRequired("source")
deactivateCmd.Flags().StringP("item", "i", "", "Item id")
deactivateCmd.Flags().BoolP("yes", "y", false, "Do not ask for confirmation")
}
func deactivate(source string, item string, yes bool) {
if source == "" {
log.Fatal("error: --source is empty")
}
db := openAndMigrateDb()
exists, err := core.SourceExists(db, source)
if err != nil {
log.Fatalf("error: failed to check for source: %v", err)
} else if !exists {
log.Fatalf("error: no such source %s", source)
}
if item == "" {
if !yes {
var response string
fmt.Printf("Are you sure you want to deactivate all items in source %s? (y/N): ", source)
_, err := fmt.Scanln(&response)
if err != nil || (response != "y" && response != "Y") {
os.Exit(1)
}
}
log.Fatal("source deactivation is not implemented")
} else {
active, err := core.DeactivateItem(db, source, item)
if err != nil {
log.Fatalf("error: failed to deactivate item: %s", err)
}
if active {
fmt.Printf("Deactivated %s/%s\n", source, item)
}
}
}