forked from nexneo/samay
/
commands.go
187 lines (160 loc) · 4.07 KB
/
commands.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
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"time"
"github.com/nexneo/samay/data"
"github.com/nexneo/samay/web"
)
var (
commands = map[string]func(*data.Project) error{
"start": startTimer,
"s": startTimer,
"stop": stopTimer,
"p": stopTimer,
"entry": addEntry,
"e": addEntry,
"rm": deleteEntryOrProject,
"show": showEntryOrProject,
"mv": moveProjectOrEntry,
"log": logProject,
"report": report,
"web": startweb,
}
)
func startTimer(project *data.Project) (err error) {
return project.StartTimer()
}
func stopTimer(project *data.Project) (err error) {
return project.StopTimer(getContent(), billable)
}
func addEntry(project *data.Project) (err error) {
data.Save(project)
entry := project.CreateEntryWithDuration(
getContent(), duration, billable,
)
err = data.Save(entry)
return
}
func showEntryOrProject(project *data.Project) (err error) {
if theIdx < 0 {
return showProject(project)
}
return showEntry(project)
}
func showProject(project *data.Project) (err error) {
fmt.Printf(" id : %s\n", project.GetShaFromName())
fmt.Printf(" name : %s\n", project.GetName())
fmt.Printf(" entries : %d\n", len(project.Entries()))
fmt.Printf(" location : %s\n", project.Location())
return nil
}
func showEntry(project *data.Project) (err error) {
var started, ended *time.Time
for i, entry := range project.Entries() {
if i == theIdx {
started, err = entry.StartedTime()
ended, err = entry.EndedTime()
fmt.Printf(" id : %s\n", entry.GetId())
fmt.Printf(" contents : %s\n", entry.GetContent())
fmt.Printf(" duration : %s\n", strings.Trim(entry.HoursMins().String(), " "))
fmt.Printf(" started : %s\n", started)
fmt.Printf(" ended : %s\n", ended)
fmt.Printf(" tags : %v\n", entry.GetTags())
fmt.Printf(" billable : %t\n", entry.GetBillable())
break
}
}
return err
}
func deleteEntryOrProject(project *data.Project) (err error) {
if theIdx < 0 {
return deleteProject(project)
}
return deleteEntry(project)
}
func deleteEntry(project *data.Project) (err error) {
for i, entry := range project.Entries() {
if i == theIdx {
return data.Destroy(entry)
}
}
return
}
func deleteProject(project *data.Project) (err error) {
var remove string
fmt.Printf(
"Remove all data for project \"%s\" ([No]/yes)? ",
project.GetName(),
)
if fmt.Scanln(&remove); remove == "yes" {
err = data.Destroy(project)
}
return
}
func moveProjectOrEntry(project *data.Project) (err error) {
newProject := data.CreateProject(newName)
if err = data.Save(newProject); err != nil {
return err
}
if theIdx < 0 {
return moveProject(project, newProject)
}
return moveEntry(project, newProject)
}
func moveProject(project, newProject *data.Project) error {
for _, entry := range project.Entries() {
entry.Project = newProject
if err := data.Save(entry); err != nil {
return err
}
}
fmt.Printf("All entries copied to project \"%s\" \n...\n", newProject.GetName())
return deleteProject(project)
}
func moveEntry(project, newProject *data.Project) error {
for i, entry := range project.Entries() {
if i == theIdx {
entry.Project = newProject
return data.Save(entry)
}
}
return nil
}
func report(project *data.Project) (err error) {
if month > 0 && month < 13 {
data.PrintProjectStatus(month)
} else {
err = fmt.Errorf("Month %d is not valid", month)
}
return
}
func startweb(project *data.Project) error {
return web.StartServer(httpPort)
}
func logProject(project *data.Project) error {
data.PrintProjectLog(project)
return nil
}
// Set timelog entry content and tags using external editor.
// Only if it didn't set via -m flag
func getContent() string {
if content == "" {
content, _ = openEditor()
}
return content
}
// Open external editor for text input from user
func openEditor() (string, error) {
file, err := ioutil.TempFile(os.TempDir(), "samay-buffer")
cmd := exec.Command("subl", "-w", file.Name())
if err = cmd.Start(); err != nil {
return "", err
}
cmd.Wait()
data, err := ioutil.ReadFile(file.Name())
return string(data), err
}