forked from naggie/dstask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
651 lines (556 loc) · 15.2 KB
/
commands.go
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
package dstask
import (
"errors"
"fmt"
"os"
"strconv"
"time"
yaml "gopkg.in/yaml.v2"
"mvdan.cc/xurls/v2"
)
// CommandAdd adds a new task to the task database.
func CommandAdd(conf Config, ctx, query Query) error {
if query.Text == "" && query.Template == 0 {
return errors.New("Task description or template required")
}
ts, err := LoadTaskSet(conf.Repo, conf.IDsFile, false)
if err != nil {
return err
}
if query.Template > 0 {
var taskSummary string
tt := ts.MustGetByID(query.Template)
query = query.Merge(ctx)
if query.Text != "" {
taskSummary = query.Text
} else {
taskSummary = tt.Summary
}
// create task from template task tt
task := Task{
WritePending: true,
Status: STATUS_PENDING,
Summary: taskSummary,
Tags: tt.Tags,
Project: tt.Project,
Priority: tt.Priority,
Notes: tt.Notes,
}
// Modify the task with any tags/projects/antiProjects/priorities in query
task.Modify(query)
task = ts.MustLoadTask(task)
ts.SavePendingChanges()
MustGitCommit(conf.Repo, "Added %s", task)
if tt.Status != STATUS_TEMPLATE {
// Insert Text Statement to inform user of real Templates
fmt.Print("\nYou've copied an open task!\nTo learn more about creating templates enter 'dstask help template'\n\n")
}
} else if query.Text != "" {
ctx.PrintContextDescription()
query = query.Merge(ctx)
task := Task{
WritePending: true,
Status: STATUS_PENDING,
Summary: query.Text,
Tags: query.Tags,
Project: query.Project,
Priority: query.Priority,
Notes: query.Note,
}
task = ts.MustLoadTask(task)
ts.SavePendingChanges()
MustGitCommit(conf.Repo, "Added %s", task)
}
return nil
}
// CommandContext sets a global context for dstask.
func CommandContext(conf Config, state State, ctx, query Query) error {
if len(os.Args) < 3 {
fmt.Println(ctx)
} else if os.Args[2] == "none" {
if err := state.SetContext(Query{}); err != nil {
return err
}
} else {
if err := state.SetContext(query); err != nil {
return err
}
}
state.Save(conf.StateFile)
return nil
}
// CommandDone marks a task as done.
func CommandDone(conf Config, ctx, query Query) error {
if query.HasOperators() {
return errors.New("operators not valid in this context")
}
if len(query.IDs) == 0 {
return errors.New("no ID(s) specified")
}
ts, err := LoadTaskSet(conf.Repo, conf.IDsFile, false)
if err != nil {
return err
}
// iterate over IDs instead of filtering; it's clearer and enables us to
// test each ID exists, and ignore context/operators
for _, id := range query.IDs {
task := ts.MustGetByID(id)
task.Status = STATUS_RESOLVED
task.Resolved = time.Now()
if query.Text != "" {
task.Notes += "\n" + query.Text
}
ts.MustUpdateTask(task)
ts.SavePendingChanges()
MustGitCommit(conf.Repo, "Resolved %s", task)
}
return nil
}
// CommandEdit edits a task's metadata, such as status, projects, tags, etc.
func CommandEdit(conf Config, ctx, query Query) error {
if query.HasOperators() {
return errors.New("operators not valid in this context")
}
if len(query.IDs) == 0 {
return errors.New("no ID(s) specified")
}
ts, err := LoadTaskSet(conf.Repo, conf.IDsFile, false)
if err != nil {
return err
}
for _, id := range query.IDs {
task := ts.MustGetByID(id)
data, err := yaml.Marshal(&task)
if err != nil {
// TODO present error to user, specific error message is important
return fmt.Errorf("failed to marshal task %s", task)
}
for {
edited := MustEditBytes(data, MakeTempFilename(task.ID, task.Summary, "yml"))
err = yaml.Unmarshal(edited, &task)
if err == nil {
break
} else {
// edit is a special case that won't be used as part of an API,
// so it's OK to exit
ConfirmOrAbort("Failed to unmarshal %s\nTry again?", err)
}
}
ts.MustUpdateTask(task)
ts.SavePendingChanges()
MustGitCommit(conf.Repo, "Edited %s", task)
}
return nil
}
// CommandHelp prints for a specific command or all commands.
func CommandHelp(args []string) {
if len(os.Args) > 2 {
Help(os.Args[2])
} else {
Help("")
}
}
// CommandLog logs a completed task immediately. Useful for tracking tasks after
// they're already completed.
func CommandLog(conf Config, ctx, query Query) error {
if query.Text == "" {
return errors.New("Task description required")
}
ts, err := LoadTaskSet(conf.Repo, conf.IDsFile, false)
if err != nil {
return err
}
ctx.PrintContextDescription()
query = query.Merge(ctx)
task := Task{
WritePending: true,
Status: STATUS_RESOLVED,
Summary: query.Text,
Tags: query.Tags,
Project: query.Project,
Priority: query.Priority,
Resolved: time.Now(),
}
task = ts.MustLoadTask(task)
ts.SavePendingChanges()
MustGitCommit(conf.Repo, "Logged %s", task)
return nil
}
// CommandModify applies a change to tasks specified by ID, or all tasks in
// current context
func CommandModify(conf Config, ctx, query Query) error {
if !query.HasOperators() {
return errors.New("no operations specified")
}
ts, err := LoadTaskSet(conf.Repo, conf.IDsFile, false)
if err != nil {
return err
}
if len(query.IDs) == 0 {
ts.Filter(ctx)
if StdoutIsTTY() {
ConfirmOrAbort("no IDs specified. Apply to all %d tasks in current ctx?", len(ts.Tasks()))
}
for _, task := range ts.Tasks() {
task.Modify(query)
ts.MustUpdateTask(task)
ts.SavePendingChanges()
MustGitCommit(conf.Repo, "Modified %s", task)
}
} else {
for _, id := range query.IDs {
task := ts.MustGetByID(id)
task.Modify(query)
ts.MustUpdateTask(task)
ts.SavePendingChanges()
MustGitCommit(conf.Repo, "Modified %s", task)
}
}
return nil
}
// CommandNext prints the unresolved tasks associated with the current context.
// This is the default command.
func CommandNext(conf Config, ctx, query Query) error {
ts, err := LoadTaskSet(conf.Repo, conf.IDsFile, false)
if err != nil {
return err
}
if len(query.IDs) > 0 {
// addressing task by ID, ignores context
if query.HasOperators() {
return errors.New("operators not valid when addressing task by ID")
}
} else {
// apply context
query = query.Merge(ctx)
}
ts.Filter(query)
ts.DisplayByNext(ctx, true)
return nil
}
// CommandNote edits or prints the markdown note associated with the task.
func CommandNote(conf Config, ctx, query Query) error {
if len(query.IDs) == 0 {
return errors.New("no ID(s) specified")
}
if query.HasOperators() {
return errors.New("operators not valid in this context")
}
ts, err := LoadTaskSet(conf.Repo, conf.IDsFile, false)
if err != nil {
return err
}
for _, id := range query.IDs {
task := ts.MustGetByID(id)
// If stdout is a TTY, we may open the editor
if StdoutIsTTY() {
if query.Text == "" {
task.Notes = string(MustEditBytes([]byte(task.Notes), MakeTempFilename(task.ID, task.Summary, "md")))
} else {
if task.Notes == "" {
task.Notes = query.Text
} else {
task.Notes += "\n" + query.Text
}
}
ts.MustUpdateTask(task)
ts.SavePendingChanges()
MustGitCommit(conf.Repo, "Edit note %s", task)
} else {
// If stdout is not a TTY, we simply write markdown notes to stdout
if err := WriteStdout([]byte(task.Notes)); err != nil {
ExitFail("Could not write to stdout: %v", err)
}
}
}
return nil
}
// CommandOpen opens a task URL in the browser, if the task has a URL.
func CommandOpen(conf Config, ctx, query Query) error {
if len(query.IDs) == 0 {
return errors.New("no ID(s) specified")
}
if query.HasOperators() {
return errors.New("operators not valid in this context")
}
ts, err := LoadTaskSet(conf.Repo, conf.IDsFile, false)
if err != nil {
return err
}
for _, id := range query.IDs {
task := ts.MustGetByID(id)
urls := xurls.Relaxed().FindAllString(task.Summary+" "+task.Notes, -1)
if len(urls) == 0 {
return fmt.Errorf("no URLs found in task %v", task.ID)
}
for _, url := range urls {
MustOpenBrowser(url)
}
}
return nil
}
// CommandRemove removes a task by ID from the database.
func CommandRemove(conf Config, ctx, query Query) error {
if len(query.IDs) == 0 {
return errors.New("no ID(s) specified")
}
if query.HasOperators() {
return errors.New("operators not valid in this context")
}
ts, err := LoadTaskSet(conf.Repo, conf.IDsFile, false)
if err != nil {
return err
}
for _, id := range query.IDs {
task := ts.MustGetByID(id)
fmt.Println(task)
}
if StdoutIsTTY() {
ConfirmOrAbort("\nThe above %d task(s) will be deleted without checking subtasks. Continue?", len(query.IDs))
}
for _, id := range query.IDs {
task := ts.MustGetByID(id)
// Mark our task for deletion
task.Deleted = true
// MustUpdateTask validates and normalises our task object
ts.MustUpdateTask(task)
ts.SavePendingChanges()
if query.Text != "" {
// commit comment, put in body
MustGitCommit(conf.Repo, "Removed: %s\n\n%s", task, query.Text)
} else {
MustGitCommit(conf.Repo, "Removed: %s", task)
}
}
return nil
}
// CommandShowActive prints a list of active tasks.
func CommandShowActive(conf Config, ctx, query Query) error {
ts, err := LoadTaskSet(conf.Repo, conf.IDsFile, false)
if err != nil {
return err
}
query = query.Merge(ctx)
ts.Filter(query)
ts.FilterByStatus(STATUS_ACTIVE)
ts.DisplayByNext(ctx, true)
return nil
}
// CommandShowProjects prints a list of projects associated with all tasks.
// Ignores context/query for valid output
func CommandShowProjects(conf Config, ctx, query Query) error {
if len(query.IDs) > 0 || query.HasOperators() {
return errors.New("query/context not supported for show-projects")
}
ts, err := LoadTaskSet(conf.Repo, conf.IDsFile, true)
if err != nil {
return err
}
ts.DisplayProjects()
return nil
}
// CommandShowOpen prints a list of open tasks without truncation
func CommandShowOpen(conf Config, ctx, query Query) error {
ts, err := LoadTaskSet(conf.Repo, conf.IDsFile, false)
if err != nil {
return err
}
query = query.Merge(ctx)
ts.Filter(query)
ts.DisplayByNext(ctx, false)
return nil
}
// CommandShowPaused prints a list of paused tasks.
func CommandShowPaused(conf Config, ctx, query Query) error {
ts, err := LoadTaskSet(conf.Repo, conf.IDsFile, false)
if err != nil {
return err
}
query = query.Merge(ctx)
ts.Filter(query)
ts.FilterByStatus(STATUS_PAUSED)
ts.DisplayByNext(ctx, true)
return nil
}
// CommandShowResolved prints a list of resolved tasks.
func CommandShowResolved(conf Config, ctx, query Query) error {
ts, err := LoadTaskSet(conf.Repo, conf.IDsFile, true)
if err != nil {
return err
}
query = query.Merge(ctx)
ts.UnHide()
ts.Filter(query)
ts.FilterByStatus(STATUS_RESOLVED)
ts.DisplayByWeek()
return nil
}
// CommandShowTags prints a list of all tags associated with non-resolved tasks.
func CommandShowTags(conf Config, ctx, query Query) error {
ts, err := LoadTaskSet(conf.Repo, conf.IDsFile, false)
if err != nil {
return err
}
query = query.Merge(ctx)
ts.Filter(query)
for tag := range ts.GetTags() {
fmt.Println(tag)
}
return nil
}
// CommandShowTemplates show a list of task templates.
func CommandShowTemplates(conf Config, ctx, query Query) error {
ts, err := LoadTaskSet(conf.Repo, conf.IDsFile, false)
if err != nil {
return err
}
ts.UnHide()
ts.FilterByStatus(STATUS_TEMPLATE)
query = query.Merge(ctx)
ts.Filter(query)
ts.DisplayByNext(ctx, false)
return nil
}
// CommandShowUnorganised prints a list of tasks without tags or projects.
// no context / query valid
func CommandShowUnorganised(conf Config, ctx, query Query) error {
if len(query.IDs) > 0 || query.HasOperators() {
return errors.New("query/context not used for show-unorganised")
}
ts, err := LoadTaskSet(conf.Repo, conf.IDsFile, false)
if err != nil {
return err
}
ts.FilterOrganised()
ts.DisplayByNext(ctx, true)
return nil
}
// CommandStart marks an existing task as started, by ID. If no ID is
// specified, it creates a new task and starts it.
func CommandStart(conf Config, ctx, query Query) error {
ts, err := LoadTaskSet(conf.Repo, conf.IDsFile, false)
if err != nil {
return err
}
if query.Template > 0 {
return errors.New("templates not yet supported for start command")
}
if len(query.IDs) > 0 {
// start given tasks by IDs
for _, id := range query.IDs {
task := ts.MustGetByID(id)
task.Status = STATUS_ACTIVE
if query.Text != "" {
task.Notes += "\n" + query.Text
}
ts.MustUpdateTask(task)
ts.SavePendingChanges()
MustGitCommit(conf.Repo, "Started %s", task)
if task.Notes != "" {
fmt.Printf("\nNotes on task %d:\n\033[38;5;245m%s\033[0m\n\n", task.ID, task.Notes)
}
}
} else if query.Text != "" {
// create a new task that is already active (started)
query = query.Merge(ctx)
task := Task{
WritePending: true,
Status: STATUS_ACTIVE,
Summary: query.Text,
Tags: query.Tags,
Project: query.Project,
Priority: query.Priority,
Notes: query.Note,
}
task = ts.MustLoadTask(task)
ts.SavePendingChanges()
MustGitCommit(conf.Repo, "Added and started %s", task)
} else {
return errors.New("nothing to do -- specify an ID or describe a task")
}
return nil
}
// CommandStop marks a task as stopped.
func CommandStop(conf Config, ctx, query Query) error {
ts, err := LoadTaskSet(conf.Repo, conf.IDsFile, false)
if err != nil {
return err
}
if query.HasOperators() {
return errors.New("operators not valid in this context")
}
if len(query.IDs) == 0 {
return errors.New("no ID(s) specified")
}
for _, id := range query.IDs {
task := ts.MustGetByID(id)
task.Status = STATUS_PAUSED
if query.Text != "" {
task.Notes += "\n" + query.Text
}
ts.MustUpdateTask(task)
ts.SavePendingChanges()
MustGitCommit(conf.Repo, "Stopped %s", task)
}
return nil
}
// CommandSync pushes and pulls task database changes from the remote repository.
func CommandSync(repoPath string) error {
// TODO(dontlaugh) return error
Sync(repoPath)
return nil
}
// CommandTemplate creates a new task template.
func CommandTemplate(conf Config, ctx, query Query) error {
ts, err := LoadTaskSet(conf.Repo, conf.IDsFile, false)
if err != nil {
return err
}
if len(query.IDs) > 0 {
for _, id := range query.IDs {
task := ts.MustGetByID(id)
task.Status = STATUS_TEMPLATE
ts.MustUpdateTask(task)
ts.SavePendingChanges()
MustGitCommit(conf.Repo, "Changed %s to Template", task)
}
} else if query.Text != "" {
query = query.Merge(ctx)
task := Task{
WritePending: true,
Status: STATUS_TEMPLATE,
Summary: query.Text,
Tags: query.Tags,
Project: query.Project,
Priority: query.Priority,
Notes: query.Note,
}
task = ts.MustLoadTask(task)
ts.SavePendingChanges()
MustGitCommit(conf.Repo, "Created Template %s", task)
}
return nil
}
// CommandUndo performs undo with git revert.
func CommandUndo(conf Config, args []string, ctx, query Query) error {
var err error
n := 1
if len(args) == 3 {
n, err = strconv.Atoi(args[2])
if err != nil {
Help(CMD_UNDO)
return err
}
}
MustRunGitCmd(conf.Repo, "revert", "--no-gpg-sign", "--no-edit", "HEAD~"+strconv.Itoa(n)+"..")
return nil
}
// CommandVersion prints version information for the dstask binary.
func CommandVersion() {
fmt.Printf(
"Version: %s\nGit commit: %s\nBuild date: %s\n",
VERSION,
GIT_COMMIT,
BUILD_DATE,
)
}