-
Notifications
You must be signed in to change notification settings - Fork 27
/
lap.go
88 lines (71 loc) · 1.71 KB
/
lap.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
package command
import (
"fmt"
"os"
"github.com/codegangsta/cli"
"github.com/hashicorp/errwrap"
"github.com/timeglass/glass/model"
"github.com/timeglass/glass/vcs"
)
type Lap struct {
*command
}
func NewLap() *Lap {
return &Lap{newCommand()}
}
func (c *Lap) Name() string {
return "lap"
}
func (c *Lap) Description() string {
return fmt.Sprintf("Resets the running timer, report spent time and punch as time spent on last commit")
}
func (c *Lap) Usage() string {
return "Register time spent on last commit and reset the timer to 0s"
}
func (c *Lap) Flags() []cli.Flag {
return []cli.Flag{
cli.BoolFlag{
Name: "from-hook",
Usage: "Indicate it is called from a git, now expects refs on stdin",
},
}
}
func (c *Lap) Action() func(ctx *cli.Context) {
return c.command.Action(c.Run)
}
func (c *Lap) Run(ctx *cli.Context) error {
dir, err := os.Getwd()
if err != nil {
return errwrap.Wrapf("Failed to fetch current working dir: {{err}}", err)
}
m := model.New(dir)
info, err := m.ReadDaemonInfo()
if err != nil {
return errwrap.Wrapf(fmt.Sprintf("Failed to get Daemon address: {{err}}"), err)
}
//get time and reset
client := NewClient(info)
t, err := client.Lap()
if err != nil {
if err == ErrDaemonDown {
//if were calling this from a hook, supress errors
if ctx.Bool("from-hook") {
return nil
}
return errwrap.Wrapf(fmt.Sprintf("No timer appears to be running for '%s': {{err}}", dir), err)
} else {
return err
}
}
//write the vcs
vc, err := vcs.GetVCS(dir)
if err != nil {
return errwrap.Wrapf("Failed to setup VCS: {{err}}", err)
}
err = vc.Log(t)
if err != nil {
return errwrap.Wrapf("Failed to log time into VCS: {{err}}", err)
}
fmt.Println(t)
return nil
}