-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtask_delete.go
More file actions
57 lines (48 loc) · 1.1 KB
/
task_delete.go
File metadata and controls
57 lines (48 loc) · 1.1 KB
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 main
import (
"fmt"
"github.com/spf13/cobra"
"github.com/wltechblog/notes/internal/notes"
"github.com/wltechblog/notes/internal/tasks"
)
var taskDeleteCmd = &cobra.Command{
Use: "delete [id]",
Aliases: []string{"del", "rm"},
Short: "Delete a task",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if !taskMode {
return fmt.Errorf("this command is only available for tasks, use 'note delete' instead")
}
tm, err := tasks.NewTaskManager()
if err != nil {
return err
}
id := args[0]
task, err := tm.GetTask(id)
if err != nil {
fmt.Printf("Task not found: %s\n", id)
return nil
}
nm, err := notes.NewNoteManager()
if err != nil {
return err
}
if task.NoteID != "" {
if err := nm.DeleteNote(task.NoteID); err != nil {
fmt.Printf("Failed to delete note: %v\n", err)
}
}
if err := tm.DeleteTask(id); err != nil {
fmt.Printf("Failed to delete task: %v\n", err)
return nil
}
fmt.Printf("Task deleted: %s\n", id)
return nil
},
}
func init() {
if taskMode {
rootCmd.AddCommand(taskDeleteCmd)
}
}