-
Notifications
You must be signed in to change notification settings - Fork 2
/
done.go
97 lines (81 loc) · 1.92 KB
/
done.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
package cmd
import (
"fmt"
"strings"
"github.com/MakeNowJust/heredoc/v2"
"github.com/emersion/go-ical"
"github.com/kkga/tdx/vdir"
"github.com/spf13/cobra"
)
type doneOptions struct {
toggle bool
}
func NewDoneCmd() *cobra.Command {
opts := &doneOptions{}
cmd := &cobra.Command{
Use: "done <id>...",
Aliases: []string{"do"},
Short: "Complete todos",
Long: "Mark todos as completed.",
Args: cobra.MinimumNArgs(1),
Example: heredoc.Doc(`
$ tdx done 1
$ tdx done 1 2 3`),
RunE: func(cmd *cobra.Command, args []string) error {
vd := make(vdir.Vdir)
if err := vd.Init(vdirPath); err != nil {
return err
}
IDs, err := stringsToInts(args)
if err != nil {
return err
}
var items []*vdir.Item
for _, id := range IDs {
item, err := vd.ItemById(id)
if err != nil {
return err
}
items = append(items, item)
}
return runDone(items, opts)
},
}
cmd.Flags().BoolVarP(&opts.toggle, "toggle", "t", false, "toggle completed state")
return cmd
}
func runDone(items []*vdir.Item, opts *doneOptions) error {
sb := strings.Builder{}
for _, item := range items {
vtodo, err := item.Vtodo()
if err != nil {
return err
}
if opts.toggle {
s, err := vtodo.Props.Text(ical.PropStatus)
if err != nil {
return err
}
switch vdir.ToDoStatus(s) {
case vdir.StatusCompleted:
vtodo.Props.SetText(ical.PropStatus, string(vdir.StatusNeedsAction))
case vdir.StatusNeedsAction, vdir.StatusCancelled:
vtodo.Props.SetText(ical.PropStatus, string(vdir.StatusCompleted))
default:
vtodo.Props.SetText(ical.PropStatus, string(vdir.StatusCompleted))
}
} else {
vtodo.Props.SetText(ical.PropStatus, string(vdir.StatusCompleted))
}
if err := item.WriteFile(); err != nil {
return err
}
s, err := item.Format()
if err != nil {
return err
}
sb.WriteString(fmt.Sprintf("%s", s))
}
fmt.Print(sb.String())
return nil
}