wrap commands with parent cli closure

This commit is contained in:
Kevin Schoon 2018-01-17 15:44:25 +08:00
parent b4169c3d6f
commit e782c4edd0
1 changed files with 50 additions and 43 deletions

93
main.go
View File

@ -36,61 +36,68 @@ func startTask(task Task, prompter Prompter, db *Store) {
}
func start(cmd *cli.Cmd) {
cmd.Spec = "[OPTIONS] MESSAGE"
var (
duration = cmd.StringOpt("d duration", "25m", "duration of each stent")
count = cmd.IntOpt("c count", 4, "number of working stents")
message = cmd.StringArg("MESSAGE", "", "descriptive name of the given task")
path = cmd.StringOpt("p path", defaultDBPath(), "path to the pomo state directory")
)
cmd.Action = func() {
parsed, err := time.ParseDuration(*duration)
maybe(err)
db, err := NewStore(*path)
maybe(err)
defer db.Close()
task := Task{
Message: *message,
count: *count,
duration: parsed,
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")
count = cmd.IntOpt("c count", 4, "number of working stents")
message = cmd.StringArg("MESSAGE", "", "descriptive name of the given task")
)
cmd.Action = func() {
parsed, err := time.ParseDuration(*duration)
maybe(err)
db, err := NewStore(*path)
maybe(err)
defer db.Close()
task := Task{
Message: *message,
count: *count,
duration: parsed,
}
startTask(task, &I3{}, db)
}
startTask(task, &I3{}, db)
}
}
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))
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(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))
func list(path *string) func(*cli.Cmd) {
return func(cmd *cli.Cmd) {
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))
}
}
}
func _delete(path *string) func(*cli.Cmd) {
return func(cmd *cli.Cmd) {}
}
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)
var (
path = app.StringOpt("p path", defaultDBPath(), "path to the pomo state directory")
)
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)
}