1 | package main
|
2 |
|
3 | import (
|
4 | "encoding/json"
|
5 | "os"
|
6 | "sort"
|
7 | "time"
|
8 |
|
9 | "github.com/jawher/mow.cli"
|
10 | )
|
11 |
|
12 | func start(path *string) func(*cli.Cmd) {
|
13 | return func(cmd *cli.Cmd) {
|
14 | cmd.Spec = "[OPTIONS] MESSAGE"
|
15 | var (
|
16 | duration = cmd.StringOpt("d duration", "25m", "duration of each stent")
|
17 | pomodoros = cmd.IntOpt("p pomodoros", 4, "number of pomodoros")
|
18 | message = cmd.StringArg("MESSAGE", "", "descriptive name of the given task")
|
19 | tags = cmd.StringsOpt("t tag", []string{}, "tags associated with this task")
|
20 | )
|
21 | cmd.Action = func() {
|
22 | parsed, err := time.ParseDuration(*duration)
|
23 | maybe(err)
|
24 | db, err := NewStore(*path)
|
25 | maybe(err)
|
26 | defer db.Close()
|
27 | task := &Task{
|
28 | Message: *message,
|
29 | Tags: *tags,
|
30 | NPomodoros: *pomodoros,
|
31 | Duration: parsed,
|
32 | }
|
33 | runner, err := NewTaskRunner(task, db, NewXnotifier(*path+"/icon.png"))
|
34 | maybe(err)
|
35 | server, err := NewServer(*path+"/pomo.sock", runner)
|
36 | maybe(err)
|
37 | server.Start()
|
38 | defer server.Stop()
|
39 | runner.Start()
|
40 | startUI(runner)
|
41 | }
|
42 | }
|
43 | }
|
44 |
|
45 | func initialize(path *string) func(*cli.Cmd) {
|
46 | return func(cmd *cli.Cmd) {
|
47 | cmd.Spec = "[OPTIONS]"
|
48 | cmd.Action = func() {
|
49 | db, err := NewStore(*path)
|
50 | maybe(err)
|
51 | defer db.Close()
|
52 | maybe(initDB(db))
|
53 | }
|
54 | }
|
55 | }
|
56 |
|
57 | func list(path *string) func(*cli.Cmd) {
|
58 | return func(cmd *cli.Cmd) {
|
59 | cmd.Spec = "[OPTIONS]"
|
60 | var (
|
61 | asJSON = cmd.BoolOpt("json", false, "output task history as JSON")
|
62 | assend = cmd.BoolOpt("assend", false, "sort tasks assending in age")
|
63 | all = cmd.BoolOpt("a all", true, "output all tasks")
|
64 | limit = cmd.IntOpt("n limit", 0, "limit the number of results by n")
|
65 | duration = cmd.StringOpt("d duration", "24h", "show tasks within this duration")
|
66 | )
|
67 | cmd.Action = func() {
|
68 | duration, err := time.ParseDuration(*duration)
|
69 | maybe(err)
|
70 | db, err := NewStore(*path)
|
71 | maybe(err)
|
72 | defer db.Close()
|
73 | tasks, err := db.ReadTasks()
|
74 | maybe(err)
|
75 | if *assend {
|
76 | sort.Sort(sort.Reverse(ByID(tasks)))
|
77 | }
|
78 | if !*all {
|
79 | tasks = After(time.Now().Add(-duration), tasks)
|
80 | }
|
81 | if *limit > 0 && (len(tasks) > *limit) {
|
82 | tasks = tasks[0:*limit]
|
83 | }
|
84 | if *asJSON {
|
85 | maybe(json.NewEncoder(os.Stdout).Encode(tasks))
|
86 | return
|
87 | }
|
88 | config, err := NewConfig(*path + "/config.json")
|
89 | maybe(err)
|
90 | summerizeTasks(config, tasks)
|
91 | }
|
92 | }
|
93 | }
|
94 |
|
95 | func _delete(path *string) func(*cli.Cmd) {
|
96 | return func(cmd *cli.Cmd) {
|
97 | cmd.Spec = "[OPTIONS] TASK_ID"
|
98 | var taskID = cmd.IntArg("TASK_ID", -1, "task to delete")
|
99 | cmd.Action = func() {
|
100 | db, err := NewStore(*path)
|
101 | maybe(err)
|
102 | defer db.Close()
|
103 | maybe(db.DeleteTask(*taskID))
|
104 | }
|
105 | }
|
106 | }
|
107 |
|
108 | func _status(path *string) func(*cli.Cmd) {
|
109 | return func(cmd *cli.Cmd) {
|
110 | cmd.Spec = "[OPTIONS]"
|
111 | cmd.Action = func() {
|
112 | client, err := NewClient(*path + "/pomo.sock")
|
113 | if err != nil {
|
114 | outputStatus(Status{})
|
115 | return
|
116 | }
|
117 | defer client.Close()
|
118 | status, err := client.Status()
|
119 | maybe(err)
|
120 | outputStatus(*status)
|
121 | }
|
122 | }
|
123 | }
|
124 |
|
125 | func main() {
|
126 | app := cli.App("pomo", "Pomodoro CLI")
|
127 | app.Spec = "[OPTIONS]"
|
128 | var (
|
129 | path = app.StringOpt("p path", defaultConfigPath(), "path to the pomo config directory")
|
130 | )
|
131 | app.Version("v version", Version)
|
132 | app.Command("start s", "start a new task", start(path))
|
133 | app.Command("init", "initialize the sqlite database", initialize(path))
|
134 | app.Command("list l", "list historical tasks", list(path))
|
135 | app.Command("delete d", "delete a stored task", _delete(path))
|
136 | app.Command("status st", "output the current status", _status(path))
|
137 | app.Run(os.Args)
|
138 | }
|