-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathedit_task.go
More file actions
122 lines (102 loc) · 2.92 KB
/
edit_task.go
File metadata and controls
122 lines (102 loc) · 2.92 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package tw
import (
"ccsync_backend/models"
"ccsync_backend/utils"
"encoding/json"
"fmt"
"os"
"strings"
)
func EditTaskInTaskwarrior(
uuid, taskUUID, email, encryptionSecret, description, project, start, entry, wait, end, due, recur string,
tags, depends []string,
annotations []models.Annotation,
) error {
tempDir, err := os.MkdirTemp("", utils.SafeTempDirPrefix("taskwarrior-", email))
if err != nil {
return fmt.Errorf("failed to create temporary directory: %v", err)
}
defer os.RemoveAll(tempDir)
origin := os.Getenv("CONTAINER_ORIGIN")
if err := SetTaskwarriorConfig(tempDir, encryptionSecret, origin, uuid); err != nil {
return err
}
if err := SyncTaskwarrior(tempDir); err != nil {
return err
}
modifyArgs := []string{taskUUID, "modify"}
if description != "" {
modifyArgs = append(modifyArgs, description)
}
if project != "" {
modifyArgs = append(modifyArgs, "project:"+project)
}
if wait != "" {
formattedWait := wait
if !strings.Contains(wait, "T") {
formattedWait = wait + "T00:00:00"
}
modifyArgs = append(modifyArgs, "wait:"+formattedWait)
}
if start != "" {
modifyArgs = append(modifyArgs, "start:"+start)
}
if entry != "" {
modifyArgs = append(modifyArgs, "entry:"+entry)
}
if end != "" {
modifyArgs = append(modifyArgs, "end:"+end)
}
dependsStr := strings.Join(depends, ",")
modifyArgs = append(modifyArgs, "depends:"+dependsStr)
if due != "" {
formattedDue := due
if !strings.Contains(due, "T") {
formattedDue = due + "T00:00:00"
}
modifyArgs = append(modifyArgs, "due:"+formattedDue)
}
if recur != "" {
modifyArgs = append(modifyArgs, "recur:"+recur)
}
for _, tag := range tags {
if strings.HasPrefix(tag, "+") {
modifyArgs = append(modifyArgs, tag)
} else if strings.HasPrefix(tag, "-") {
modifyArgs = append(modifyArgs, tag)
} else {
modifyArgs = append(modifyArgs, "+"+tag)
}
}
if err := utils.ExecCommand("task", modifyArgs...); err != nil {
return fmt.Errorf("failed to edit task: %v", err)
}
if len(annotations) > 0 {
output, err := utils.ExecCommandForOutputInDir(tempDir, "task", taskUUID, "export")
if err == nil {
var tasks []map[string]interface{}
if err := json.Unmarshal(output, &tasks); err == nil && len(tasks) > 0 {
if existingAnnotations, ok := tasks[0]["annotations"].([]interface{}); ok {
for _, ann := range existingAnnotations {
if annMap, ok := ann.(map[string]interface{}); ok {
if desc, ok := annMap["description"].(string); ok {
utils.ExecCommand("task", taskUUID, "denotate", desc)
}
}
}
}
}
}
for _, annotation := range annotations {
if annotation.Description != "" {
if err := utils.ExecCommand("task", taskUUID, "annotate", annotation.Description); err != nil {
return fmt.Errorf("failed to add annotation %s: %v", annotation.Description, err)
}
}
}
}
if err := SyncTaskwarrior(tempDir); err != nil {
return err
}
return nil
}