-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgolog.go
164 lines (143 loc) · 3.59 KB
/
golog.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
package main
import (
"fmt"
"os"
"regexp"
"time"
"github.com/codegangsta/cli"
"github.com/mitchellh/go-homedir"
)
const alphanumericRegex = "^[a-zA-Z0-9_-]*$"
const dbFile = "~/.golog"
var dbPath, _ = homedir.Expand(dbFile)
var repository = TaskCsvRepository{Path: dbPath}
var transformer = Transformer{}
var commands = []cli.Command{
{
Name: "start",
Usage: "Start tracking a given task",
Action: Start,
BashComplete: AutocompleteTasks,
},
{
Name: "stop",
Usage: "Stop tracking a given task",
Action: Stop,
BashComplete: AutocompleteTasks,
},
{
Name: "status",
Usage: "Give status of all tasks",
Action: Status,
BashComplete: AutocompleteTasks,
},
{
Name: "clear",
Usage: "Clear all data",
Action: Clear,
},
{
Name: "list",
Usage: "List all tasks",
Action: List,
},
}
// Start a given task
func Start(context *cli.Context) error {
identifier := context.Args().First()
if !IsValidIdentifier(identifier) {
return invalidIdentifier(identifier)
}
err := repository.save(Task{Identifier: identifier, Action: "start", At: time.Now().Format(time.RFC3339)})
if err == nil {
fmt.Println("Started tracking ", identifier)
}
return err
}
// Stop a given task
func Stop(context *cli.Context) error {
identifier := context.Args().First()
if !IsValidIdentifier(identifier) {
return invalidIdentifier(identifier)
}
err := repository.save(Task{Identifier: identifier, Action: "stop", At: time.Now().Format(time.RFC3339)})
if err == nil {
fmt.Println("Stopped tracking ", identifier)
}
return err
}
// Status display tasks being tracked
func Status(context *cli.Context) error {
identifier := context.Args().First()
if !IsValidIdentifier(identifier) {
return invalidIdentifier(identifier)
}
tasks, err := repository.load()
if err != nil {
return err
}
transformer.LoadedTasks = tasks.getByIdentifier(identifier)
fmt.Println(transformer.Transform()[identifier])
return nil
}
// List lists all tasks
func List(context *cli.Context) error {
var err error
transformer.LoadedTasks, err = repository.load()
if err != nil {
return err
}
for _, task := range transformer.Transform() {
fmt.Println(task)
}
return nil
}
// Clear all data
func Clear(context *cli.Context) error {
err := repository.clear()
if err == nil {
fmt.Println("All tasks deleted")
}
return err
}
// AutocompleteTasks loads tasks from repository and show them for completion
func AutocompleteTasks(context *cli.Context) {
var err error
transformer.LoadedTasks, err = repository.load()
// This will complete if no args are passed
// or there is problem with tasks repo
if len(context.Args()) > 0 || err != nil {
return
}
for _, task := range transformer.LoadedTasks.Items {
fmt.Println(task.getIdentifier())
}
}
// IsValidIdentifier checks if the string passed is a valid task identifier
func IsValidIdentifier(identifier string) bool {
re := regexp.MustCompile(alphanumericRegex)
return len(identifier) > 0 && re.MatchString(identifier)
}
func checkInitialDbFile() {
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
os.Create(dbPath)
}
}
func main() {
// @todo remove this from here, should be in file repo implementation
checkInitialDbFile()
app := cli.NewApp()
app.Name = "Golog"
app.Usage = "Easy CLI time tracker for your tasks"
app.Version = "0.1"
app.EnableBashCompletion = true
app.Commands = commands
err := app.Run(os.Args)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func invalidIdentifier(identifier string) error {
return fmt.Errorf("identifier %q is invalid", identifier)
}