-
Notifications
You must be signed in to change notification settings - Fork 13
/
cmd_config.go
301 lines (251 loc) · 6.53 KB
/
cmd_config.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
//
// Copyright (c) 2017 Dean Jackson <deanishe@deanishe.net>
//
// MIT Licence. See http://opensource.org/licenses/MIT
//
// Created on 2017-11-25
//
package main
import (
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
aw "github.com/deanishe/awgo"
"github.com/pkg/errors"
)
// doConfig shows configuration options.
func doConfig() error {
wf.Var("CALENDAR_APP", "") // Open links in default browser, not CALENDAR_APP
if opts.Query == "" {
wf.Configure(aw.SuppressUIDs(true))
}
if len(accounts) > 0 {
wf.NewItem("Active Calendars…").
Subtitle("Turn calendars on/off").
UID("calendars").
Icon(iconCalendars).
Valid(true).
Var("action", "calendars")
wf.NewItem("Add Account…").
Subtitle("Action this item to add a Google account").
UID("add-account").
Autocomplete("workflow:login").
Icon(iconAccountAdd)
} else {
wf.NewItem("No Accounts Configured").
Subtitle("Action this item to add a Google account").
UID("add-account").
Autocomplete("workflow:login").
Icon(aw.IconWarning)
}
for _, acc := range accounts {
it := wf.NewItem(acc.Name).
Subtitle("⌥↩ to remove account / ⌘↩ to re-authenticate").
UID(acc.Name).
Arg(acc.Name).
Valid(false).
Icon(acc.Icon())
it.NewModifier("opt").
Subtitle("Remove account").
Valid(true).
Var("action", "logout").
Var("account", acc.Name)
it.NewModifier("cmd").
Subtitle("Re-authenticate account with read-write permission").
Valid(true).
Var("action", "reauth").
Var("account", acc.Name)
}
var (
name = "Google Maps"
other = "Apple Maps"
icon = iconGoogleMaps
arg = "apple"
)
if opts.UseAppleMaps {
name, other = other, name
icon = iconAppleMaps
arg = "google"
}
wf.NewItem("Open Locations in "+name).
Subtitle("Toggle this setting to use "+other).
UID("location").
Arg(arg).
Valid(true).
Icon(icon).
Var("action", "set").
Var("key", "maps").
Var("value", arg)
if wf.UpdateAvailable() {
wf.NewItem("An Update is Available").
Subtitle("A newer version of the workflow is available").
UID("update").
Autocomplete("workflow:update").
Icon(iconUpdateAvailable).
Valid(false)
} else {
wf.NewItem("Workflow is up to Date").
Subtitle("Action to force update check").
UID("update").
Icon(iconUpdateOK).
Valid(true).
Var("action", "update")
}
wf.NewItem("Open Documentation").
Subtitle("Open workflow README in your browser").
UID("docs").
Arg(readmeURL).
Valid(true).
Icon(iconDocs).
Var("action", "open")
wf.NewItem("Get Help").
Subtitle("Open alfredforum.com thread in your browser").
UID("forum").
Arg(forumURL).
Valid(true).
Icon(iconHelp).
Var("action", "open")
wf.NewItem("Report Issue").
Subtitle("Open GitHub issues in your browser").
UID("issues").
Arg(helpURL).
Valid(true).
Icon(iconIssue).
Var("action", "open")
wf.NewItem("Clear Cached Calendars & Events").
Subtitle("Remove cached list of calendars and events").
UID("clear").
Icon(iconDelete).
Valid(true).
Var("action", "clear")
if opts.Query != "" {
wf.Filter(opts.Query)
}
wf.WarnEmpty("No Matches", "Try a different query")
wf.SendFeedback()
return nil
}
// doToggle turns a calendar on or off.
func doToggle() error {
IDs, err := activeCalendarIDs()
if err != nil && err != errNoActive {
return err
}
if err == errNoActive {
IDs = map[string]bool{}
}
if IDs[opts.CalendarID] {
log.Printf("deactivating calendar %s ...", opts.CalendarID)
delete(IDs, opts.CalendarID)
} else {
log.Printf("activating calendar %s ...", opts.CalendarID)
IDs[opts.CalendarID] = true
}
active := []string{}
for ID := range IDs {
active = append(active, ID)
}
if err := wf.Cache.StoreJSON("active.json", active); err != nil {
return errors.Wrap(err, "save active calendar list")
}
// calendars have changed, so delete cached schedules
return clearEvents()
}
// Re-authenticate specified account.
func doReauth() error {
wf.Configure(aw.TextErrors(true))
log.Printf("[reauth] account=%q", opts.Account)
for _, acc := range accounts {
if acc.Name == opts.Account {
acc.Token = nil
if err := acc.Save(); err != nil {
return errors.Wrap(err, "reauth: save account")
}
// retrieve calendar list to trigger authentication
if err := acc.FetchCalendars(); err != nil {
return errors.Wrap(err, "reauth: fetch calendars")
}
}
}
return nil
}
// doLogout removes an account.
func doLogout() error {
wf.Configure(aw.TextErrors(true))
log.Printf("[logout] account=%q", opts.Account)
deleteMe := map[string]bool{}
for _, acc := range accounts {
if acc.Name == opts.Account {
for _, cal := range acc.Calendars {
deleteMe[cal.ID] = true
}
if err := wf.Cache.Store(acc.CacheName(), nil); err != nil {
return errors.Wrap(err, "delete account file")
}
if err := os.Remove(acc.IconPath()); err != nil && !os.IsNotExist(err) {
return errors.Wrap(err, "delete account avatar")
}
log.Printf("[logout] removed account %q", opts.Account)
}
}
var (
active []string
IDs map[string]bool
err error
)
// Remove active calendars belonging to account
if IDs, err = activeCalendarIDs(); err != nil && err != errNoActive {
return errors.Wrap(err, "get active calendars")
}
// No active calendars to change
if err == errNoActive || len(deleteMe) == 0 {
return nil
}
for id := range IDs {
if !deleteMe[id] {
active = append(active, id)
}
}
if err := wf.Cache.StoreJSON("active.json", active); err != nil {
return errors.Wrap(err, "save active calendar list")
}
// delete cached schedules now calendars have changed
return clearEvents()
}
// doClear removes cached calendars and events.
func doClear() error {
log.Print("clearing cached calendars and events…")
wf.Configure(aw.TextErrors(true))
if err := clearEvents(); err != nil {
return errors.Wrap(err, "clear cached data")
}
for _, acc := range accounts {
acc.Calendars = []*Calendar{}
if err := acc.Save(); err != nil {
return errors.Wrap(err, "remove account calendars")
}
}
return nil
}
// delete cached events.
func clearEvents() error {
var (
infos []os.FileInfo
err error
)
if infos, err = ioutil.ReadDir(wf.CacheDir()); err != nil {
return errors.Wrap(err, "read cache directory")
}
for _, fi := range infos {
name := fi.Name()
if strings.HasPrefix(name, "events-") && strings.HasSuffix(name, ".json") {
if err = os.Remove(filepath.Join(wf.CacheDir(), name)); err != nil {
return errors.Wrap(err, "delete events cache file")
}
log.Printf("[cache] deleted %q", name)
}
}
return err
}