-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
336 lines (277 loc) · 9.7 KB
/
main.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//nolint:lll // we accept long lines for struct field tags used by kong
package main
import (
"database/sql"
"errors"
"fmt"
"os"
"strconv"
"time"
"github.com/alecthomas/kong"
"github.com/dgsb/configlite"
"github.com/sirupsen/logrus"
"github.com/dgsb/tt/internal/db"
itime "github.com/dgsb/tt/internal/time"
)
var (
errInvalidParameter = fmt.Errorf("invalid parameter")
)
const (
appName = "github.com/dgsb/tt"
)
type CommonConfig struct {
Database string `name:"db" type:"file" default:"${home}/.tt.db" help:"the sqlite database to use for application data"`
}
type StartCmd struct {
At itime.Time `help:"specify the start timestamp in RFC3339 format" group:"time" xor:"time"`
Ago time.Duration `help:"specify the start timestamp as a duration in the past" group:"time" xor:"time"`
Tags []string `arg:"" optional:"" help:"the value to tag the interval with"`
}
func (cmd *StartCmd) Run(tt *db.TimeTracker) error {
startTime := time.Now()
if !cmd.At.Time().IsZero() {
startTime = cmd.At.Time()
} else if cmd.Ago != 0 {
startTime = time.Now().Add(-cmd.Ago)
}
// Stop the current interval before opening a new one
if err := tt.StopAt(startTime); err != nil && !errors.Is(err, sql.ErrNoRows) {
return fmt.Errorf("cannot stop currently opened interval: %w", err)
}
if err := tt.Start(startTime, cmd.Tags); err != nil {
return fmt.Errorf("cannot start a new opened interval: %w", err)
}
return nil
}
type StopCmd struct {
At itime.Time `help:"specify the stop timestamp in RFC3339 format" group:"time" xor:"time"`
Ago time.Duration `help:"specify the stop timestamp as a duration in the past" group:"time" xor:"time"`
For time.Duration `help:"specify the stop timestamp as the wanted duration for closed interval" group:"time" xor:"time"`
}
func (cmd *StopCmd) Run(tt *db.TimeTracker) error {
if cmd.For != 0 {
if err := tt.StopFor(cmd.For); err != nil {
return fmt.Errorf("cannot stop a currently opened interval: %w", err)
}
return nil
}
stopTime := time.Now()
if !cmd.At.Time().IsZero() {
stopTime = cmd.At.Time()
} else if cmd.Ago != 0 {
stopTime = time.Now().Add(-cmd.Ago)
}
if err := tt.StopAt(stopTime); err != nil {
return fmt.Errorf("cannot stop a currently opened interval: %w", err)
}
return nil
}
type ListCmd struct {
At itime.Time `help:"another starting point for the required time period instead of now"`
Tag string `help:"a tag to output filter on"`
Period string `arg:"" help:"a logical description of the time period to look at" default:":day" enum:":week,:day,:month,:year"`
}
func (cmd *ListCmd) Run(tt *db.TimeTracker) error {
startTime := cmd.At.Time()
if startTime.IsZero() {
startTime = time.Now()
}
var stopTime time.Time
switch cmd.Period {
case ":day":
year, month, day := startTime.Date()
startTime = time.Date(year, month, day, 0, 0, 0, 0, time.Local)
stopTime = time.Date(year, month, day+1, 0, 0, 0, 0, time.Local)
case ":week":
year, month, day := startTime.Date()
weekday := startTime.Weekday()
if weekday == time.Sunday {
weekday = time.Saturday + 1
}
startTime = time.Date(year, month, day-int(weekday-time.Monday), 0, 0, 0, 0, time.Local)
stopTime = time.Date(year, month, day+1+int(time.Saturday+1-weekday), 0, 0, 0, 0, time.Local)
case ":month":
year, month, _ := startTime.Date()
startTime = time.Date(year, month, 1, 0, 0, 0, 0, time.Local)
stopTime = time.Date(year, month+1, 1, 0, 0, 0, 0, time.Local)
case ":year":
year, _, _ := startTime.Date()
startTime = time.Date(year, time.January, 1, 0, 0, 0, 0, time.Local)
stopTime = time.Date(year+1, time.January, 1, 0, 0, 0, 0, time.Local)
default:
return fmt.Errorf("%w: time range not implemented %s", errInvalidParameter, cmd.Period)
}
taggedIntervals, err := tt.List(startTime, stopTime)
if err != nil {
return fmt.Errorf("cannot list recorded interval: %w", err)
}
filteredTaggedIntervals := make([]db.TaggedInterval, 0, len(taggedIntervals))
if cmd.Tag == "" {
filteredTaggedIntervals = taggedIntervals
} else {
for _, itv := range taggedIntervals {
for _, t := range itv.Tags {
if t == cmd.Tag {
filteredTaggedIntervals = append(filteredTaggedIntervals, itv)
break
}
}
}
}
return FlatReport(filteredTaggedIntervals, os.Stdout)
}
type DeleteCmd struct {
IDs []string `arg:"" name:"ids" help:"the ids of the intervals to delete"`
}
func (cmd *DeleteCmd) Run(tt *db.TimeTracker) error {
for _, id := range cmd.IDs {
if err := tt.Delete(id); err != nil {
return fmt.Errorf("cannot delete interval %s: %w", id, err)
}
}
return nil
}
type TagCmd struct {
ID string `arg:"" help:"the interval id to tag"`
Tags []string `arg:"" help:"values to tag the interval with"`
}
func (cmd *TagCmd) Run(tt *db.TimeTracker) error {
if err := tt.Tag(cmd.ID, cmd.Tags); err != nil {
return fmt.Errorf("cannot tag interval %s with %s: %w", cmd.ID, cmd.Tags, err)
}
return nil
}
type UntagCmd struct {
ID string `arg:"" help:"the interval id to untag"`
Tags []string `arg:"" help:"the tag to remove from the interval"`
}
func (cmd *UntagCmd) Run(tt *db.TimeTracker) error {
if err := tt.Untag(cmd.ID, cmd.Tags); err != nil {
return fmt.Errorf("cannot untag %s from %s: %w", cmd.ID, cmd.Tags, err)
}
return nil
}
type CurrentCmd struct {
}
func (cmd *CurrentCmd) Run(tt *db.TimeTracker) error {
interval, err := tt.Current()
if err != nil {
return fmt.Errorf("cannot retrieve current interval: %w", err)
}
if interval != nil {
return FlatReport([]db.TaggedInterval{*interval}, os.Stdout)
}
return nil
}
type ContinueCmd struct {
ID string `long:"id" help:"specify an interval ID to continue"`
}
func (cmd *ContinueCmd) Run(tt *db.TimeTracker) error {
if err := tt.Continue(time.Now(), cmd.ID); err != nil {
return fmt.Errorf("cannot continue a previously closed interval: %w", err)
}
return nil
}
type VacuumCmd struct {
Since time.Duration `required:"" help:"specify the duration to delete data before" group:"time" xor:"time"`
Before time.Time `required:"" help:"specify the timestamp to delete data before" group:"time" xor:"time"`
}
func (cmd *VacuumCmd) Run(tt *db.TimeTracker) error {
checkpoint := cmd.Before
if checkpoint.IsZero() {
checkpoint = time.Now().Add(-cmd.Since)
}
if err := tt.Vacuum(checkpoint); err != nil {
return fmt.Errorf("cannot vacuum the database: %w", err)
}
return nil
}
type RecordCmd struct {
Start itime.Time `arg:"" help:"the start time interval of the record"`
Stop itime.Time `arg:"" help:"the stop time interval of the record"`
Tags []string `arg:"" help:"the list of tags to attach to this new interval"`
}
func (cmd *RecordCmd) Run(tt *db.TimeTracker) error {
if err := tt.Start(cmd.Start.Time(), cmd.Tags); err != nil {
return fmt.Errorf("cannot register new start interval: %w", err)
}
if err := tt.StopAt(cmd.Stop.Time()); err != nil {
return fmt.Errorf("cannot register new stop interval: %w", err)
}
return nil
}
type SyncCmd struct {
Login string `long:"login" short:"l" help:"remote database user login"`
Password string `long:"password" help:"remote database password" env:"TT_SYNC_PASSWORD"`
Hostname string `long:"host" help:"remote database host name"`
Port string `long:"port" short:"p" help:"remote database connection port"`
DatabaseName string `long:"dbname" help:"remote database name"`
}
func (cmd *SyncCmd) Run(tt *db.TimeTracker) error {
repo, err := configlite.New(configlite.DefaultConfigurationFile())
if err != nil {
return fmt.Errorf("cannot open configuration repository: %w", err)
}
if cmd.Login == "" {
cmd.Login, err = repo.GetConfig(appName, "syncer_login")
}
if cmd.Password == "" && err == nil {
cmd.Password, err = repo.GetConfig(appName, "syncer_password")
}
if cmd.Hostname == "" && err == nil {
cmd.Hostname, err = repo.GetConfig(appName, "syncer_hostname")
}
if cmd.Port == "" && err == nil {
cmd.Port, err = repo.GetConfig(appName, "syncer_port")
}
var portInt int
if err == nil {
portInt, err = strconv.Atoi(cmd.Port)
}
if cmd.DatabaseName == "" && err == nil {
cmd.DatabaseName, err = repo.GetConfig(appName, "syncer_databasename")
}
if err == nil {
err = tt.Sync(db.SyncerConfig{
Login: cmd.Login,
Password: cmd.Password,
Hostname: cmd.Hostname,
Port: portInt,
DatabaseName: cmd.DatabaseName,
})
}
return err
}
func main() {
homeDir, err := os.UserHomeDir()
if err != nil {
logrus.WithError(err).Fatal("cannot retrieve user home directory")
}
var CLI struct {
CommonConfig
Continue ContinueCmd `cmd:"" help:"start a new interval with same tags as the last closed one"`
Current CurrentCmd `default:"1" cmd:"" help:"return the current opened interval"`
Delete DeleteCmd `cmd:"" help:"delete a registered interval"`
List ListCmd `cmd:"" help:"list intervals"`
Record RecordCmd `cmd:"" help:"record a new closed interval with it tags"`
Start StartCmd `cmd:"" help:"start tracking a new time interval"`
Stop StopCmd `cmd:"" help:"stop tracking the current opened interval"`
Sync SyncCmd `cmd:"" help:"synchronise with remote central database"`
Tag TagCmd `cmd:"" help:"tag an interval with given values"`
Untag UntagCmd `cmd:"" help:"remove tags from an interval"`
Vacuum VacuumCmd `cmd:"" help:"hard delete old soft deleted data"`
}
ctx := kong.Parse(&CLI, kong.Vars{"home": homeDir})
tt, err := db.New(CLI.CommonConfig.Database)
if err != nil {
logrus.WithError(err).Fatal("cannot setup application database")
}
defer func() {
if err := tt.Close(); err != nil {
logrus.WithError(err).Fatal("cannot close TimeTracker object")
}
}()
if err := ctx.Run(tt); err != nil {
logrus.WithError(err).WithField("command", ctx.Command).Fatal("cannot run command")
}
}