/* Copyright © 2021 NAME HERE */ package cmd import ( "fmt" "strconv" "github.com/spf13/cobra" "os" ) // deleteCmd represents the delete command var deleteCmd = &cobra.Command{ Use: "delete [Id]", Args: cobra.ExactArgs(1), Short: "Delete the given entry. Will ask for confirmation", Long: `We do not recommend using delete, unless something was truly added by mistake. Delete is a hard delete. There is no recovery.`, PreRunE: func(cmd *cobra.Command, args []string) error { id, err := strconv.Atoi(args[0]) if err != nil { return err } entry, err := App.Db.GetDiaryEntry(int64(id)) notes := App.Db.GetDiaryEntryNotes(int64(id)) if err != nil { return err } fmt.Println("Are you sure you want to delete the following entry? (y/n)") PrintEntry(entry) PrintNotes(notes, App.Cfg.UseMarkdownInOutput) var response string fmt.Scanln(&response) if response == "y" { return nil } else { fmt.Println("Will not delete entry " + args[0]) os.Exit(0) } return nil }, Run: func(cmd *cobra.Command, args []string) { id, err := strconv.Atoi(args[0]) if err != nil { panic(err) } _, err = App.Db.DeleteDiaryEntry(int64(id)) _, err = App.Db.DeleteNotesForDiaryEntry(int64(id)) if err != nil { panic(err) } }, } func init() { rootCmd.AddCommand(deleteCmd) }