This repository has been archived on 2025-10-30. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
gitwarrior/internal/taskwarrior/task.go
Yves Biener 255b35783c add(taskwarrior): update tasks in taskwarrior
The implementation updates the tasks in taskwarrior and/or creates new
tasks. WIP for #3.
2023-10-20 13:41:43 +02:00

70 lines
1.7 KiB
Go

package taskwarrior
import (
"bytes"
"encoding/json"
"os/exec"
)
/// A task with an Id of 0 is either `completed` or `deleted` which is also
/// mentioned in the Status field
type Task struct {
Id uint `json:"id,omitempty"`
Project string `json:"project,omitempty"`
Tags []string `json:"tags,omitempty"`
Description string `json:"description,omitempty"`
Annotations []Annotation `json:"annotations,omitempty"`
Status string `json:"status,omitempty"`
Due string `json:"due,omitempty"`
Entry string `json:"entry,omitempty"`
Modified string `json:"modified,omitempty"`
End string `json:"end,omitempty"`
Uuid string `json:"uuid,omitempty"`
Urgency float32 `json:"urgency,omitempty"`
}
type Annotation struct {
Description string `json:"description,omitempty"`
Entry string `json:"entry,omitempty"`
}
func NewTask(description string, project string, tags ...string) Task {
return Task{
Project: project,
Tags: tags,
Description: description,
}
}
func GetTasks(filter Filter) ([]Task, error) {
filter.filter = append(filter.filter, "export")
cmd := exec.Command("task", filter.filter...)
out, err := cmd.Output()
if err != nil {
return nil, err
}
var tasks []Task
decoder := json.NewDecoder(bytes.NewBuffer(out))
decoder.DisallowUnknownFields()
if err = decoder.Decode(&tasks); err != nil {
return nil, err
}
return tasks, nil
}
func UpdateTasks(tasks []Task) error {
cmd := exec.Command("task", "import")
value, err := json.Marshal(tasks)
if err != nil {
return err
}
var buffer bytes.Buffer
_, err = buffer.Write(value)
if err != nil {
return err
}
cmd.Stdin = &buffer
return cmd.Run()
}