-
Notifications
You must be signed in to change notification settings - Fork 27
/
start.go
75 lines (59 loc) · 1.54 KB
/
start.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
package command
import (
"fmt"
"os"
"os/exec"
"github.com/codegangsta/cli"
"github.com/hashicorp/errwrap"
"github.com/timeglass/glass/model"
)
type Start struct {
*command
}
func NewStart() *Start {
return &Start{newCommand()}
}
func (c *Start) Name() string {
return "start"
}
func (c *Start) Description() string {
return fmt.Sprintf("Starts the daemon that is reposible for tracking time for the current repository. If the daemon is already running, this operation is a no-op.")
}
func (c *Start) Usage() string {
return "Manually start timer for the current repository"
}
func (c *Start) Flags() []cli.Flag {
return []cli.Flag{}
}
func (c *Start) Action() func(ctx *cli.Context) {
return c.command.Action(c.Run)
}
func (c *Start) 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)
}
conf, err := m.ReadConfig()
if err != nil {
return errwrap.Wrapf(fmt.Sprintf("Failed to read configuration: {{err}}"), err)
}
client := NewClient(info)
err = client.Call("timer.start")
if err != nil {
if err != ErrDaemonDown {
return err
}
cmd := exec.Command("glass-daemon", fmt.Sprintf("--mbu=%s", conf.MBU))
err := cmd.Start()
if err != nil {
return errwrap.Wrapf(fmt.Sprintf("Failed to start Daemon: {{err}}"), err)
}
}
fmt.Println("Timeglass: timer started")
return nil
}