2020-09-08 18:35:47 +02:00
|
|
|
package cmd
|
2018-01-16 10:50:08 +01:00
|
|
|
|
|
|
|
import (
|
2019-01-25 06:04:41 +01:00
|
|
|
"database/sql"
|
2018-01-16 13:02:35 +01:00
|
|
|
"encoding/json"
|
2019-01-25 06:04:41 +01:00
|
|
|
"fmt"
|
2018-01-16 10:50:08 +01:00
|
|
|
"os"
|
2020-09-08 18:35:47 +02:00
|
|
|
"os/user"
|
|
|
|
"path"
|
2018-01-20 18:20:01 +01:00
|
|
|
"sort"
|
2018-01-16 10:50:08 +01:00
|
|
|
"time"
|
2019-01-13 13:01:00 +01:00
|
|
|
|
|
|
|
cli "github.com/jawher/mow.cli"
|
2020-09-08 18:35:47 +02:00
|
|
|
|
|
|
|
pomo "github.com/kevinschoon/pomo/pkg/internal"
|
2018-01-16 10:50:08 +01:00
|
|
|
)
|
|
|
|
|
2020-09-08 18:35:47 +02:00
|
|
|
func maybe(err error) {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error:\n%s\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultConfigPath() string {
|
|
|
|
u, err := user.Current()
|
|
|
|
maybe(err)
|
|
|
|
return path.Join(u.HomeDir, "/.pomo/config.json")
|
|
|
|
}
|
|
|
|
|
|
|
|
func start(config *pomo.Config) func(*cli.Cmd) {
|
2018-01-17 08:44:25 +01:00
|
|
|
return func(cmd *cli.Cmd) {
|
|
|
|
cmd.Spec = "[OPTIONS] MESSAGE"
|
|
|
|
var (
|
2018-01-20 13:17:58 +01:00
|
|
|
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")
|
2018-01-17 08:44:25 +01:00
|
|
|
)
|
|
|
|
cmd.Action = func() {
|
|
|
|
parsed, err := time.ParseDuration(*duration)
|
|
|
|
maybe(err)
|
2020-09-08 18:35:47 +02:00
|
|
|
db, err := pomo.NewStore(config.DBPath)
|
2018-01-17 08:44:25 +01:00
|
|
|
maybe(err)
|
|
|
|
defer db.Close()
|
2020-09-08 18:35:47 +02:00
|
|
|
task := &pomo.Task{
|
2018-01-20 19:54:30 +01:00
|
|
|
Message: *message,
|
|
|
|
Tags: *tags,
|
|
|
|
NPomodoros: *pomodoros,
|
|
|
|
Duration: parsed,
|
2018-01-17 08:44:25 +01:00
|
|
|
}
|
2019-01-25 06:04:41 +01:00
|
|
|
maybe(db.With(func(tx *sql.Tx) error {
|
|
|
|
id, err := db.CreateTask(tx, *task)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
task.ID = id
|
|
|
|
return nil
|
|
|
|
}))
|
2020-09-08 18:35:47 +02:00
|
|
|
runner, err := pomo.NewTaskRunner(task, config)
|
2018-01-21 11:35:03 +01:00
|
|
|
maybe(err)
|
2020-09-08 18:35:47 +02:00
|
|
|
server, err := pomo.NewServer(runner, config)
|
2018-02-04 04:13:46 +01:00
|
|
|
maybe(err)
|
|
|
|
server.Start()
|
|
|
|
defer server.Stop()
|
2018-01-26 16:07:38 +01:00
|
|
|
runner.Start()
|
2020-09-08 18:35:47 +02:00
|
|
|
pomo.StartUI(runner)
|
2018-01-16 10:50:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-08 18:35:47 +02:00
|
|
|
func create(config *pomo.Config) func(*cli.Cmd) {
|
2019-01-12 19:39:38 +01:00
|
|
|
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)
|
2020-09-08 18:35:47 +02:00
|
|
|
db, err := pomo.NewStore(config.DBPath)
|
2019-01-12 19:39:38 +01:00
|
|
|
maybe(err)
|
|
|
|
defer db.Close()
|
2020-09-08 18:35:47 +02:00
|
|
|
task := &pomo.Task{
|
2019-01-12 19:39:38 +01:00
|
|
|
Message: *message,
|
|
|
|
Tags: *tags,
|
|
|
|
NPomodoros: *pomodoros,
|
|
|
|
Duration: parsed,
|
|
|
|
}
|
2019-01-25 06:04:41 +01:00
|
|
|
maybe(db.With(func(tx *sql.Tx) error {
|
|
|
|
taskId, err := db.CreateTask(tx, *task)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println(taskId)
|
|
|
|
return nil
|
|
|
|
}))
|
2019-01-12 19:39:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-08 18:35:47 +02:00
|
|
|
func begin(config *pomo.Config) func(*cli.Cmd) {
|
2019-01-13 13:01:00 +01:00
|
|
|
return func(cmd *cli.Cmd) {
|
2019-01-25 06:04:41 +01:00
|
|
|
cmd.Spec = "[OPTIONS] TASK_ID"
|
|
|
|
var (
|
|
|
|
taskId = cmd.IntArg("TASK_ID", -1, "ID of Pomodoro to begin")
|
|
|
|
)
|
2019-01-13 13:01:00 +01:00
|
|
|
|
|
|
|
cmd.Action = func() {
|
2020-09-08 18:35:47 +02:00
|
|
|
db, err := pomo.NewStore(config.DBPath)
|
2019-01-13 13:01:00 +01:00
|
|
|
maybe(err)
|
|
|
|
defer db.Close()
|
2020-09-08 18:35:47 +02:00
|
|
|
var task *pomo.Task
|
2019-01-25 06:04:41 +01:00
|
|
|
maybe(db.With(func(tx *sql.Tx) error {
|
|
|
|
read, err := db.ReadTask(tx, *taskId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-01-13 13:01:00 +01:00
|
|
|
}
|
2019-01-25 06:04:41 +01:00
|
|
|
task = read
|
|
|
|
err = db.DeletePomodoros(tx, *taskId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-08 18:35:47 +02:00
|
|
|
task.Pomodoros = []*pomo.Pomodoro{}
|
2019-01-25 06:04:41 +01:00
|
|
|
return nil
|
|
|
|
}))
|
2020-09-08 18:35:47 +02:00
|
|
|
runner, err := pomo.NewTaskRunner(task, config)
|
2019-01-25 06:04:41 +01:00
|
|
|
maybe(err)
|
2020-09-08 18:35:47 +02:00
|
|
|
server, err := pomo.NewServer(runner, config)
|
2019-01-25 06:04:41 +01:00
|
|
|
maybe(err)
|
|
|
|
server.Start()
|
|
|
|
defer server.Stop()
|
|
|
|
runner.Start()
|
2020-09-08 18:35:47 +02:00
|
|
|
pomo.StartUI(runner)
|
2019-01-13 13:01:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-08 18:35:47 +02:00
|
|
|
func initialize(config *pomo.Config) func(*cli.Cmd) {
|
2018-01-17 08:44:25 +01:00
|
|
|
return func(cmd *cli.Cmd) {
|
|
|
|
cmd.Spec = "[OPTIONS]"
|
|
|
|
cmd.Action = func() {
|
2020-09-08 18:35:47 +02:00
|
|
|
db, err := pomo.NewStore(config.DBPath)
|
2018-01-17 08:44:25 +01:00
|
|
|
maybe(err)
|
|
|
|
defer db.Close()
|
2020-09-08 18:35:47 +02:00
|
|
|
maybe(pomo.InitDB(db))
|
2018-01-17 08:44:25 +01:00
|
|
|
}
|
2018-01-16 12:06:20 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-16 10:50:08 +01:00
|
|
|
|
2020-09-08 18:35:47 +02:00
|
|
|
func list(config *pomo.Config) func(*cli.Cmd) {
|
2018-01-17 08:44:25 +01:00
|
|
|
return func(cmd *cli.Cmd) {
|
2018-01-20 17:51:27 +01:00
|
|
|
cmd.Spec = "[OPTIONS]"
|
2018-01-20 18:20:01 +01:00
|
|
|
var (
|
2018-02-09 18:02:34 +01:00
|
|
|
asJSON = cmd.BoolOpt("json", false, "output task history as JSON")
|
|
|
|
assend = cmd.BoolOpt("assend", false, "sort tasks assending in age")
|
2018-03-01 18:18:13 +01:00
|
|
|
all = cmd.BoolOpt("a all", true, "output all tasks")
|
2018-02-09 18:02:34 +01:00
|
|
|
limit = cmd.IntOpt("n limit", 0, "limit the number of results by n")
|
|
|
|
duration = cmd.StringOpt("d duration", "24h", "show tasks within this duration")
|
2018-01-20 18:20:01 +01:00
|
|
|
)
|
2018-01-17 08:44:25 +01:00
|
|
|
cmd.Action = func() {
|
2018-02-09 18:02:34 +01:00
|
|
|
duration, err := time.ParseDuration(*duration)
|
|
|
|
maybe(err)
|
2020-09-08 18:35:47 +02:00
|
|
|
db, err := pomo.NewStore(config.DBPath)
|
2018-01-17 08:44:25 +01:00
|
|
|
maybe(err)
|
|
|
|
defer db.Close()
|
2019-01-25 06:04:41 +01:00
|
|
|
maybe(db.With(func(tx *sql.Tx) error {
|
|
|
|
tasks, err := db.ReadTasks(tx)
|
|
|
|
maybe(err)
|
|
|
|
if *assend {
|
2020-09-08 18:35:47 +02:00
|
|
|
sort.Sort(sort.Reverse(pomo.ByID(tasks)))
|
2019-01-25 06:04:41 +01:00
|
|
|
}
|
|
|
|
if !*all {
|
2020-09-08 18:35:47 +02:00
|
|
|
tasks = pomo.After(time.Now().Add(-duration), tasks)
|
2019-01-25 06:04:41 +01:00
|
|
|
}
|
|
|
|
if *limit > 0 && (len(tasks) > *limit) {
|
|
|
|
tasks = tasks[0:*limit]
|
|
|
|
}
|
|
|
|
if *asJSON {
|
|
|
|
maybe(json.NewEncoder(os.Stdout).Encode(tasks))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
maybe(err)
|
2020-09-08 18:35:47 +02:00
|
|
|
pomo.SummerizeTasks(config, tasks)
|
2019-01-25 06:04:41 +01:00
|
|
|
return nil
|
|
|
|
}))
|
2018-01-17 08:44:25 +01:00
|
|
|
}
|
2018-01-16 13:02:35 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-16 10:50:08 +01:00
|
|
|
|
2020-09-08 18:35:47 +02:00
|
|
|
func _delete(config *pomo.Config) func(*cli.Cmd) {
|
2018-01-17 08:53:45 +01:00
|
|
|
return func(cmd *cli.Cmd) {
|
|
|
|
cmd.Spec = "[OPTIONS] TASK_ID"
|
|
|
|
var taskID = cmd.IntArg("TASK_ID", -1, "task to delete")
|
|
|
|
cmd.Action = func() {
|
2020-09-08 18:35:47 +02:00
|
|
|
db, err := pomo.NewStore(config.DBPath)
|
2018-01-17 08:53:45 +01:00
|
|
|
maybe(err)
|
|
|
|
defer db.Close()
|
2019-01-25 06:04:41 +01:00
|
|
|
maybe(db.With(func(tx *sql.Tx) error {
|
|
|
|
return db.DeleteTask(tx, *taskID)
|
|
|
|
}))
|
2018-01-17 08:53:45 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-17 08:44:25 +01:00
|
|
|
}
|
|
|
|
|
2020-09-08 18:35:47 +02:00
|
|
|
func _status(config *pomo.Config) func(*cli.Cmd) {
|
2018-02-04 04:13:46 +01:00
|
|
|
return func(cmd *cli.Cmd) {
|
|
|
|
cmd.Spec = "[OPTIONS]"
|
|
|
|
cmd.Action = func() {
|
2020-09-08 18:35:47 +02:00
|
|
|
client, err := pomo.NewClient(config.SocketPath)
|
2018-02-04 04:13:46 +01:00
|
|
|
if err != nil {
|
2020-09-08 18:35:47 +02:00
|
|
|
pomo.OutputStatus(pomo.Status{})
|
2018-02-04 04:13:46 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer client.Close()
|
|
|
|
status, err := client.Status()
|
|
|
|
maybe(err)
|
2020-09-08 18:35:47 +02:00
|
|
|
pomo.OutputStatus(*status)
|
2018-02-04 04:13:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-08 18:35:47 +02:00
|
|
|
func _config(config *pomo.Config) func(*cli.Cmd) {
|
2019-07-05 00:13:04 +02:00
|
|
|
return func(cmd *cli.Cmd) {
|
|
|
|
cmd.Spec = "[OPTIONS]"
|
|
|
|
cmd.Action = func() {
|
|
|
|
maybe(json.NewEncoder(os.Stdout).Encode(config))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-21 16:20:44 +02:00
|
|
|
func New(config *pomo.Config) *cli.Cli {
|
2018-01-16 10:50:08 +01:00
|
|
|
app := cli.App("pomo", "Pomodoro CLI")
|
2019-01-25 06:42:38 +01:00
|
|
|
app.LongDesc = "Pomo helps you track what you did, how long it took you to do it, and how much effort you expect it to take."
|
2018-01-16 10:50:08 +01:00
|
|
|
app.Spec = "[OPTIONS]"
|
2018-01-17 08:44:25 +01:00
|
|
|
var (
|
2020-09-21 16:20:44 +02:00
|
|
|
path = app.StringOpt("p path", defaultConfigPath(), "path to the pomo config directory")
|
2018-01-17 08:44:25 +01:00
|
|
|
)
|
2019-07-05 00:57:41 +02:00
|
|
|
app.Before = func() {
|
2020-09-21 16:20:44 +02:00
|
|
|
if config == nil {
|
|
|
|
config = &pomo.Config{}
|
|
|
|
}
|
2020-09-08 18:35:47 +02:00
|
|
|
maybe(pomo.LoadConfig(*path, config))
|
2019-07-05 00:57:41 +02:00
|
|
|
}
|
2020-09-08 18:35:47 +02:00
|
|
|
app.Version("v version", pomo.Version)
|
2019-07-05 00:57:41 +02:00
|
|
|
app.Command("start s", "start a new task", start(config))
|
|
|
|
app.Command("init", "initialize the sqlite database", initialize(config))
|
|
|
|
app.Command("config cf", "display the current configuration", _config(config))
|
|
|
|
app.Command("create c", "create a new task without starting", create(config))
|
|
|
|
app.Command("begin b", "begin requested pomodoro", begin(config))
|
|
|
|
app.Command("list l", "list historical tasks", list(config))
|
|
|
|
app.Command("delete d", "delete a stored task", _delete(config))
|
|
|
|
app.Command("status st", "output the current status", _status(config))
|
2020-09-21 16:20:44 +02:00
|
|
|
return app
|
2018-01-16 10:50:08 +01:00
|
|
|
}
|
2020-09-21 16:20:44 +02:00
|
|
|
|
|
|
|
func Run() { New(nil).Run(os.Args) }
|