kevinschoon-pomo/main.go

97 lines
2.2 KiB
Go
Raw Normal View History

2018-01-16 10:50:08 +01:00
package main
import (
2018-01-16 13:02:35 +01:00
"encoding/json"
2018-01-16 10:50:08 +01:00
"fmt"
"github.com/jawher/mow.cli"
"os"
"time"
)
func maybe(err error) {
if err != nil {
2018-01-16 12:06:20 +01:00
fmt.Printf("Error:\n%s\n", err)
2018-01-16 10:50:08 +01:00
os.Exit(1)
}
}
func startTask(task Task, prompter Prompter, db *Store) {
2018-01-16 13:02:35 +01:00
taskID, err := db.CreateTask(task)
2018-01-16 10:50:08 +01:00
maybe(err)
for i := 0; i < task.count; i++ {
2018-01-16 12:06:20 +01:00
// Create a record for
2018-01-16 10:50:08 +01:00
// this particular stent of work
2018-01-16 12:06:20 +01:00
record := &Record{}
2018-01-16 10:50:08 +01:00
// Prompt the client
maybe(prompter.Prompt("Begin Working!"))
2018-01-16 12:06:20 +01:00
record.Start = time.Now()
2018-01-16 10:50:08 +01:00
// Wait the specified interval
time.Sleep(task.duration)
maybe(prompter.Prompt("Take a Break!"))
// Record how long the user waited
// until closing the notification
2018-01-16 12:06:20 +01:00
record.End = time.Now()
2018-01-16 13:02:35 +01:00
maybe(db.CreateRecord(taskID, *record))
2018-01-16 10:50:08 +01:00
}
}
func start(cmd *cli.Cmd) {
2018-01-17 06:14:20 +01:00
cmd.Spec = "[OPTIONS] MESSAGE"
2018-01-16 10:50:08 +01:00
var (
duration = cmd.StringOpt("d duration", "25m", "duration of each stent")
count = cmd.IntOpt("c count", 4, "number of working stents")
2018-01-17 06:14:20 +01:00
message = cmd.StringArg("MESSAGE", "", "descriptive name of the given task")
2018-01-16 12:06:20 +01:00
path = cmd.StringOpt("p path", defaultDBPath(), "path to the pomo state directory")
2018-01-16 10:50:08 +01:00
)
cmd.Action = func() {
parsed, err := time.ParseDuration(*duration)
maybe(err)
db, err := NewStore(*path)
maybe(err)
2018-01-16 12:06:20 +01:00
defer db.Close()
2018-01-16 10:50:08 +01:00
task := Task{
2018-01-17 06:14:20 +01:00
Message: *message,
2018-01-16 10:50:08 +01:00
count: *count,
duration: parsed,
}
startTask(task, &I3{}, db)
}
}
2018-01-16 12:06:20 +01:00
func initialize(cmd *cli.Cmd) {
cmd.Spec = "[OPTIONS]"
var (
path = cmd.StringOpt("p path", defaultDBPath(), "path to the pomo state directory")
)
cmd.Action = func() {
db, err := NewStore(*path)
maybe(err)
defer db.Close()
maybe(initDB(db))
}
}
2018-01-16 10:50:08 +01:00
2018-01-16 13:02:35 +01:00
func list(cmd *cli.Cmd) {
var (
path = cmd.StringOpt("p path", defaultDBPath(), "path to the pomo state directory")
)
cmd.Action = func() {
db, err := NewStore(*path)
maybe(err)
defer db.Close()
tasks, err := db.ReadTasks()
maybe(err)
maybe(json.NewEncoder(os.Stdout).Encode(tasks))
}
}
2018-01-16 10:50:08 +01:00
func main() {
app := cli.App("pomo", "Pomodoro CLI")
app.Spec = "[OPTIONS]"
app.Command("start", "start a new task", start)
app.Command("init", "initialize the sqlite database", initialize)
app.Command("ls", "list historical tasks", list)
app.Run(os.Args)
}