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
|
package task
import (
"bufio"
"bytes"
"encoding/json"
"os/exec"
)
// Task represents a taskwarrior task as returned by `task export`.
type Annotation struct {
Entry string `json:"entry"`
Description string `json:"description"`
}
type Task struct {
ID int `json:"id"`
UUID string `json:"uuid"`
Description string `json:"description"`
Tags []string `json:"tags"`
Status string `json:"status"`
Start string `json:"start"`
Entry string `json:"entry"`
Due string `json:"due"`
Priority string `json:"priority"`
Recur string `json:"recur"`
Urgency float64 `json:"urgency"`
Annotations []Annotation `json:"annotations"`
}
// Add creates a new task with the given description and tags.
func Add(description string, tags []string) error {
args := []string{"add"}
for _, t := range tags {
if len(t) > 0 && t[0] != '+' {
t = "+" + t
}
args = append(args, t)
}
args = append(args, description)
cmd := exec.Command("task", args...)
return cmd.Run()
}
// Export retrieves all tasks using `task export rc.json.array=off` and parses
// the JSON output into a slice of Task structs.
func Export() ([]Task, error) {
cmd := exec.Command("task", "export", "rc.json.array=off")
out, err := cmd.Output()
if err != nil {
return nil, err
}
var tasks []Task
scanner := bufio.NewScanner(bytes.NewReader(out))
for scanner.Scan() {
line := scanner.Bytes()
line = bytes.TrimSpace(line)
if len(line) == 0 {
continue
}
var t Task
if err := json.Unmarshal(line, &t); err != nil {
return nil, err
}
tasks = append(tasks, t)
}
if err := scanner.Err(); err != nil {
return nil, err
}
return tasks, nil
}
|