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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
package askcli
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"strings"
)
func (d Dispatcher) handleInfo(ctx context.Context, args []string, stdout, stderr io.Writer) (int, error) {
tasks, code, err := d.infoTasks(ctx, args, stderr)
if err != nil {
writeInfoError(stderr, err)
return code, nil
}
if d.jsonOutput {
data, err := json.Marshal(tasks)
if err != nil {
fmt.Fprintf(stderr, "error: failed to marshal JSON: %v\n", err)
return 1, nil
}
stdout.Write(data)
io.WriteString(stdout, "\n")
} else {
allUUIDs := append([]string{tasks[0].UUID}, tasks[0].Depends...)
aliases, err := ensureTaskAliasesForUUIDs(allUUIDs)
if err != nil {
fmt.Fprintf(stderr, "error: failed to load task aliases: %v\n", err)
return 1, nil
}
io.WriteString(stdout, FormatTaskInfo(tasks[0], displayTaskAlias(tasks[0].UUID, aliases), aliases))
}
return 0, nil
}
func (d Dispatcher) infoTasks(ctx context.Context, args []string, stderr io.Writer) ([]TaskExport, int, error) {
if len(args) >= 2 {
_, tasks, code, err := d.resolveTaskSelector(ctx, args[1], stderr)
return tasks, code, err
}
return d.startedInfoTasks(ctx, stderr)
}
func (d Dispatcher) startedInfoTasks(ctx context.Context, stderr io.Writer) ([]TaskExport, int, error) {
tasks, code, err := d.exportTasks(ctx, []string{"started", "export"}, stderr)
if err != nil {
return nil, code, err
}
switch len(tasks) {
case 0:
return nil, 1, fmt.Errorf("no started task found")
case 1:
return tasks, 0, nil
default:
return nil, 1, fmt.Errorf("multiple started tasks found; pass a UUID explicitly")
}
}
func (d Dispatcher) exportTasks(ctx context.Context, args []string, stderr io.Writer) ([]TaskExport, int, error) {
var outBuf bytes.Buffer
code, err := d.runner.Run(ctx, args, nil, &outBuf, stderr)
if code != 0 {
return nil, code, err
}
tasks, err := ParseTaskExport(&outBuf)
if err != nil {
return nil, 1, err
}
if len(tasks) == 0 && len(args) > 0 && strings.HasPrefix(args[0], "uuid:") {
return nil, 1, fmt.Errorf("task not found")
}
return tasks, 0, nil
}
func writeInfoError(stderr io.Writer, err error) {
msg := err.Error()
if strings.HasPrefix(msg, "error:") {
io.WriteString(stderr, msg+"\n")
return
}
fmt.Fprintf(stderr, "error: %v\n", err)
}
func (d Dispatcher) handleAdd(ctx context.Context, args []string, stdout, stderr io.Writer) (int, error) {
if len(args) < 2 {
io.WriteString(stderr, "error: ask add requires a description\n")
return 1, nil
}
modifiers, description := parseAddArgs(args[1:])
var outBuf bytes.Buffer
// rc.verbose=nothing keeps Taskwarrior quiet by default. rc.verbose=new-uuid
// then re-enables the UUID-only confirmation we parse below.
taskArgs := []string{"add", "rc.verbose=nothing", "rc.verbose=new-uuid"}
taskArgs = append(taskArgs, modifiers...)
taskArgs = append(taskArgs, description)
code, err := d.runner.Run(ctx, taskArgs, nil, &outBuf, stderr)
if code != 0 {
return code, err
}
uuid := extractUUIDFromAddOutput(outBuf.String())
if uuid == "" {
io.WriteString(stderr, "error: could not parse UUID from task creation output\n")
return 1, nil
}
aliases, err := ensureTaskAliasesForUUIDs([]string{uuid})
if err != nil {
fmt.Fprintf(stderr, "error: failed to assign task alias: %v\n", err)
return 1, nil
}
io.WriteString(stdout, displayTaskAlias(uuid, aliases)+"\n")
return 0, nil
}
// extractUUIDFromAddOutput parses the UUID from taskwarrior's
// "Created task <uuid>." output (produced when rc.verbose=new-uuid is set).
func extractUUIDFromAddOutput(output string) string {
for _, line := range strings.Split(strings.TrimSpace(output), "\n") {
if strings.HasPrefix(line, "Created task ") {
parts := strings.Fields(line)
if len(parts) >= 3 {
return strings.TrimSuffix(parts[2], ".")
}
}
}
return ""
}
// parseAddArgs splits args into taskwarrior modifier tokens and the description.
// Modifier tokens are args that start with "priority:", "+", or "-" AND contain
// no spaces (tags and priority flags cannot have spaces). The first arg that is
// not a modifier begins the description; all remaining args are joined with spaces.
// If all args are modifiers the description is empty.
func parseAddArgs(args []string) (modifiers []string, description string) {
for i, arg := range args {
isModifier := !strings.Contains(arg, " ") &&
(strings.HasPrefix(arg, "priority:") || strings.HasPrefix(arg, "+") || strings.HasPrefix(arg, "-"))
if isModifier {
modifiers = append(modifiers, arg)
} else {
description = strings.Join(args[i:], " ")
return
}
}
// All args were modifiers; no description provided.
return
}
|