Skip to content
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 45 additions & 76 deletions backend/utils/tw/edit_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,20 @@ import (
"strings"
)

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 {
if err := utils.ExecCommand("rm", "-rf", "/root/.task"); err != nil {
Comment thread
Harshkamboj11 marked this conversation as resolved.
return fmt.Errorf("error deleting Taskwarrior data: %v", err)
}
func EditTaskInTaskwarrior(
Comment thread
Harshkamboj11 marked this conversation as resolved.
uuid, description, email, encryptionSecret, taskUUID string,
tags []string,
project string,
start string,
entry string,
wait string,
end string,
depends []string,
due string,
recur string,
annotations []models.Annotation,
) error {

tempDir, err := os.MkdirTemp("", "taskwarrior-"+email)
if err != nil {
return fmt.Errorf("failed to create temporary directory: %v", err)
Expand All @@ -28,107 +38,66 @@ func EditTaskInTaskwarrior(uuid, description, email, encryptionSecret, taskID st
return err
}

// Escape the double quotes in the description and format it
if err := utils.ExecCommand("task", taskID, "modify", description); err != nil {
return fmt.Errorf("failed to edit task: %v", err)
}
args := []string{"modify", taskUUID}

// Handle project
if project != "" {
if err := utils.ExecCommand("task", taskID, "modify", "project:"+project); err != nil {
return fmt.Errorf("failed to set project %s: %v", project, err)
}
if description != "" {
args = append(args, description)
}

// Handle wait date
if wait != "" {
Comment thread
Harshkamboj11 marked this conversation as resolved.
// Convert `2025-11-29` -> `2025-11-29T00:00:00`
formattedWait := wait + "T00:00:00"

if err := utils.ExecCommand("task", taskID, "modify", "wait:"+formattedWait); err != nil {
return fmt.Errorf("failed to set wait date %s: %v", formattedWait, err)
}
}

// Handle tags
if len(tags) > 0 {
for _, tag := range tags {
if strings.HasPrefix(tag, "+") {
// Add tag
tagValue := strings.TrimPrefix(tag, "+")
if err := utils.ExecCommand("task", taskID, "modify", "+"+tagValue); err != nil {
return fmt.Errorf("failed to add tag %s: %v", tagValue, err)
}
} else if strings.HasPrefix(tag, "-") {
// Remove tag
tagValue := strings.TrimPrefix(tag, "-")
if err := utils.ExecCommand("task", taskID, "modify", "-"+tagValue); err != nil {
return fmt.Errorf("failed to remove tag %s: %v", tagValue, err)
}
} else {
// Add tag without prefix
if err := utils.ExecCommand("task", taskID, "modify", "+"+tag); err != nil {
return fmt.Errorf("failed to add tag %s: %v", tag, err)
}
}
}
if project != "" {
args = append(args, "project:"+project)
}

// Handle start date
if start != "" {
if err := utils.ExecCommand("task", taskID, "modify", "start:"+start); err != nil {
return fmt.Errorf("failed to set start date %s: %v", start, err)
}
args = append(args, "start:"+start)
}

// Handle entry date
if entry != "" {
if err := utils.ExecCommand("task", taskID, "modify", "entry:"+entry); err != nil {
return fmt.Errorf("failed to set entry date %s: %v", entry, err)
}
args = append(args, "entry:"+entry)
}

if wait != "" {
args = append(args, "wait:"+wait)
}

// Handle end date
if end != "" {
if err := utils.ExecCommand("task", taskID, "modify", "end:"+end); err != nil {
return fmt.Errorf("failed to set end date %s: %v", end, err)
}
args = append(args, "end:"+end)
}

// Handle depends - always set to ensure clearing works
dependsStr := strings.Join(depends, ",")
if err := utils.ExecCommand("task", taskID, "modify", "depends:"+dependsStr); err != nil {
return fmt.Errorf("failed to set depends %s: %v", dependsStr, err)
if len(depends) > 0 {
args = append(args, "depends:"+strings.Join(depends, ","))
}

// Handle due date
if due != "" {
// Convert `2025-11-29` -> `2025-11-29T00:00:00`
formattedDue := due + "T00:00:00"

if err := utils.ExecCommand("task", taskID, "modify", "due:"+formattedDue); err != nil {
return fmt.Errorf("failed to set due date %s: %v", formattedDue, err)
}
args = append(args, "due:"+due)
}

// Handle recur - this will automatically set rtype field
if recur != "" {
if err := utils.ExecCommand("task", taskID, "modify", "recur:"+recur); err != nil {
return fmt.Errorf("failed to set recur %s: %v", recur, err)
args = append(args, "recur:"+recur)
}

for _, tag := range tags {
if strings.HasPrefix(tag, "+") || strings.HasPrefix(tag, "-") {
args = append(args, tag)
} else {
args = append(args, "+"+tag)
}
}

// Handle annotations
if err := utils.ExecCommand("task", args...); err != nil {
return fmt.Errorf("failed to edit task %s: %v", taskUUID, err)
}

if len(annotations) >= 0 {
Comment thread
Harshkamboj11 marked this conversation as resolved.
Outdated
output, err := utils.ExecCommandForOutputInDir(tempDir, "task", taskID, "export")
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", taskID, "denotate", desc)
utils.ExecCommand("task", taskUUID, "denotate", desc)
}
}
}
Expand All @@ -138,16 +107,16 @@ func EditTaskInTaskwarrior(uuid, description, email, encryptionSecret, taskID st

for _, annotation := range annotations {
if annotation.Description != "" {
if err := utils.ExecCommand("task", taskID, "annotate", annotation.Description); err != nil {
if err := utils.ExecCommand("task", taskUUID, "annotate", annotation.Description); err != nil {
return fmt.Errorf("failed to add annotation %s: %v", annotation.Description, err)
}
}
}
}

// Sync Taskwarrior again
if err := SyncTaskwarrior(tempDir); err != nil {
return err
}

return nil
}
Loading