-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcmd_update.go
237 lines (187 loc) · 4.61 KB
/
cmd_update.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
//
// Copyright (c) 2017 Dean Jackson <deanishe@deanishe.net>
//
// MIT Licence. See http://opensource.org/licenses/MIT
//
// Created on 2017-11-25
//
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"time"
aw "github.com/deanishe/awgo"
"github.com/deanishe/awgo/util"
"github.com/pkg/errors"
)
// Check if a new version of the workflow is available.
func doUpdateWorkflow() error {
wf.Configure(aw.TextErrors(true))
log.Print("[update] checking for new version of workflow…")
return wf.CheckForUpdate()
}
// Fetch and cache list of calendars.
func doUpdateCalendars() error {
var (
acc *Account
err error
)
wf.Configure(aw.TextErrors(true))
log.Print("[update] reloading calendars…")
if len(accounts) == 0 {
log.Print("[update] no Google accounts configured")
}
for _, acc = range accounts {
if err = acc.FetchCalendars(); err != nil {
return err
}
if !util.PathExists(acc.IconPath()) {
if err := download(acc.AvatarURL, acc.IconPath()); err != nil {
return errors.Wrap(err, "fetch account avatar")
}
}
log.Printf("[update] %d calendar(s) in account %q", len(acc.Calendars), acc.Name)
}
return nil
}
// Fetch events for a specified date.
func doUpdateEvents() error {
wf.Configure(aw.TextErrors(true))
var (
name = fmt.Sprintf("events-%s.json", opts.StartTime.Format(timeFormat))
cals []*Calendar
events []*Event
err error
)
log.Printf("[update] fetching events for %s ...", opts.StartTime.Format(timeFormat))
if err := clearOldFiles(); err != nil {
log.Printf("[update] ERR: delete old cache files: %v", err)
}
if cals, err = activeCalendars(); err != nil {
return err
}
if len(accounts) == 0 {
log.Print("[update] no Google accounts configured")
return nil
}
if len(cals) == 0 {
log.Print("[update] no active calendars")
return nil
}
log.Printf("[update] %d active calendar(s)", len(cals))
// Fetch events in parallel
var (
ch = make(chan *Event)
wg sync.WaitGroup
wanted = make(map[string]bool, len(cals)) // IDs of calendars to update
)
for _, c := range cals {
wanted[c.ID] = true
}
wg.Add(len(cals))
for _, acc := range accounts {
for _, c := range acc.Calendars {
if _, ok := wanted[c.ID]; !ok {
continue
}
go func(c *Calendar, acc *Account) {
defer wg.Done()
evs, err := acc.FetchEvents(c, opts.StartTime)
if err != nil {
log.Printf("[update] ERR: fetching events for calendar %q: %v", c.Title, err)
return
}
log.Printf("[update] %d event(s) in calendar %q", len(evs), c.Title)
for _, e := range evs {
ch <- e
}
}(c, acc)
}
}
// Close channel when all goroutines are done
go func() {
wg.Wait()
close(ch)
}()
colours := map[string]bool{}
for e := range ch {
log.Printf("[update] %s", e)
events = append(events, e)
colours[e.Colour] = true
}
sort.Sort(EventsByStart(events))
if err := wf.Cache.StoreJSON(name, events); err != nil {
return err
}
// Ensure icons exist in all colours
for clr := range colours {
_ = ColouredIcon(iconCalendar, clr)
_ = ColouredIcon(iconMap, clr)
}
return nil
}
// Remove events-* files and icons older than two weeks.
func clearOldFiles() error {
var (
cutoff = time.Now().AddDate(0, 0, -14)
dirs = []string{}
)
err := filepath.Walk(wf.CacheDir(), func(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if fi.Name() == "_aw" && fi.IsDir() {
return filepath.SkipDir
}
if fi.IsDir() {
dirs = append(dirs, path)
return nil
}
if fi.ModTime().After(cutoff) {
return nil
}
ext := filepath.Ext(path)
if (strings.HasPrefix(fi.Name(), "events-") && ext == ".json") || ext == ".png" {
if err := os.Remove(path); err != nil {
log.Printf("[cache] ERR: delete %q: %v", path, err)
return err
}
}
return nil
})
if err != nil {
return err
}
// Remove empty directories. Sort in reverse order so sub-directories are
// before their parents.
sort.Sort(sort.Reverse(sort.StringSlice(dirs)))
Outer:
for _, dir := range dirs {
infos, err := ioutil.ReadDir(dir)
if err != nil {
log.Printf("[cache] ERR: open dir %s: %v", dir, err)
return err
}
// ignore dotfiles
for _, fi := range infos {
if strings.HasPrefix(fi.Name(), ".") {
continue
}
// rel, _ := filepath.Rel(wf.CacheDir(), dir)
// log.Printf("[cache] %s -- %d item(s)", rel, len(infos))
continue Outer
}
if err := os.RemoveAll(dir); err != nil {
log.Printf("[cache] ERR: delete dir %s: %v", dir, err)
return err
}
log.Printf("[cache] deleted dir: %s", util.PrettyPath(dir))
}
return nil
}