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) { func start(path *string) func(*cli.Cmd) {
cmd.Spec = "[OPTIONS] MESSAGE" return func(cmd *cli.Cmd) {
var ( cmd.Spec = "[OPTIONS] MESSAGE"
duration = cmd.StringOpt("d duration", "25m", "duration of each stent") var (
count = cmd.IntOpt("c count", 4, "number of working stents") duration = cmd.StringOpt("d duration", "25m", "duration of each stent")
message = cmd.StringArg("MESSAGE", "", "descriptive name of the given task") count = cmd.IntOpt("c count", 4, "number of working stents")
path = cmd.StringOpt("p path", defaultDBPath(), "path to the pomo state directory") message = cmd.StringArg("MESSAGE", "", "descriptive name of the given task")
) )
cmd.Action = func() { cmd.Action = func() {
parsed, err := time.ParseDuration(*duration) parsed, err := time.ParseDuration(*duration)
maybe(err) maybe(err)
db, err := NewStore(*path) db, err := NewStore(*path)
maybe(err) maybe(err)
defer db.Close() defer db.Close()
task := Task{ task := Task{
Message: *message, Message: *message,
count: *count, count: *count,
duration: parsed, duration: parsed,
}
startTask(task, &I3{}, db)
} }
startTask(task, &I3{}, db)
} }
} }
func initialize(cmd *cli.Cmd) { func initialize(path *string) func(*cli.Cmd) {
cmd.Spec = "[OPTIONS]" return func(cmd *cli.Cmd) {
var ( cmd.Spec = "[OPTIONS]"
path = cmd.StringOpt("p path", defaultDBPath(), "path to the pomo state directory") cmd.Action = func() {
) db, err := NewStore(*path)
cmd.Action = func() { maybe(err)
db, err := NewStore(*path) defer db.Close()
maybe(err) maybe(initDB(db))
defer db.Close() }
maybe(initDB(db))
} }
} }
func list(cmd *cli.Cmd) { func list(path *string) func(*cli.Cmd) {
var ( return func(cmd *cli.Cmd) {
path = cmd.StringOpt("p path", defaultDBPath(), "path to the pomo state directory") cmd.Action = func() {
) db, err := NewStore(*path)
cmd.Action = func() { maybe(err)
db, err := NewStore(*path) defer db.Close()
maybe(err) tasks, err := db.ReadTasks()
defer db.Close() maybe(err)
tasks, err := db.ReadTasks() maybe(json.NewEncoder(os.Stdout).Encode(tasks))
maybe(err) }
maybe(json.NewEncoder(os.Stdout).Encode(tasks))
} }
} }
func _delete(path *string) func(*cli.Cmd) {
return func(cmd *cli.Cmd) {}
}
func main() { func main() {
app := cli.App("pomo", "Pomodoro CLI") app := cli.App("pomo", "Pomodoro CLI")
app.Spec = "[OPTIONS]" app.Spec = "[OPTIONS]"
app.Command("start", "start a new task", start) var (
app.Command("init", "initialize the sqlite database", initialize) path = app.StringOpt("p path", defaultDBPath(), "path to the pomo state directory")
app.Command("ls", "list historical tasks", list) )
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) app.Run(os.Args)
} }