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

33
main.go
View File

@ -36,13 +36,13 @@ func startTask(task Task, prompter Prompter, db *Store) {
}
func start(cmd *cli.Cmd) {
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")
path = cmd.StringOpt("p path", defaultDBPath(), "path to the pomo state directory")
)
cmd.Action = func() {
parsed, err := time.ParseDuration(*duration)
@ -58,12 +58,11 @@ func start(cmd *cli.Cmd) {
startTask(task, &I3{}, db)
}
}
}
func initialize(cmd *cli.Cmd) {
func initialize(path *string) func(*cli.Cmd) {
return func(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)
@ -71,11 +70,10 @@ func initialize(cmd *cli.Cmd) {
maybe(initDB(db))
}
}
}
func list(cmd *cli.Cmd) {
var (
path = cmd.StringOpt("p path", defaultDBPath(), "path to the pomo state directory")
)
func list(path *string) func(*cli.Cmd) {
return func(cmd *cli.Cmd) {
cmd.Action = func() {
db, err := NewStore(*path)
maybe(err)
@ -85,12 +83,21 @@ func list(cmd *cli.Cmd) {
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)
}