-
Notifications
You must be signed in to change notification settings - Fork 1
/
gomato.go
190 lines (152 loc) · 4.86 KB
/
gomato.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
package gomato
import (
"io/ioutil"
"log"
"os"
"strings"
"time"
gcache "github.com/patrickmn/go-cache"
"github.com/pkg/errors"
"github.com/rs/xid"
)
// PomodoroManager represents the methods necessary to implement gomato for easy testing
type PomodoroManager interface {
StartWithTime(uID string, start time.Time, duration time.Duration, actions ...func()) (string, error)
Start(uID string, duration time.Duration, actions ...func()) (string, error)
Resume(uID string) error
Pause(uID string) error
Stop(uID string) error
}
// TimeKeeper represents the necessary components to manage pomodoros
type TimeKeeper struct {
cache *gcache.Cache
logger *log.Logger
}
// NewDefaultTimeKeeper instantiates a TimeKeeper with default logging and cache options
func NewDefaultTimeKeeper() *TimeKeeper {
return NewTimeKeeper(log.New(os.Stdout, "GOMATO: ", log.Lshortfile), gcache.New(-1, -1))
}
// NewTimeKeeper instantiates a TimeKeeper object
func NewTimeKeeper(l *log.Logger, c *gcache.Cache) *TimeKeeper {
if c == nil {
c = gcache.New(-1, -1) // cache with no expiration, no cleanup
}
if l == nil { // assume no logging is wanted
l = log.New(ioutil.Discard, "", 0)
}
return &TimeKeeper{cache: c, logger: l}
}
type pomodoro struct {
startTime time.Time
currentDuration time.Duration
timer *time.Timer
}
// StartWithTime begins a new pomodoro
// A user identifier should be passed through, but if it is not then it will be generated and returned
func (t *TimeKeeper) StartWithTime(uID string, start time.Time, duration time.Duration, actions ...func()) (string, error) {
if uID == "" {
t.logger.Print("[INFO] User ID not provided, setting ID")
uID = xid.New().String()
}
if start.IsZero() {
t.logger.Print("[INFO] Time is zero, setting to current time")
start = time.Now()
}
if duration.String() == "0s" { // zero duration
duration = 25 * time.Minute
}
p := pomodoro{
startTime: start,
currentDuration: duration,
timer: time.AfterFunc(duration, t.runActions(uID, actions...)),
}
t.cache.SetDefault(uID, &p)
return uID, nil
}
// Start begins a new pomodoro without the need to pass in a start time
// A user identifier should be passed through, but if it is not then it will be generated and returned
func (t *TimeKeeper) Start(uID string, duration time.Duration, actions ...func()) (string, error) {
if uID == "" {
t.logger.Print("[INFO] User ID not provided, setting ID")
uID = xid.New().String()
}
start := time.Now()
if duration.String() == "0s" { // zero duration
duration = 25 * time.Minute
}
p := pomodoro{
startTime: start,
currentDuration: duration,
timer: time.AfterFunc(duration, t.runActions(uID, actions...)),
}
t.cache.SetDefault(uID, &p)
return uID, nil
}
// Pause pauses a timer with the given user ID
func (t *TimeKeeper) Pause(uID string) error {
if strings.TrimSpace(uID) == "" {
t.logger.Print("[ERROR] No user ID provided")
return errors.New("no user ID provided")
}
pd, ok := t.cache.Get(uID)
if !ok {
t.logger.Print("[INFO] No timer associated with given user")
return errors.New("no timer associated with given user")
}
pomData, ok := pd.(*pomodoro)
if !ok {
t.logger.Print("[ERROR] Error parsing pomodoro data")
return errors.New("failed to cast cached data as pomodoro")
}
_ = pomData.timer.Stop()
pomData.currentDuration = pomData.currentDuration - time.Since(pomData.startTime)
return nil
}
// Resume resumes a paused timer with the given user ID
func (t *TimeKeeper) Resume(uID string) error {
if strings.TrimSpace(uID) == "" {
t.logger.Print("[ERROR] No user ID provided")
return errors.New("no user ID provided")
}
pd, ok := t.cache.Get(uID)
if !ok {
t.logger.Print("[INFO] No timer associated with given user")
return errors.New("no timer associated with given user")
}
pomData, ok := pd.(*pomodoro)
if !ok {
t.logger.Print("[ERROR] Error parsing pomodoro data")
return errors.New("failed to cast cached data as pomodoro")
}
_ = pomData.timer.Reset(pomData.currentDuration)
return nil
}
// Stop stops a timer (running or paused) and deletes it from the cache
func (t *TimeKeeper) Stop(uID string) error {
if strings.TrimSpace(uID) == "" {
t.logger.Print("[ERROR] No user ID provided")
return errors.New("no user ID provided")
}
pd, ok := t.cache.Get(uID)
if !ok {
t.logger.Print("[INFO] No timer associated with given user")
return errors.New("no timer associated with given user")
}
pomData, ok := pd.(*pomodoro)
if !ok {
t.logger.Print("[ERROR] Error parsing pomodoro data")
return errors.New("failed to cast cached data as pomodoro")
}
_ = pomData.timer.Stop()
t.cache.Delete(uID)
return nil
}
func (t *TimeKeeper) runActions(userID string, actions ...func()) func() {
return func() {
t.logger.Print("[INFO] Running finish timer actions")
for _, action := range actions {
action()
}
t.cache.Delete(userID)
}
}