/
current.go
81 lines (66 loc) · 1.68 KB
/
current.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
package command
import (
"fmt"
"strconv"
"time"
"github.com/sachaos/toggl/cache"
"github.com/sachaos/toggl/lib"
"github.com/urfave/cli"
)
func formatTimeDuration(duration time.Duration) string {
hours := duration / time.Hour
minutes := duration / time.Minute % 60
seconds := duration / time.Second % 60
return fmt.Sprintf("%d:%02d:%02d", hours, minutes, seconds)
}
func calcDuration(duration int64) time.Duration {
second := duration + time.Now().Unix()
return time.Duration(second * int64(time.Second))
}
func (app *App) CmdCurrent(c *cli.Context) error {
var project toggl.Project
var timeEntry toggl.TimeEntry
var workspace toggl.Workspace
timeEntry = cache.GetContent().CurrentTimeEntry
if !c.GlobalBool("cache") {
current, err := app.client.GetCurrentTimeEntry()
timeEntry = current.Data
if err != nil {
return err
}
cache.SetCurrentTimeEntry(timeEntry)
cache.Write()
workspaces, err := app.getWorkspaces(c)
if err != nil {
return err
}
workspace, err = workspaces.FindByID(timeEntry.WID)
if timeEntry.PID != 0 {
projects, err := app.getProjects(c)
if err != nil {
return err
}
project, err = projects.FindByID(timeEntry.PID)
if err != nil {
return err
}
}
}
if timeEntry.ID == 0 {
fmt.Println("No time entry")
return nil
}
writer := NewWriter(c)
defer writer.Flush()
records := [][]string{
[]string{"ID", strconv.Itoa(timeEntry.ID)},
[]string{"Description", timeEntry.Description},
[]string{"Project", project.Name},
[]string{"Workspace", workspace.Name},
[]string{"Duration", formatTimeDuration(calcDuration(timeEntry.Duration))},
}
for _, record := range records {
writer.Write(record)
}
return nil
}