Gomain.go -rw-r--r-- 2.6 KiB
1package main
2
3import (
4 "encoding/json"
5 "github.com/jawher/mow.cli"
6 "os"
7 "sort"
8 "time"
9)
10
11func start(path *string) func(*cli.Cmd) {
12 return func(cmd *cli.Cmd) {
13 cmd.Spec = "[OPTIONS] MESSAGE"
14 var (
15 duration = cmd.StringOpt("d duration", "25m", "duration of each stent")
16 pomodoros = cmd.IntOpt("p pomodoros", 4, "number of pomodoros")
17 message = cmd.StringArg("MESSAGE", "", "descriptive name of the given task")
18 tags = cmd.StringsOpt("t tag", []string{}, "tags associated with this task")
19 )
20 cmd.Action = func() {
21 parsed, err := time.ParseDuration(*duration)
22 maybe(err)
23 db, err := NewStore(*path)
24 maybe(err)
25 defer db.Close()
26 task := &Task{
27 Message: *message,
28 Tags: *tags,
29 NPomodoros: *pomodoros,
30 Duration: parsed,
31 }
32 runner, err := NewTaskRunner(task, db)
33 maybe(err)
34 maybe(runner.Run())
35 }
36 }
37}
38
39func initialize(path *string) func(*cli.Cmd) {
40 return func(cmd *cli.Cmd) {
41 cmd.Spec = "[OPTIONS]"
42 cmd.Action = func() {
43 db, err := NewStore(*path)
44 maybe(err)
45 defer db.Close()
46 maybe(initDB(db))
47 }
48 }
49}
50
51func list(path *string) func(*cli.Cmd) {
52 return func(cmd *cli.Cmd) {
53 cmd.Spec = "[OPTIONS]"
54 var (
55 asJSON = cmd.BoolOpt("json", false, "output task history as JSON")
56 assend = cmd.BoolOpt("assend", true, "sort tasks assending in age")
57 limit = cmd.IntOpt("n limit", 0, "limit the number of results by n")
58 )
59 cmd.Action = func() {
60 db, err := NewStore(*path)
61 maybe(err)
62 defer db.Close()
63 tasks, err := db.ReadTasks()
64 maybe(err)
65 if *assend {
66 sort.Sort(sort.Reverse(ByID(tasks)))
67 }
68 if *limit > 0 && (len(tasks) > *limit) {
69 tasks = tasks[0:*limit]
70 }
71 if *asJSON {
72 maybe(json.NewEncoder(os.Stdout).Encode(tasks))
73 return
74 }
75 config, _ := NewConfig(*path + "/config.json")
76 summerizeTasks(config, tasks)
77 }
78 }
79}
80
81func _delete(path *string) func(*cli.Cmd) {
82 return func(cmd *cli.Cmd) {
83 cmd.Spec = "[OPTIONS] TASK_ID"
84 var taskID = cmd.IntArg("TASK_ID", -1, "task to delete")
85 cmd.Action = func() {
86 db, err := NewStore(*path)
87 maybe(err)
88 defer db.Close()
89 maybe(db.DeleteTask(*taskID))
90 }
91 }
92}
93
94func main() {
95 app := cli.App("pomo", "Pomodoro CLI")
96 app.Spec = "[OPTIONS]"
97 var (
98 path = app.StringOpt("p path", defaultConfigPath(), "path to the pomo config directory")
99 )
100 app.Command("start s", "start a new task", start(path))
101 app.Command("init", "initialize the sqlite database", initialize(path))
102 app.Command("list l", "list historical tasks", list(path))
103 app.Command("delete d", "delete a stored task", _delete(path))
104 app.Run(os.Args)
105}