/
task.go
832 lines (726 loc) · 20.4 KB
/
task.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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
package taskpoet
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"path/filepath"
"sort"
"strings"
"time"
"github.com/google/uuid"
"github.com/pterm/pterm"
bolt "go.etcd.io/bbolt"
)
var prefixes []string
// Task is the actual task item
type Task struct {
ID string `json:"id"`
PluginID string `json:"plugin_id"`
Description string `json:"description"`
Due *time.Time `json:"due,omitempty"`
HideUntil *time.Time `json:"hide_until,omitempty"` // HideUntil is similar to 'wait' in taskwarrior
CancelAfter *time.Time `json:"cancel_after,omitempty"` // CancelAfter is similar to 'until' in taskwarrior
Completed *time.Time `json:"completed,omitempty"`
Reviewed *time.Time `json:"reviewed,omitempty"`
Deleted *time.Time `json:"deleted,omitempty"`
Added time.Time `json:"added,omitempty"`
EffortImpact EffortImpact `json:"effort_impact"`
Children []string `json:"children,omitempty"`
Parents []string `json:"parents,omitempty"`
Tags []string `json:"tags,omitempty"`
Comments []Comment `json:"comments,omitempty"`
Project string `json:"project,omitempty"`
Urgency float64 `json:"urgency:omitempty"`
}
// Comment is just a little comment/note on a task
type Comment struct {
Added time.Time `json:"added,omitempty"`
Comment string `json:"comment,omitempty"`
}
// DueString returns a string representation of Due
/*
func (t Task) DueString() string {
if t.Due != nil {
return t.Due.Format("2006-01-02")
}
return ""
}
*/
// Tasks represents multiple Task items
type Tasks []*Task
// SortBy specifies how to sort the tasks
func (t *Tasks) SortBy(s any) {
switch s.(type) {
case ByDue:
sort.Sort(ByDue(*t))
case ByCompleted:
sort.Sort(ByCompleted(*t))
case ByUrgency:
sort.Sort(ByUrgency(*t))
default:
sort.Sort(*t)
}
}
// Len helps to satisfy the sort interface
func (t Tasks) Len() int {
return len(t)
}
// Swap helps to satisfy the sort interface
func (t Tasks) Swap(i, j int) {
t[i], t[j] = t[j], t[i]
}
// Less helps to satisfy the sort interface
func (t Tasks) Less(i, j int) bool {
return t[i].Added.Before(t[j].Added)
}
// DetectKeyPath finds the key path for a given task
func (t *Task) DetectKeyPath() []byte {
// Is this a new active task, or just logging completed?
pluginID := DefaultPluginID
if t.PluginID != "" {
pluginID = t.PluginID
}
switch {
case t.Deleted != nil:
return []byte(filepath.Join("/deleted", pluginID, t.ID))
case t.Completed != nil:
return []byte(filepath.Join("/completed", pluginID, t.ID))
default:
return []byte(filepath.Join("/active", pluginID, t.ID))
}
}
func (t *Task) setDefaults(d *Task) {
if t.Added.IsZero() {
t.Added = time.Now()
}
// If no ID is set, just generate one
if t.ID == "" {
t.ID = uuid.New().String()
}
// Set the plugin id to default if not set
if t.PluginID == "" {
t.PluginID = DefaultPluginID
}
// Handle defaults due
if d != nil {
if t.Due == nil && d.Due != nil {
t.Due = d.Due
}
}
}
// ShortID is just the first 5 characters of the ID
func (t *Task) ShortID() string {
return t.ID[0:min(len(t.ID), 5)]
}
// TaskService is the interface to tasks...we'll delete this junk
type TaskService interface {
// This should replace New, and Log
Add(t *Task) (*Task, error)
AddSet(t Tasks) error
// BucketName() string
// Helper for adding Parent
AddParent(c, p *Task) error
AddChild(p, c *Task) error
// Describe something
Describe(t *Task) error
// Edit a Task entry
Edit(t *Task) (*Task, error)
EditSet(t []Task) error
// Should we use this instead of above funcs?
AddOrEditSet(tasks []Task) error
// Purge completely removes a task out of the DB
Purge(t *Task) error
// Complete a task
Complete(t *Task) error
// Sweet lord, this gettin' confusin' Drew
GetPlugins() (map[string]Creator, error)
SyncPlugin(TaskPlugin) error
// States of a Task
GetStates() []string
GetStatePaths() []string
// Just a little helper function to add and immediately mark as completed
Log(t, d *Task) (*Task, error)
// This on prolly needs work
List(prefix string) (Tasks, error)
/*
Path Conventions
/${state}/${plugin-id}/${id}
$state -> Active or Completed
$plugin-id -> Could be 'default'...or ''...or 'plugin-1'
$id -> Unique identifier
$plugin-id/$id should be unique together. Think things like:
gitlab.com/ISSUE-2
gitlab.local.io/ISSUE-2
*/
// New way to get stuff
GetWithID(id, pluginID, state string) (*Task, error)
GetWithPartialID(partialID, pluginID, state string) (*Task, error)
GetWithExactPath(path []byte) (*Task, error)
// Old way to get stuff, delete soon
GetIDsByPrefix(prefix string) ([]string, error)
}
// GetStates returns types of states
func (svc *TaskServiceOp) GetStates() []string {
return []string{"active", "completed", "deleted"}
}
// GetStatePaths is each of the states with a / prefix i guess
func (svc *TaskServiceOp) GetStatePaths() []string {
s := svc.GetStates()
r := make([]string, len(s))
for idx, item := range s {
r[idx] = filepath.Join("/", item)
}
return r
}
// SyncPlugin syncs a plugin or whatever
func (svc *TaskServiceOp) SyncPlugin(tp TaskPlugin) error {
ts, err := tp.Sync()
if err != nil {
return err
}
err = svc.localClient.Task.AddOrEditSet(ts)
if err != nil {
return err
}
return nil
}
// GetPlugins returns all plugins
func (svc *TaskServiceOp) GetPlugins() (map[string]Creator, error) {
return TaskPlugins, nil
}
// AddParent adds a parent to a child command
func (svc *TaskServiceOp) AddParent(c, p *Task) error {
c.Parents = append(c.Parents, p.ID)
p.Children = append(p.Children, c.ID)
if err := svc.EditSet([]Task{*c, *p}); err != nil {
return err
}
return nil
}
// AddChild adds a child to a parent
func (svc *TaskServiceOp) AddChild(p, c *Task) error {
p.Children = append(p.Children, c.ID)
c.Parents = append(c.Parents, p.ID)
if err := svc.EditSet([]Task{*c, *p}); err != nil {
return err
}
return nil
}
// AddOrEditSet adds or edits a set of tasks
func (svc *TaskServiceOp) AddOrEditSet(tasks []Task) error {
var addSet Tasks
var editSet []Task
for _, t := range tasks {
t := t
_, err := svc.GetWithExactPath(t.DetectKeyPath())
if err != nil {
addSet = append(addSet, &t)
} else {
editSet = append(editSet, t)
}
}
if err := svc.localClient.Task.AddSet(addSet); err != nil {
return err
}
if err := svc.localClient.Task.EditSet(editSet); err != nil {
return err
}
return nil
}
// EditSet edits a single set of tasks
func (svc *TaskServiceOp) EditSet(tasks []Task) error { //nolint:funlen,gocognit
// We need to merge the new value with the old
var mergedTasks []Task
for _, t := range tasks {
t := t
originalTask, err := svc.GetWithExactPath(t.DetectKeyPath())
if err != nil {
return errors.New("cannot edit a task that did not previously exist: " + t.ID)
}
// Right now we wanna use the Complete function to do this, not edit...at least yet
if originalTask.Completed != t.Completed {
return errors.New("editing the Completed field is not yet supported as it changes the path")
}
// var mergedTask Task
// Decide how to handle missing data and such
// Added should never change
t.Added = originalTask.Added
// Re-add the description if needed
if t.Description == "" {
t.Description = originalTask.Description
}
if t.Due == nil {
t.Due = originalTask.Due
}
if t.Completed == nil {
t.Completed = originalTask.Completed
}
if t.HideUntil == nil {
t.HideUntil = originalTask.HideUntil
}
if t.EffortImpact == 0 {
t.EffortImpact = originalTask.EffortImpact
}
mergedTasks = append(mergedTasks, t)
}
err := svc.localClient.DB.Update(func(tx *bolt.Tx) error {
for _, t := range mergedTasks {
taskSerial, err := json.Marshal(t)
if err != nil {
return err
}
if perr := svc.localClient.getBucket(tx).Put(t.DetectKeyPath(), taskSerial); perr != nil {
return perr
}
}
return nil
})
if err != nil {
return err
}
return nil
}
// Purge removes a task from the database
func (svc *TaskServiceOp) Purge(t *Task) error {
originalTask, err := svc.GetWithExactPath(t.DetectKeyPath())
if err != nil {
return errors.New("Cannot delete a task that did not previously exist: " + t.ID)
}
if svc.localClient.DB.Update(func(tx *bolt.Tx) error {
if derr := svc.localClient.getBucket(tx).Delete(originalTask.DetectKeyPath()); derr != nil {
return err
}
return nil
}) != nil {
return err
}
return nil
}
// Edit edits an existing task
func (svc *TaskServiceOp) Edit(t *Task) (*Task, error) {
originalTask, err := svc.GetWithExactPath(t.DetectKeyPath())
if err != nil {
return nil, errors.New("cannot edit a task that did not previously exist: " + t.ID)
}
if verr := t.Validate(); verr != nil {
return nil, verr
}
// Right now we wanna use the Complete function to do this, not edit...at least yet
if originalTask.Completed != t.Completed {
return nil, errors.New("editing the Completed field is not yet supported as it changes the path")
}
taskSerial, err := json.Marshal(t)
if err != nil {
return nil, err
}
if uerr := svc.localClient.DB.Update(func(tx *bolt.Tx) error {
if err := svc.localClient.getBucket(tx).Put(t.DetectKeyPath(), taskSerial); err != nil {
return err
}
return nil
}); uerr != nil {
return nil, uerr
}
return t, nil
}
// Describe describes a task
func (svc *TaskServiceOp) Describe(t *Task) error {
now := time.Now()
var due string
var wait string
var completed string
if t.Due == nil {
due = "-"
} else {
due = humanizeDuration(t.Due.Sub(now))
}
if t.HideUntil == nil {
wait = "-"
} else {
wait = humanizeDuration(t.HideUntil.Sub(now))
}
added := humanizeDuration(t.Added.Sub(now))
if t.Completed == nil {
completed = "-"
} else {
completed = humanizeDuration(t.Completed.Sub(now))
}
var parentsBuff bytes.Buffer
var childrenBuff bytes.Buffer
for _, p := range t.Parents {
parent, err := svc.GetWithID(p, "", "")
if err != nil {
return nil
}
parentsBuff.Write([]byte(parent.Description))
}
for _, c := range t.Children {
child, err := svc.GetWithID(c, "", "")
if err != nil {
return nil
}
childrenBuff.Write([]byte(child.Description))
}
data := [][]string{
{"Field", "Value", "Read-Value"},
{"ID", t.ShortID(), t.ID},
{"Description", t.Description, ""},
{"Added", added, fmt.Sprintf("%+v", t.Added)},
{"Completed", completed, fmt.Sprintf("%+v", t.Completed)},
{"Due", due, fmt.Sprintf("%+v", t.Due)},
{"Wait", wait, fmt.Sprintf("%+v", t.HideUntil)},
{"Effort/Impact", EffortImpactText(int(t.EffortImpact)), fmt.Sprintf("%+v", t.EffortImpact)},
{"Parents", parentsBuff.String(), fmt.Sprintf("%+v", t.Parents)},
{"Children", childrenBuff.String(), fmt.Sprintf("%+v", t.Children)},
{"Tags", fmt.Sprintf("%+v", t.Tags), ""},
}
return pterm.DefaultTable.WithHasHeader().WithData(data).Render()
}
// TaskServiceOp is the TaskService Operator
type TaskServiceOp struct {
localClient *Poet
}
// DefaultPluginID is just the string used as the built in plugin default
const DefaultPluginID string = "builtin"
// GetWithPartialID returns using a partial id of the task
func (svc *TaskServiceOp) GetWithPartialID(partialID, pluginID, state string) (*Task, error) {
var possibleStates []string
if state == "" {
possibleStates = svc.GetStatePaths()
} else {
possibleStates = append(possibleStates, state)
}
matches := []string{}
if pluginID == "" {
pluginID = DefaultPluginID
}
qualifiedPartialID := filepath.Join(pluginID, partialID)
for _, prefix := range possibleStates {
ids, err := svc.GetIDsByPrefix(fmt.Sprintf("%v/%v", prefix, qualifiedPartialID))
if err != nil {
return nil, err
}
for _, id := range ids {
if strings.HasPrefix(id, fmt.Sprintf("%v/%v", prefix, qualifiedPartialID)) {
matches = append(matches, id)
}
}
}
if len(matches) == 0 {
return nil, fmt.Errorf("no matches for %v found in %v", partialID, prefixes)
} else if len(matches) > 1 {
return nil, fmt.Errorf(
"more than 1 match for %v found in %v, please try using more of the ID. Returned: %v", partialID, prefixes, matches)
}
return svc.GetWithExactPath([]byte(matches[0]))
}
// GetWithExactPath returns a task from an exact path
func (svc *TaskServiceOp) GetWithExactPath(path []byte) (*Task, error) {
var task Task
if err := svc.localClient.DB.View(func(tx *bolt.Tx) error {
taskBytes := svc.localClient.getBucket(tx).Get(path)
if taskBytes == nil {
return fmt.Errorf("could not find task: %s", path)
}
return json.Unmarshal(taskBytes, &task)
}); err != nil {
return nil, err
}
return &task, nil
}
// GetWithID returns a task from an ID
func (svc *TaskServiceOp) GetWithID(id, pluginID, state string) (*Task, error) {
var possibleKeypaths []string
if pluginID == "" {
pluginID = DefaultPluginID
}
if state != "" {
possibleKeypaths = append(possibleKeypaths, filepath.Join(state, pluginID, id))
} else {
for _, state := range svc.GetStatePaths() {
possibleKeypaths = append(possibleKeypaths, filepath.Join(state, pluginID, id))
}
}
for _, possible := range possibleKeypaths {
task, err := svc.GetWithExactPath([]byte(possible))
if err == nil {
return task, nil
}
}
return nil, fmt.Errorf("could not find that task at any of %v", possibleKeypaths)
}
// Complete marks a task as completed
func (svc *TaskServiceOp) Complete(t *Task) error {
activePath := t.DetectKeyPath()
t.Completed = nowPTR()
completePath := t.DetectKeyPath()
if err := svc.localClient.DB.Update(func(tx *bolt.Tx) error {
taskSerial, err := json.Marshal(t)
if err != nil {
return err
}
b := svc.localClient.getBucket(tx)
if perr := b.Put(completePath, taskSerial); perr != nil {
return perr
}
return b.Delete(activePath)
}); err != nil {
return err
}
return nil
}
// List lists items under a given prefix
func (svc *TaskServiceOp) List(prefix string) (Tasks, error) {
var tasks Tasks
if err := svc.localClient.DB.View(func(tx *bolt.Tx) error {
if err := svc.localClient.getBucket(tx).ForEach(func(k, v []byte) error {
var task Task
if err := json.Unmarshal(v, &task); err != nil {
return err
}
if strings.HasPrefix(string(k), prefix) {
tasks = append(tasks, &task)
}
return nil
}); err != nil {
return err
}
return nil
}); err != nil {
return nil, err
}
return tasks, nil
}
// Log is a Shortcut utility to add a new task, but a completed time of 'now'
func (svc *TaskServiceOp) Log(t *Task, d *Task) (*Task, error) {
if t.Completed == nil {
t.Completed = nowPTR()
}
return svc.Add(t)
}
// AddSet adds a task set
func (svc *TaskServiceOp) AddSet(t Tasks) error {
for _, task := range t {
task := task
if _, err := svc.Add(task); err != nil {
return err
}
}
return nil
}
// Add adds a new task
func (svc *TaskServiceOp) Add(t *Task) (*Task, error) {
// t is the new task
t.setDefaults(&svc.localClient.Default)
// Assign a weight/urgency
t.Urgency = svc.localClient.curator.Weigh(*t)
// Does this already exist??
if svc.localClient.exists(t) {
return nil, errExists
}
taskSerial, err := json.Marshal(t)
if err != nil {
return nil, err
}
if uerr := svc.localClient.DB.Update(func(tx *bolt.Tx) error {
if perr := svc.localClient.getBucket(tx).Put(t.DetectKeyPath(), taskSerial); perr != nil {
return perr
}
return nil
}); uerr != nil {
return nil, uerr
}
return t, nil
}
// GetIDsByPrefix returns a list of ids matching the given prefix
func (svc *TaskServiceOp) GetIDsByPrefix(prefix string) ([]string, error) {
allIDs := []string{}
if err := svc.localClient.DB.View(func(tx *bolt.Tx) error {
if err := svc.localClient.getBucket(tx).ForEach(func(k, v []byte) error {
if strings.HasPrefix(string(k), prefix) {
allIDs = append(allIDs, string(k))
}
return nil
}); err != nil {
return err
}
return nil
}); err != nil {
return nil, err
}
return allIDs, nil
}
// CompleteIDsWithPrefix returns a list of ids matching the given prefix and
// autocomplete pattern
func (p Poet) CompleteIDsWithPrefix(prefix, toComplete string) []string {
allIDs := []string{}
if err := p.DB.View(func(tx *bolt.Tx) error {
if err := p.getBucket(tx).ForEach(func(k, v []byte) error {
if strings.HasPrefix(string(k), prefix) {
idPieces := strings.Split(string(k), "/")
id := idPieces[len(idPieces)-1]
var task Task
panicIfErr(json.Unmarshal(v, &task))
if strings.HasPrefix(id, toComplete) || strings.Contains(task.Description, toComplete) {
allIDs = append(allIDs, fmt.Sprintf("%v\t%v", id[0:5], task.Description))
}
}
return nil
}); err != nil {
return err
}
return nil
}); err != nil {
return nil
}
return allIDs
}
func panicIfErr(err error) {
if err != nil {
panic(err)
}
}
// TaskOption is a functional option for a new Task
type TaskOption func(*Task)
// WithEffortImpact sets the impact statement on create
func WithEffortImpact(e EffortImpact) TaskOption {
return func(t *Task) {
t.EffortImpact = e
}
}
// WithDescription sets the description on create
func WithDescription(d string) TaskOption {
return func(t *Task) {
t.Description = d
}
}
// WithID sets the UUID on create
func WithID(i string) TaskOption {
return func(t *Task) {
t.ID = i
}
}
// WithChildren set the children of a task
func WithChildren(c []string) TaskOption {
return func(t *Task) {
t.Children = c
}
}
// WithParents set the parents of a task
func WithParents(p []string) TaskOption {
return func(t *Task) {
t.Parents = p
}
}
// WithTags sets the tags on create
func WithTags(s []string) TaskOption {
return func(t *Task) {
t.Tags = s
}
}
// WithDue sets the due date on create
func WithDue(d *time.Time) TaskOption {
return func(t *Task) {
t.Due = d
}
}
// WithCompleted sets the completed date on create
func WithCompleted(d *time.Time) TaskOption {
return func(t *Task) {
t.Completed = d
}
}
// WithAdded sets the added date on create
func WithAdded(d *time.Time) TaskOption {
return func(t *Task) {
t.Added = *d
}
}
// WithHideUntil sets the hide until on create
func WithHideUntil(d *time.Time) TaskOption {
return func(t *Task) {
t.HideUntil = d
}
}
// WithTaskWarriorTask imports a task from a task warrior task
func WithTaskWarriorTask(twItem TaskWarriorTask) TaskOption {
return func(t *Task) {
t.Description = twItem.Description
t.ID = twItem.UUID
t.Tags = twItem.Tags
t.Due = (*time.Time)(twItem.Due)
t.Completed = (*time.Time)(twItem.End)
t.Reviewed = (*time.Time)(twItem.Reviewed)
t.CancelAfter = (*time.Time)(twItem.Until)
if twItem.Status == "deleted" {
t.Deleted = (*time.Time)(twItem.End)
}
if twItem.Entry == nil {
t.Added = time.Now()
} else {
t.Added = time.Time(*twItem.Entry)
}
if twItem.Annotations != nil {
t.Comments = make([]Comment, len(twItem.Annotations))
for idx, a := range twItem.Annotations {
t.Comments[idx].Comment = a.Description
t.Comments[idx].Added = time.Time(*a.Entry)
}
}
if (twItem.Wait != nil) && (twItem.Due != nil) && (*time.Time)(twItem.Wait).After((time.Time)(*twItem.Due)) {
nh := t.Due.Add(-1 * time.Minute)
t.HideUntil = &nh
} else {
t.HideUntil = (*time.Time)(twItem.Wait)
}
}
}
// MustNewTask returns a task or panics
func MustNewTask(description string, options ...TaskOption) *Task {
got, err := NewTask(description, options...)
if err != nil {
panic(err)
}
return got
}
// Validate makes sure the task isn't malformed
func (t Task) Validate() error {
if t.Description == "" {
return errors.New("missing description for Task")
}
if strings.Contains(t.ID, "/") {
return errors.New("ID Cannot contain a slash (/)")
}
// If both HideUntil and Due are set, make sure HideUntil isn't after Due
if t.HideUntil != nil && t.Due != nil {
if t.HideUntil.After(*t.Due) {
return fmt.Errorf("HideUntil cannot be later than Due")
}
}
// Make sure we didn't add ourself
if containsString(t.Parents, t.ID) {
return fmt.Errorf("self id is set in the parents, we don't do that")
}
if containsString(t.Children, t.ID) {
return fmt.Errorf("self id is set in the children, we don't do that")
}
// kid, _ := lc.Task.Add(&Task{ID: "test-self-add-child", Description: "foo"})
// Make sure Parents contains no duplicates
if !CheckUniqueStringSlice(t.Parents) {
return fmt.Errorf("found duplicate ids in the Parents field")
}
return nil
}
// NewTask returns a new task given functional options
func NewTask(desc string, options ...TaskOption) (*Task, error) {
task := &Task{
Description: desc,
}
for _, opt := range options {
opt(task)
}
// Sort the tags alphabetically
sort.Strings(task.Tags)
return task, task.Validate()
}