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" cmd.Spec = "[OPTIONS] MESSAGE"
var ( var (
duration = cmd.StringOpt("d duration", "25m", "duration of each stent") duration = cmd.StringOpt("d duration", "25m", "duration of each stent")
count = cmd.IntOpt("c count", 4, "number of working stents") count = cmd.IntOpt("c count", 4, "number of working stents")
message = cmd.StringArg("MESSAGE", "", "descriptive name of the given task") 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() { cmd.Action = func() {
parsed, err := time.ParseDuration(*duration) parsed, err := time.ParseDuration(*duration)
@ -57,25 +57,23 @@ func start(cmd *cli.Cmd) {
} }
startTask(task, &I3{}, db) startTask(task, &I3{}, db)
} }
}
} }
func initialize(cmd *cli.Cmd) { func initialize(path *string) func(*cli.Cmd) {
return func(cmd *cli.Cmd) {
cmd.Spec = "[OPTIONS]" cmd.Spec = "[OPTIONS]"
var (
path = cmd.StringOpt("p path", defaultDBPath(), "path to the pomo state directory")
)
cmd.Action = func() { cmd.Action = func() {
db, err := NewStore(*path) db, err := NewStore(*path)
maybe(err) maybe(err)
defer db.Close() defer db.Close()
maybe(initDB(db)) 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() { cmd.Action = func() {
db, err := NewStore(*path) db, err := NewStore(*path)
maybe(err) maybe(err)
@ -84,13 +82,22 @@ func list(cmd *cli.Cmd) {
maybe(err) maybe(err)
maybe(json.NewEncoder(os.Stdout).Encode(tasks)) 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)
} }