-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete.go
57 lines (47 loc) · 1.23 KB
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package cmd
import (
"fmt"
"github.com/lucasnevespereira/lembra/internal/pkg/storage"
"github.com/lucasnevespereira/lembra/internal/utils/logger"
"github.com/spf13/cobra"
)
var deleteCommand = &cobra.Command{
Use: "delete",
Short: "Delete a reminder",
Long: "Delete deletes an existing reminder with the given ID",
RunE: deleteReminder,
}
func init() {
rootCmd.AddCommand(deleteCommand)
deleteCommand.PersistentFlags().String("id", "", "Reminder ID")
deleteCommand.PersistentFlags().Bool("all", false, "Delete all reminders")
}
func deleteReminder(cmd *cobra.Command, args []string) error {
id, _ := cmd.Flags().GetString("id")
deleteAll, _ := cmd.Flags().GetBool("all")
dbFile, err := storage.OpenStorageFile()
if err != nil {
return err
}
reminderRepo := storage.NewReminderStorage(dbFile)
if deleteAll {
err := reminderRepo.DeleteAll()
if err != nil {
return err
}
logger.Log.Infoln("All reminders deleted successfully")
} else {
existingReminder, err := reminderRepo.GetByID(id)
if err != nil {
return err
}
if existingReminder.ID != "" {
err = reminderRepo.DeleteByID(existingReminder.ID)
if err != nil {
return err
}
fmt.Println("Reminder deleted successfully")
}
}
return nil
}