forked from kevinschoon/pomo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
108 lines (101 loc) · 2.64 KB
/
main.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
package main
import (
"encoding/json"
"os"
"sort"
"time"
"github.com/jawher/mow.cli"
)
func start(path *string) func(*cli.Cmd) {
return func(cmd *cli.Cmd) {
cmd.Spec = "[OPTIONS] MESSAGE"
var (
duration = cmd.StringOpt("d duration", "25m", "duration of each stent")
pomodoros = cmd.IntOpt("p pomodoros", 4, "number of pomodoros")
message = cmd.StringArg("MESSAGE", "", "descriptive name of the given task")
tags = cmd.StringsOpt("t tag", []string{}, "tags associated with this task")
)
cmd.Action = func() {
parsed, err := time.ParseDuration(*duration)
maybe(err)
db, err := NewStore(*path)
maybe(err)
defer db.Close()
task := &Task{
Message: *message,
Tags: *tags,
NPomodoros: *pomodoros,
Duration: parsed,
}
runner, err := NewTaskRunner(task, db, NewXnotifier(*path+"/icon.png"))
maybe(err)
runner.Start()
startUI(runner)
}
}
}
func initialize(path *string) func(*cli.Cmd) {
return func(cmd *cli.Cmd) {
cmd.Spec = "[OPTIONS]"
cmd.Action = func() {
db, err := NewStore(*path)
maybe(err)
defer db.Close()
maybe(initDB(db))
}
}
}
func list(path *string) func(*cli.Cmd) {
return func(cmd *cli.Cmd) {
cmd.Spec = "[OPTIONS]"
var (
asJSON = cmd.BoolOpt("json", false, "output task history as JSON")
assend = cmd.BoolOpt("assend", true, "sort tasks assending in age")
limit = cmd.IntOpt("n limit", 0, "limit the number of results by n")
)
cmd.Action = func() {
db, err := NewStore(*path)
maybe(err)
defer db.Close()
tasks, err := db.ReadTasks()
maybe(err)
if *assend {
sort.Sort(sort.Reverse(ByID(tasks)))
}
if *limit > 0 && (len(tasks) > *limit) {
tasks = tasks[0:*limit]
}
if *asJSON {
maybe(json.NewEncoder(os.Stdout).Encode(tasks))
return
}
config, _ := NewConfig(*path + "/config.json")
summerizeTasks(config, tasks)
}
}
}
func _delete(path *string) func(*cli.Cmd) {
return func(cmd *cli.Cmd) {
cmd.Spec = "[OPTIONS] TASK_ID"
var taskID = cmd.IntArg("TASK_ID", -1, "task to delete")
cmd.Action = func() {
db, err := NewStore(*path)
maybe(err)
defer db.Close()
maybe(db.DeleteTask(*taskID))
}
}
}
func main() {
app := cli.App("pomo", "Pomodoro CLI")
app.Spec = "[OPTIONS]"
var (
path = app.StringOpt("p path", defaultConfigPath(), "path to the pomo config directory")
)
app.Version("v version", Version)
app.Command("start s", "start a new task", start(path))
app.Command("init", "initialize the sqlite database", initialize(path))
app.Command("list l", "list historical tasks", list(path))
app.Command("delete d", "delete a stored task", _delete(path))
app.Run(os.Args)
}