@@ -9,10 +9,11 @@ import (
99 "strings"
1010)
1111
12- func EditTaskInTaskwarrior (uuid , description , email , encryptionSecret , taskID string , tags []string , project string , start string , entry string , wait string , end string , depends []string , due string , recur string , annotations []models.Annotation ) error {
13- if err := utils .ExecCommand ("rm" , "-rf" , "/root/.task" ); err != nil {
14- return fmt .Errorf ("error deleting Taskwarrior data: %v" , err )
15- }
12+ func EditTaskInTaskwarrior (
13+ uuid , taskUUID , email , encryptionSecret , description , project , start , entry , wait , end , due , recur string ,
14+ tags , depends []string ,
15+ annotations []models.Annotation ,
16+ ) error {
1617 tempDir , err := os .MkdirTemp ("" , utils .SafeTempDirPrefix ("taskwarrior-" , email ))
1718 if err != nil {
1819 return fmt .Errorf ("failed to create temporary directory: %v" , err )
@@ -28,112 +29,69 @@ func EditTaskInTaskwarrior(uuid, description, email, encryptionSecret, taskID st
2829 return err
2930 }
3031
31- // Escape the double quotes in the description and format it
32- if err := utils .ExecCommand ("task" , taskID , "modify" , description ); err != nil {
33- return fmt .Errorf ("failed to edit task: %v" , err )
32+ modifyArgs := []string {taskUUID , "modify" }
33+
34+ if description != "" {
35+ modifyArgs = append (modifyArgs , description )
3436 }
3537
36- // Handle project
3738 if project != "" {
38- if err := utils .ExecCommand ("task" , taskID , "modify" , "project:" + project ); err != nil {
39- return fmt .Errorf ("failed to set project %s: %v" , project , err )
40- }
39+ modifyArgs = append (modifyArgs , "project:" + project )
4140 }
4241
43- // Handle wait date
4442 if wait != "" {
45- // Convert `2025-11-29` -> `2025-11-29T00:00:00`
4643 formattedWait := wait + "T00:00:00"
47-
48- if err := utils .ExecCommand ("task" , taskID , "modify" , "wait:" + formattedWait ); err != nil {
49- return fmt .Errorf ("failed to set wait date %s: %v" , formattedWait , err )
50- }
51- }
52-
53- // Handle tags
54- if len (tags ) > 0 {
55- for _ , tag := range tags {
56- if strings .HasPrefix (tag , "+" ) {
57- // Add tag
58- tagValue := strings .TrimPrefix (tag , "+" )
59- if err := utils .ExecCommand ("task" , taskID , "modify" , "+" + tagValue ); err != nil {
60- return fmt .Errorf ("failed to add tag %s: %v" , tagValue , err )
61- }
62- } else if strings .HasPrefix (tag , "-" ) {
63- // Remove tag
64- tagValue := strings .TrimPrefix (tag , "-" )
65- if err := utils .ExecCommand ("task" , taskID , "modify" , "-" + tagValue ); err != nil {
66- return fmt .Errorf ("failed to remove tag %s: %v" , tagValue , err )
67- }
68- } else {
69- // Add tag without prefix
70- if err := utils .ExecCommand ("task" , taskID , "modify" , "+" + tag ); err != nil {
71- return fmt .Errorf ("failed to add tag %s: %v" , tag , err )
72- }
73- }
74- }
44+ modifyArgs = append (modifyArgs , "wait:" + formattedWait )
7545 }
7646
77- // Handle start date
7847 if start != "" {
79- if err := utils .ExecCommand ("task" , taskID , "modify" , "start:" + start ); err != nil {
80- return fmt .Errorf ("failed to set start date %s: %v" , start , err )
81- }
48+ modifyArgs = append (modifyArgs , "start:" + start )
8249 }
8350
84- // Handle entry date
8551 if entry != "" {
86- if err := utils .ExecCommand ("task" , taskID , "modify" , "entry:" + entry ); err != nil {
87- return fmt .Errorf ("failed to set entry date %s: %v" , entry , err )
88- }
52+ modifyArgs = append (modifyArgs , "entry:" + entry )
8953 }
9054
91- // Handle end date
9255 if end != "" {
93- if err := utils .ExecCommand ("task" , taskID , "modify" , "end:" + end ); err != nil {
94- return fmt .Errorf ("failed to set end date %s: %v" , end , err )
95- }
56+ modifyArgs = append (modifyArgs , "end:" + end )
9657 }
9758
98- // Handle depends - always set to ensure clearing works
99- if err := utils .ExecCommand ("task" , taskID , "modify" , "depends:" ); err != nil {
100- return fmt .Errorf ("failed to clear dependencies: %v" , err )
101- }
102- if len (depends ) > 0 {
103- dependsStr := strings .Join (depends , "," )
104- if err := utils .ExecCommand ("task" , taskID , "modify" , "depends:" + dependsStr ); err != nil {
105- return fmt .Errorf ("failed to set dependencies %s: %v" , dependsStr , err )
106- }
107- }
59+ dependsStr := strings .Join (depends , "," )
60+ modifyArgs = append (modifyArgs , "depends:" + dependsStr )
10861
109- // Handle due date
11062 if due != "" {
111- // Convert `2025-11-29` -> `2025-11-29T00:00:00`
11263 formattedDue := due + "T00:00:00"
113-
114- if err := utils .ExecCommand ("task" , taskID , "modify" , "due:" + formattedDue ); err != nil {
115- return fmt .Errorf ("failed to set due date %s: %v" , formattedDue , err )
116- }
64+ modifyArgs = append (modifyArgs , "due:" + formattedDue )
11765 }
11866
119- // Handle recur - this will automatically set rtype field
12067 if recur != "" {
121- if err := utils .ExecCommand ("task" , taskID , "modify" , "recur:" + recur ); err != nil {
122- return fmt .Errorf ("failed to set recur %s: %v" , recur , err )
68+ modifyArgs = append (modifyArgs , "recur:" + recur )
69+ }
70+
71+ for _ , tag := range tags {
72+ if strings .HasPrefix (tag , "+" ) {
73+ modifyArgs = append (modifyArgs , tag )
74+ } else if strings .HasPrefix (tag , "-" ) {
75+ modifyArgs = append (modifyArgs , tag )
76+ } else {
77+ modifyArgs = append (modifyArgs , "+" + tag )
12378 }
12479 }
12580
126- // Handle annotations
81+ if err := utils .ExecCommand ("task" , modifyArgs ... ); err != nil {
82+ return fmt .Errorf ("failed to edit task: %v" , err )
83+ }
84+
12785 if len (annotations ) >= 0 {
128- output , err := utils .ExecCommandForOutputInDir (tempDir , "task" , taskID , "export" )
86+ output , err := utils .ExecCommandForOutputInDir (tempDir , "task" , taskUUID , "export" )
12987 if err == nil {
13088 var tasks []map [string ]interface {}
13189 if err := json .Unmarshal (output , & tasks ); err == nil && len (tasks ) > 0 {
13290 if existingAnnotations , ok := tasks [0 ]["annotations" ].([]interface {}); ok {
13391 for _ , ann := range existingAnnotations {
13492 if annMap , ok := ann .(map [string ]interface {}); ok {
13593 if desc , ok := annMap ["description" ].(string ); ok {
136- utils .ExecCommand ("task" , taskID , "denotate" , desc )
94+ utils .ExecCommand ("task" , taskUUID , "denotate" , desc )
13795 }
13896 }
13997 }
@@ -143,16 +101,16 @@ func EditTaskInTaskwarrior(uuid, description, email, encryptionSecret, taskID st
143101
144102 for _ , annotation := range annotations {
145103 if annotation .Description != "" {
146- if err := utils .ExecCommand ("task" , taskID , "annotate" , annotation .Description ); err != nil {
104+ if err := utils .ExecCommand ("task" , taskUUID , "annotate" , annotation .Description ); err != nil {
147105 return fmt .Errorf ("failed to add annotation %s: %v" , annotation .Description , err )
148106 }
149107 }
150108 }
151109 }
152110
153- // Sync Taskwarrior again
154111 if err := SyncTaskwarrior (tempDir ); err != nil {
155112 return err
156113 }
114+
157115 return nil
158116}
0 commit comments