-
Notifications
You must be signed in to change notification settings - Fork 17
/
project.go
288 lines (238 loc) · 6.93 KB
/
project.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
package main
import (
"encoding/json"
"fmt"
"github.com/jason0x43/go-alfred"
"github.com/jason0x43/go-toggl"
)
// ProjectCommand is a command for handling projects
type ProjectCommand struct{}
// About returns information about a command
func (c ProjectCommand) About() alfred.CommandDef {
return alfred.CommandDef{
Keyword: "projects",
Description: "List your projects, add new ones",
IsEnabled: config.APIKey != "",
}
}
// Items returns a list of filter items
func (c ProjectCommand) Items(arg, data string) (items []alfred.Item, err error) {
if err = checkRefresh(); err != nil {
return
}
var cfg projectCfg
if data != "" {
if err = json.Unmarshal([]byte(data), &cfg); err != nil {
dlog.Printf("Error unmarshalling projects var: %v", err)
}
}
pid := -1
if cfg.Project != nil {
pid = *cfg.Project
}
runningTimer, isRunning := getRunningTimer()
if pid != -1 {
// List menu for a project
if project, _, ok := getProjectByID(pid); ok {
return projectItems(project, arg)
}
} else {
projectCfg := projectCfg{}
for _, entry := range cache.Account.Projects {
clientName := ""
if entry.Cid != nil {
client, _, _ := getClientByID(*entry.Cid)
clientName = client.Name
}
if entry.IsActive() && alfred.FuzzyMatches(entry.Name+clientName, arg) {
projectCfg.Project = &entry.ID
item := alfred.Item{
UID: fmt.Sprintf("%s.project.%d", workflow.BundleID(), entry.ID),
Title: entry.Name,
Subtitle: clientName,
Autocomplete: entry.Name,
Icon: "off.png",
Arg: &alfred.ItemArg{
Keyword: "projects",
Data: alfred.Stringify(projectCfg),
},
}
if isRunning && runningTimer.Pid != nil && *runningTimer.Pid == entry.ID {
item.Icon = "running.png"
}
items = append(items, item)
}
}
if len(items) == 0 && arg != "" {
projectCfg.ToCreate = &createProjectMessage{Name: arg}
items = append(items, alfred.Item{
Title: arg,
Subtitle: "New project",
Arg: &alfred.ItemArg{
Keyword: "projects",
Mode: alfred.ModeDo,
Data: alfred.Stringify(projectCfg),
},
})
}
if len(items) == 0 {
items = append(items, alfred.Item{Title: "No matching projects"})
}
}
return items, nil
}
// Do runs the command
func (c ProjectCommand) Do(data string) (out string, err error) {
var cfg projectCfg
if data != "" {
if err := json.Unmarshal([]byte(data), &cfg); err != nil {
dlog.Printf("Error unmarshaling project data: %v", err)
}
}
if cfg.Default != nil {
dlog.Printf("setting default project to %v", cfg.Default)
config.DefaultProjectID = *cfg.Default
if err := alfred.SaveJSON(configFile, &config); err != nil {
return "Error saving config", err
}
return fmt.Sprintf(`Set default project to %d`, cfg.Default), nil
}
if cfg.ToCreate != nil {
dlog.Printf("creating project %v", cfg.ToCreate)
var project toggl.Project
if project, err = createProject(cfg.ToCreate); err != nil {
return
}
return fmt.Sprintf(`Created project "%s"`, project.Name), nil
}
if cfg.ToUpdate != nil {
dlog.Printf("updating project %v", cfg.ToUpdate)
var project toggl.Project
if project, err = updateProject(cfg.ToUpdate); err != nil {
return
}
return fmt.Sprintf(`Updated project "%s"`, project.Name), nil
}
return "Unrecognized input", nil
}
// support -------------------------------------------------------------------
type projectCfg struct {
Project *int `json:"project,omitempty"`
Default *int `json:"default,omitempty"`
ToCreate *createProjectMessage `json:"create,omitempty"`
ToUpdate *toggl.Project `json:"update,omitempty"`
}
type createProjectMessage struct {
Name string
WID int
}
func createProject(msg *createProjectMessage) (project toggl.Project, err error) {
session := toggl.OpenSession(config.APIKey)
if msg.WID == 0 {
msg.WID = cache.Account.Workspaces[0].ID
}
if project, err = session.CreateProject(msg.Name, msg.WID); err == nil {
cache.Account.Projects = append(cache.Account.Projects, project)
if err := alfred.SaveJSON(cacheFile, &cache); err != nil {
dlog.Printf("Error saving cache: %s\n", err)
}
}
return
}
func updateProject(p *toggl.Project) (project toggl.Project, err error) {
session := toggl.OpenSession(config.APIKey)
if project, err = session.UpdateProject(*p); err != nil {
return
}
adata := &cache.Account
for i, p := range adata.Projects {
if p.ID == project.ID {
adata.Projects[i] = project
if err := alfred.SaveJSON(cacheFile, &cache); err != nil {
dlog.Printf("Error saving cache: %v\n", err)
}
break
}
}
return
}
func projectItems(project toggl.Project, arg string) (items []alfred.Item, err error) {
if alfred.FuzzyMatches("name:", arg) {
item := alfred.Item{}
_, name := alfred.SplitCmd(arg)
if name != "" {
updateEntry := project
updateEntry.Name = name
item.Title = fmt.Sprintf("Change name to '%s'", name)
item.Subtitle = "Name: " + project.Name
item.Arg = &alfred.ItemArg{
Keyword: "projects",
Data: alfred.Stringify(projectCfg{ToUpdate: &updateEntry}),
}
} else {
item.Title = "Name: " + project.Name
item.Autocomplete = "Name: "
}
items = append(items, item)
}
if project.ID != config.DefaultProjectID {
if alfred.FuzzyMatches("Make default", arg) {
c := config
c.DefaultProjectID = project.ID
items = append(items, alfred.Item{
Title: "Make default",
Subtitle: "Make this the default project",
Autocomplete: "Make default",
Arg: &alfred.ItemArg{
Keyword: "options",
Mode: alfred.ModeDo,
Data: alfred.Stringify(c),
},
})
}
} else {
if alfred.FuzzyMatches("Clear default", arg) {
c := config
c.DefaultProjectID = 0
items = append(items, alfred.Item{
Title: "Clear default",
Subtitle: "Clear the default project",
Autocomplete: "Clear default",
Arg: &alfred.ItemArg{
Keyword: "options",
Mode: alfred.ModeDo,
Data: alfred.Stringify(c),
},
})
}
}
if isWorkspacePremium(project.Wid) && project.Billable != nil &&
alfred.FuzzyMatches("billable:", arg) {
var item alfred.Item
updateEntry := project
newBillable := !*project.Billable
updateEntry.Billable = &newBillable
item.Title = "Billable"
item.Subtitle = "Update default project's billable flag"
item.Autocomplete = "Billable: " + alfred.Stringify(project.Billable)
item.Arg = &alfred.ItemArg{
Keyword: "projects",
Mode: alfred.ModeDo,
Data: alfred.Stringify(projectCfg{ToUpdate: &updateEntry}),
}
item.AddCheckBox(*project.Billable)
items = append(items, item)
}
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{Project: &project.ID}),
},
})
}
return
}