forked from deanishe/alfred-gcal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_update.go
133 lines (117 loc) · 2.87 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
//
// Copyright (c) 2017 Dean Jackson <[email protected]>
//
// 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"
)
// Check if a new version of the workflow is available.
func doUpdateWorkflow() error {
log.Print("[update] checking for new version of workflow…")
wf.TextErrors = true
return wf.CheckForUpdate()
}
// Fetch and cache list of calendars.
func doUpdateCalendars() error {
log.Print("[update] reloading calendars…")
wf.TextErrors = true
cals, err := FetchCalendars(auth)
if err != nil {
return fmt.Errorf("couldn't load calendars: %v", err)
}
return wf.Cache.StoreJSON("calendars.json", cals)
}
// Fetch events for a specified date.
func doUpdateEvents() error {
log.Printf("[update] fetching events for %s…", startTime.Format(timeFormat))
wf.TextErrors = true
var (
events = []*Event{}
name = fmt.Sprintf("events-%s.json", startTime.Format(timeFormat))
)
if err := clearOldEvents(); err != nil {
log.Printf("[update/error] problem deleting old cache files: %v", err)
}
cals, err := activeCalendars()
if err != nil {
log.Printf("[update/error] couldn't load active calendars: %v", err)
return err
}
log.Printf("[update] %d active calendar(s)", len(cals))
// Fetch events in parallel
var (
ch = make(chan *Event)
wg sync.WaitGroup
)
wg.Add(len(cals))
for _, c := range cals {
go func(c *Calendar) {
defer wg.Done()
evs, err := FetchEvents(auth, c, startTime)
if err != nil {
log.Printf("[update/error] fetching events for calendar \"%s\": %v", c.Title, err)
return
}
log.Printf("[update] %d event(s) in calendar \"%s\"", len(evs), c.Title)
for _, e := range evs {
ch <- e
// events = append(events, e)
}
}(c)
}
// Close channel when all goroutines are done
go func() {
wg.Wait()
close(ch)
}()
for e := range ch {
log.Printf("[update] %s", e)
events = append(events, e)
}
sort.Sort(EventsByStart(events))
if err := wf.Cache.StoreJSON(name, events); err != nil {
return err
}
return nil
}
// doUpdateIcons fetches queued icons.
func doUpdateIcons() error {
gen, err := NewIconGenerator(cacheDirIcons, aw.IconWorkflow)
if err != nil {
return err
}
return gen.Download()
}
// Remove events-* files that haven't been updated in a week.
func clearOldEvents() error {
files, err := ioutil.ReadDir(wf.CacheDir())
if err != nil {
return err
}
for _, fi := range files {
name := fi.Name()
cutoff := time.Now().AddDate(0, 0, -7)
if strings.HasPrefix(name, "events-") && strings.HasSuffix(name, ".json") {
if fi.ModTime().Before(cutoff) {
p := filepath.Join(wf.CacheDir(), name)
if err := os.Remove(p); err != nil {
log.Printf("[ERROR] couldn't delete file \"%s\": %v", p, err)
}
}
}
}
return nil
}