-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
405 lines (338 loc) · 11 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"regexp"
"strings"
"text/template"
"time"
"github.com/MakeNowJust/heredoc/v2"
"github.com/mgutz/ansi"
"github.com/tj/go-naturaldate"
"github.com/maelvls/clockidup/logutil"
)
const (
confPath = ".config/clockidup.yml"
layoutISO = "2006-01-02"
)
var (
tokenFlag = flag.String("token", "", "The Clockify API token.")
debugFlag = flag.Bool("debug", false, "Show debug output, including the HTTP requests.")
onlyBillable = flag.Bool("billable", false, "Only print the entries that are billable.")
// The 'version' var is set during build, using something like:
// go build -ldflags"-X main.version=$(git describe --tags)".
// Note: "version", is set automatically by goreleaser.
version = ""
)
const help = `{{- if .Extended -}}
{{ section "NAME" }}
clockidup helps you generating your standup entry using the time entries from
{{ url "https://clockify.me" }}.
{{ end -}}
{{ section "SYNOPSYS" }}
clockidup login
clockidup [--billable] {{ url "DAY" }}
clockidup version
where {{ url "DAY" }} is of the form:
today
yesterday
thursday
"2 days ago"
2021-01-28
{{- if .Extended }}
{{ section "HOW TO USE IT" }}
To start, log in:
{{ cmd "clockidup login" }}
Each "project" in Clockify will show up as a prefix of your time entries. For
example, imagining that you have an entry that you created with clockify-cli:
clockify-cli in prod/cert-manager --when=now "#3444: continue dataforcertificate unit test"
clockify-cli out
where {{ url "prod/cert-manager" }} is a Clockify project. clockidup will show:
{{ cmd "clockidup today" }}
{{ out "Friday:" }}
{{ out "- [1.2] prod/cert-manager: #3444: continue dataforcertificate unit test" }}
<---> <---------------> <------------------------------------------>
duration project entry text
You may only want the "billable" entries to be displayed by clockidup:
{{ cmd "clockidup --billable today" }}
You can also use Clockify tasks. Like projects, there are displayed as a prefix
to the entry text:
{{ out "- [1.2] prod/cert-manager: big refactoring for the v2: rm large defers" }}
<---> <---------------> <------------------------> <-------------->
duration project task entry text
{{ section "CONFIG FILE" }}
The auth token is saved to {{ url "~/.config/clockidup.yml" }}. The file looks
like this:
token: your-clockify-auth-token
{{- end }}
{{- if not .Extended }}
More help is available with the command {{ yel "clockidup help" }}.
{{- end }}
{{ section "OPTIONS" }}
`
func main() {
printHelp := func(extended bool) func() {
return func() {
t, err := template.New("help").Funcs(map[string]interface{}{
"section": ansi.ColorFunc("black+hb"),
"url": ansi.ColorFunc("white+u"),
"grey": ansi.ColorFunc("white+d"),
"yel": ansi.ColorFunc("yellow"),
"cmd": func(cmd string) string {
return ansi.ColorFunc("white+d")("% ") + ansi.ColorFunc("yellow+b")(cmd)
},
"out": ansi.ColorFunc("white+d"),
}).Parse(help)
if err != nil {
log.Fatal(err)
}
err = t.Execute(flag.CommandLine.Output(), struct{ Extended bool }{
Extended: extended,
})
if err != nil {
log.Fatal(err)
}
flag.PrintDefaults()
}
}
flag.Usage = printHelp(false)
flag.Parse()
if *debugFlag {
logutil.EnableDebug = true
}
err := Run(*tokenFlag, printHelp)
if err != nil {
logutil.Errorf(err.Error())
os.Exit(1)
}
}
func Run(tokenFlag string, printHelp func(bool) func()) error {
conf, err := loadConfig(confPath)
if err != nil {
return fmt.Errorf("could not load config: %s", err)
}
var day time.Time
switch flag.Arg(0) {
case "login":
conf, err := askToken(conf)
if err != nil {
return fmt.Errorf("login failed: %s", err)
}
logutil.Infof("you are logged in!")
err = saveConfig(confPath, conf)
if err != nil {
return fmt.Errorf("saving configuration: %s", err)
}
logutil.Debugf("config: %+v", conf)
return nil
case "version":
if version == "" {
version, err = versionUsingGo()
if err != nil {
return fmt.Errorf("binary not built with a version, and error while fetching the error using 'go version -m': %w", err)
}
}
fmt.Printf("%s\n", version)
return nil
case "help":
printHelp(true)()
os.Exit(0)
case "":
printHelp(false)()
return fmt.Errorf("a command is required, e.g. 'login' or 'yesterday'")
default:
day, err = time.Parse(layoutISO, flag.Arg(0))
if err != nil {
day, err = naturaldate.Parse(flag.Arg(0), time.Now(),
naturaldate.WithDirection(naturaldate.Past),
)
}
logutil.Debugf("day parsed: %s", day.String())
if err != nil {
logutil.Debugf("error parsing: %s", err)
return fmt.Errorf(heredoc.Doc(`
'%s' is not a valid date. The date must of the form:
2021-12-31
today
yesterday
three days ago
3 days ago
wednesday
monday
last tuesday
See the documentation at https://github.com/tj/go-naturaldate#examples.`),
flag.Arg(0))
}
if day.After(time.Now()) {
return fmt.Errorf("cannot give a future date, %s is in the future", day)
}
}
token := conf.Token
if tokenFlag != "" {
token = tokenFlag
}
if token == "" {
logutil.Errorf("no configuration found in ~/.config/clockidup.yml, run 'clockidup login' first or use --token")
os.Exit(1)
}
if !tokenWorks(token) {
logutil.Errorf("existing token does not work, run the 'login' command first or use --token")
os.Exit(1)
}
start := time.Date(day.Year(), day.Month(), day.Day(), 0, 0, 0, 0, day.Location())
end := time.Date(day.Year(), day.Month(), day.Day(), 23, 59, 59, 0, day.Location())
clockify := NewClockify(token, http.DefaultClient)
workspaces, err := clockify.Workspaces()
if err != nil {
return fmt.Errorf("%s", err)
}
if len(workspaces) == 0 {
return fmt.Errorf("no workspace found")
}
workspace := workspaces[0]
userID := workspace.Memberships[0].UserID
timeEntries, err := clockify.TimeEntries(workspaces[0].ID, userID, start, end)
if err != nil {
return fmt.Errorf("%s", err)
}
projects, err := clockify.Projects(workspaces[0].ID)
if err != nil {
return fmt.Errorf("%s", err)
}
projectMap := make(map[string]*Project)
for i := range projects {
proj := &projects[i]
projectMap[proj.ID] = proj
}
// Find the corresponding task when the taskId is set.
for i := range timeEntries {
entry := &timeEntries[i]
if entry.TaskID == "" {
continue
}
task, err := clockify.Task(entry.WorkspaceID, entry.ProjectID, entry.TaskID)
if err != nil {
return fmt.Errorf("while fetching task for time entry '%s: %s': %s", projectMap[entry.ProjectID].Name, entry.Description, err)
}
entry.Description = task.Name + ": " + entry.Description
}
// When onlyBillable is enabled, we leave out the non-billable entries.
selectBillable := func(entries []TimeEntry) []TimeEntry {
var selected []TimeEntry
for _, entry := range entries {
if entry.Billable {
selected = append(selected, entry)
}
}
return selected
}
if *onlyBillable {
timeEntries = selectBillable(timeEntries)
}
// Deduplicate activities: when two activities have the same
// description, I merge them by summing up their duration. The key of
// the entriesSeen map is the description string.
type MergedEntry struct {
Project string
Description string
Task string
Duration time.Duration
Billable bool
}
entriesSeen := make(map[string]*MergedEntry)
var mergedEntries []*MergedEntry
for _, entry := range timeEntries {
existing, found := entriesSeen[entry.Description]
if found && !entry.TimeInterval.End.IsZero() {
existing.Duration += entry.TimeInterval.End.Sub(entry.TimeInterval.Start)
continue
}
projectName := "no-project"
if entry.ProjectID != "" {
projectName = projectMap[entry.ProjectID].Name
}
// When the time entry is still "ticking" i.e., the user has not
// stopped the timer yet, the "end" date is null. In this case, we
// still want to have an estimation of how long this entry has been
// going on for.
duration := entry.TimeInterval.End.Sub(entry.TimeInterval.Start)
if entry.TimeInterval.End.IsZero() {
duration = time.Now().UTC().Sub(entry.TimeInterval.Start)
}
new := MergedEntry{
Project: projectName,
Description: entry.Description,
Duration: duration,
}
mergedEntries = append(mergedEntries, &new)
entriesSeen[entry.Description] = &new
}
// Print the current day e.g., "Monday" if the date is within a week in
// the past; otherwise, print "2021-01-28".
if day.After(time.Now().AddDate(0, 0, -6)) {
fmt.Printf("%s:\n", day.Format("Monday"))
} else {
fmt.Printf("%s:\n", day.Format("2006-01-02"))
}
for i := range mergedEntries {
entry := mergedEntries[len(mergedEntries)-i-1]
// The format "%.1f" (precision = 1) rounds the 2nd digit after the
// decimal to the closest neightbor. We also remove the leading
// zero to distinguish "small" amounts (e.g. 0.5) from larger
// amounts (e.g. 2.0). For example:
//
// 0.55 becomes ".5"
// 0.56 becomes ".6"
// 0.98 becomes "1.0"
// 1.85 becomes "1.8"
// 1.86 becomes "1.9"
hours := fmt.Sprintf("%.1f", entry.Duration.Hours())
hours = strings.TrimPrefix(hours, "0")
fmt.Printf("- [%s] %s: %s\n", hours, entry.Project, entry.Description)
}
return nil
}
// Use Go installed on the system to get the git tag of the running Go
// executable by running:
//
// go version -m /path/to/binary
//
// and by parsing the "mod" line. For example, we want to show "v0.3.0"
// from the following:
//
// /home/mvalais/go/bin/clockidup: go1.16.3
// path github.com/maelvls/clockidup
// mod github.com/maelvls/clockidup v0.3.0 h1:84sL4RRZKsyJgSs8KFyE6ykSjtNk79bBVa0ZgC48Kpw=
// dep github.com/AlecAivazis/survey/v2 v2.2.12 h1:5a07y93zA6SZ09gOa9wLVLznF5zTJMQ+pJ3cZK4IuO8=
func versionUsingGo() (string, error) {
bin, err := os.Executable()
if err != nil {
return "", fmt.Errorf("while trying to find the clockidup binary path: %s", err)
}
cmd := exec.Command("go", "version", "-m", bin)
bytes, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("while slurping stdout from 'go version -m %s': %s", bin, err)
}
if cmd.ProcessState.ExitCode() != 0 {
return "", fmt.Errorf("running 'go version -m %s': %s", bin, err)
}
// We want to be parsing the following line:
//
// mod github.com/maelvls/clockidup v0.3.0 h1:84sL4RRZKsyJgSs8KFyE6ykSjtNk79bBVa0ZgC48Kpw=
// <--> <--> <---><---->
// tab tab tab m[0][1]
regStr := `mod\s*[^\s]*\s*([^\s]*)`
// <------>
// m[0][1]
reg := regexp.MustCompile(regStr)
m := reg.FindAllStringSubmatch(string(bytes), 1)
if len(m) < 1 || len(m[0]) < 2 {
return "", fmt.Errorf("'go version -m %s' did not return a string of the form '%s':\nmatches: %v\nstdout: %s", bin, regStr, m, string(bytes))
}
return m[0][1], nil
}