Skip to content

Commit

Permalink
Add hooks support
Browse files Browse the repository at this point in the history
  • Loading branch information
justincampbell committed Jan 18, 2021
1 parent 447c73e commit 484f70f
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 4 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,32 @@ Writing a blog post
%c - Completed Pomodoros today
%l - Pomodoros remaining (left) to reach goal
```
## Hooks

Hooks can be run when Pomodoros change state. There are 3 possible hooks:

* `break`
* `start`
* `stop`

Commands which run hooks:

* `break`: `break` immediately, `stop` when break is over
* `cancel`: `stop`
* `clear`: `stop`
* `finish`: `stop`
* `repeat`: `start`
* `start`: `start`

To enable a hook, create an executable file in the `hooks` subdirectory within your configuration directory. For example:

```sh
#!/usr/bin/env bash
set -exo pipefail

say "Pomodoro started"
```

```shellsession
$ chmod +x ~/.pomodoro/hooks/start
```
11 changes: 10 additions & 1 deletion cmd/break.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/justincampbell/go-countdown"
"github.com/justincampbell/go-countdown/format"
"github.com/open-pomodoro/openpomodoro-cli/hook"
"github.com/spf13/cobra"
)

Expand All @@ -31,7 +32,15 @@ func breakCmd(cmd *cobra.Command, args []string) error {
}
}

return wait(d)
if err := hook.Run(client, "break"); err != nil {
return err
}

if err := wait(d); err != nil {
return err
}

return hook.Run(client, "stop")
}

func wait(d time.Duration) error {
Expand Down
5 changes: 5 additions & 0 deletions cmd/cancel.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"github.com/open-pomodoro/openpomodoro-cli/hook"
"github.com/spf13/cobra"
)

Expand All @@ -15,5 +16,9 @@ func init() {
}

func cancelCmd(cmd *cobra.Command, args []string) error {
if err := hook.Run(client, "stop"); err != nil {
return err
}

return client.Cancel()
}
5 changes: 5 additions & 0 deletions cmd/clear.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"github.com/open-pomodoro/openpomodoro-cli/hook"
"github.com/spf13/cobra"
)

Expand All @@ -15,5 +16,9 @@ func init() {
}

func clearCmd(cmd *cobra.Command, args []string) error {
if err := hook.Run(client, "stop"); err != nil {
return err
}

return client.Clear()
}
9 changes: 8 additions & 1 deletion cmd/finish.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cmd

import "github.com/spf13/cobra"
import (
"github.com/open-pomodoro/openpomodoro-cli/hook"
"github.com/spf13/cobra"
)

func init() {
command := &cobra.Command{
Expand All @@ -13,5 +16,9 @@ func init() {
}

func finishCmd(cmd *cobra.Command, args []string) error {
if err := hook.Run(client, "stop"); err != nil {
return err
}

return client.Finish()
}
5 changes: 5 additions & 0 deletions cmd/repeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"time"

"github.com/open-pomodoro/openpomodoro-cli/hook"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -45,5 +46,9 @@ func repeatCmd(cmd *cobra.Command, args []string) error {
return err
}

if err := hook.Run(client, "start"); err != nil {
return err
}

return statusCmd(cmd, args)
}
8 changes: 6 additions & 2 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/open-pomodoro/go-openpomodoro"
"github.com/open-pomodoro/openpomodoro-cli/hook"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -46,8 +47,11 @@ func startCmd(cmd *cobra.Command, args []string) error {
p.StartTime = time.Now().Add(-agoFlag)
p.Tags = tagsFlag

err := client.Start(p)
if err != nil {
if err := client.Start(p); err != nil {
return err
}

if err := hook.Run(client, "start"); err != nil {
return err
}

Expand Down
28 changes: 28 additions & 0 deletions hook/hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package hook

import (
"fmt"
"os"
"os/exec"
"path"

"github.com/open-pomodoro/go-openpomodoro"
)

// Run runs a hook with the given name.
func Run(client *openpomodoro.Client, name string) error {
filename := path.Join(client.Directory, "hooks", name)

if _, err := os.Stat(filename); os.IsNotExist(err) {
return nil
}

cmd := exec.Command(filename)
cmd.Env = os.Environ()
if output, err := cmd.Output(); err != nil {
fmt.Printf("Hook %q failed:\n%s\n\n", name, output)
return err
}

return nil
}

0 comments on commit 484f70f

Please sign in to comment.