forked from jason0x43/alfred-toggl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtag.go
229 lines (192 loc) · 4.94 KB
/
tag.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
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/jason0x43/go-alfred"
"github.com/jason0x43/go-toggl"
)
// tag filter --------------------------------------------
// TagCommand is a command
type TagCommand struct{}
// About returns information about this command
func (c TagCommand) About() alfred.CommandDef {
return alfred.CommandDef{
Keyword: "tags",
Description: "List your tags",
IsEnabled: config.APIKey != "",
}
}
// Items returns a list of filter items
func (c TagCommand) Items(arg, data string) (items []alfred.Item, err error) {
dlog.Printf("getting tag items")
if err = checkRefresh(); err != nil {
return
}
var cfg tagCfg
if data != "" {
if err = json.Unmarshal([]byte(data), &cfg); err != nil {
dlog.Printf("Error unmarshalling tag config: %v", err)
}
}
tid := -1
if cfg.Tag != nil {
tid = *cfg.Tag
}
if tid != -1 {
// List menu for a project
if tag, _, ok := getTagByID(tid); ok {
return tagItems(tag, arg)
}
} else {
tagCfg := tagCfg{}
for _, entry := range cache.Account.Data.Tags {
if alfred.FuzzyMatches(entry.Name, arg) {
tagCfg.Tag = &entry.ID
items = append(items, alfred.Item{
UID: fmt.Sprintf("%s.tag.%d", workflow.BundleID(), entry.ID),
Title: entry.Name,
Autocomplete: entry.Name,
Arg: &alfred.ItemArg{
Keyword: "tags",
Data: alfred.Stringify(tagCfg),
},
})
}
}
if len(items) == 0 && arg != "" {
tagCfg.ToCreate = &createTagMessage{Name: arg}
items = append(items, alfred.Item{
Title: arg,
Subtitle: "New tag",
Arg: &alfred.ItemArg{
Keyword: "tags",
Mode: alfred.ModeDo,
Data: alfred.Stringify(tagCfg),
},
})
}
if len(items) == 0 {
items = append(items, alfred.Item{Title: "No matching tags"})
}
}
return items, nil
}
// Do runs the command
func (c TagCommand) Do(data string) (out string, err error) {
var cfg tagCfg
if data != "" {
if err := json.Unmarshal([]byte(data), &cfg); err != nil {
dlog.Printf("Error unmarshaling tag data: %v", err)
}
}
session := toggl.OpenSession(config.APIKey)
if cfg.ToUpdate != nil {
if _, err = session.UpdateTag(*cfg.ToUpdate); err != nil {
out += "Updated tag"
}
}
if cfg.ToCreate != nil {
var tag toggl.Tag
if cfg.ToCreate.WID == 0 {
cfg.ToCreate.WID = cache.Account.Data.Workspaces[0].ID
}
if tag, err = session.CreateTag(cfg.ToCreate.Name, cfg.ToCreate.WID); err == nil {
cache.Account.Data.Tags = append(cache.Account.Data.Tags, tag)
if err := alfred.SaveJSON(cacheFile, &cache); err != nil {
log.Printf("Error saving cache: %s\n", err)
}
}
if out != "" {
out += ", created tag"
} else {
out = "Created tag"
}
}
if cfg.ToDelete != nil {
var ok bool
var tag toggl.Tag
var index int
var id = *cfg.ToDelete
if tag, index, ok = getTagByID(id); !ok {
err = fmt.Errorf(`Tag %d does not exist`, id)
return
}
if _, err = session.DeleteTag(tag); err == nil {
adata := &cache.Account.Data
if index < len(adata.Tags)-1 {
adata.Tags = append(adata.Tags[:index], adata.Tags[index+1:]...)
} else {
adata.Tags = adata.Tags[:index]
}
if err := alfred.SaveJSON(cacheFile, &cache); err != nil {
dlog.Printf("Error saving cache: %s\n", err)
}
}
if out != "" {
out += ", deleted tag"
} else {
out = "Deleted tag"
}
}
// Since tags are referenced by name, updating one can cause changes in
// multiple time entries. The simplest way to handle that is just to
// refresh everything.
refresh()
return
}
// support -------------------------------------------------------------------
type tagCfg struct {
Tag *int
ToCreate *createTagMessage
ToUpdate *toggl.Tag
ToDelete *int
}
type createTagMessage struct {
Name string
WID int
}
func tagItems(tag toggl.Tag, arg string) (items []alfred.Item, err error) {
if alfred.FuzzyMatches("timers", arg) {
items = append(items, alfred.Item{
Title: "Time entries...",
Subtitle: "List associated time entries",
Autocomplete: "Time entries...",
Arg: &alfred.ItemArg{
Keyword: "timers",
Data: alfred.Stringify(timerCfg{Tag: &tag.ID}),
},
})
}
if alfred.FuzzyMatches("name", arg) {
item := alfred.Item{}
_, name := alfred.SplitCmd(arg)
if name != "" {
updateEntry := tag
updateEntry.Name = name
item.Title = fmt.Sprintf("Change name to '%s'", name)
item.Subtitle = "Name: " + tag.Name
item.Arg = &alfred.ItemArg{
Keyword: "tags",
Mode: alfred.ModeDo,
Data: alfred.Stringify(tagCfg{ToUpdate: &updateEntry}),
}
} else {
item.Title = "Name: " + tag.Name
item.Autocomplete = "Name: "
}
items = append(items, item)
}
if alfred.FuzzyMatches("delete", arg) {
items = append(items, alfred.Item{
Title: "Delete",
Autocomplete: "Delete",
Arg: &alfred.ItemArg{
Keyword: "tags",
Mode: alfred.ModeDo,
Data: alfred.Stringify(tagCfg{ToDelete: &tag.ID}),
},
})
}
return
}