Skip to content

Commit

Permalink
Add a hook argument for the daemon command
Browse files Browse the repository at this point in the history
  • Loading branch information
naspeh committed Apr 6, 2024
1 parent 7a9c9f0 commit 8771afa
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
14 changes: 14 additions & 0 deletions testcmd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,23 @@
Flags:
--break-time duration time for a break reminder (default 1h20m0s)
-h, --help help for daemon
--hook string a hook command template
--repeat-time duration time to repeat a break reminder (default 10m0s)
--sleep-time duration sleep time in the loop (default 30s)
- name: daemon--bad-hook-template
cmd: daemon --sleep-time 1s --hook 'echo "{{if}}"'
code: 1
output: |
Error: cannot render hook command: failed to parse template: template: tpl:1: missing value for if
- name: daemon--err-in-hook-cmd
cmd: daemon --sleep-time 1s --hook 'exit 1'
code: 1
output: |
running hook command: exit 1
Error: cannot run hook command: exit status 1
- name: report--inactive
cmd: report
output: |
Expand Down
Binary file modified timefor
Binary file not shown.
33 changes: 28 additions & 5 deletions timefor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"database/sql"
"errors"
"fmt"
"io/ioutil"
"io"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -186,13 +186,21 @@ func newCmd(db *sqlx.DB) *cobra.Command {
if err != nil {
return err
}
Daemon(db, sleepTime, breakTime, repeatTime)
hook, err := cmd.Flags().GetString("hook")
if err != nil {
return err
}
err = Daemon(db, sleepTime, breakTime, repeatTime, hook)
if err != nil {
return err
}
return nil
},
}
daemonCmd.Flags().Duration("sleep-time", sleepTimeForDaemon, "sleep time in the loop")
daemonCmd.Flags().Duration("break-time", breakTimeForDaemon, "time for a break reminder")
daemonCmd.Flags().Duration("repeat-time", repeatTimeForDaemon, "time to repeat a break reminder")
daemonCmd.Flags().StringP("hook", "", "", "a hook command template")

var dbCmd = &cobra.Command{
Use: "db",
Expand Down Expand Up @@ -427,8 +435,9 @@ func Show(db *sqlx.DB, tpl string) error {
}

// Daemon updates the duration of current activity then sleeps for a while
func Daemon(db *sqlx.DB, sleepTime time.Duration, breakTime time.Duration, repeatTime time.Duration) error {
func Daemon(db *sqlx.DB, sleepTime time.Duration, breakTime time.Duration, repeatTime time.Duration, hook string) error {
var notified time.Time
var lastHook string
for {
_, err := UpdateIfExists(db, "", false)
if err != nil {
Expand All @@ -455,10 +464,24 @@ func Daemon(db *sqlx.DB, sleepTime time.Duration, breakTime time.Duration, repea
}
err := exec.Command("notify-send", args...).Run()
if err != nil {
log.Printf("cannot send notification: %v", err)
fmt.Printf("cannot send notification: %v", err)
}
notified = time.Now()
}
if hook != "" {
cmd, err := activity.Format(hook)
if err != nil {
return fmt.Errorf("cannot render hook command: %v", err)
}
if lastHook != cmd {
lastHook = cmd
fmt.Printf("running hook command: %s\n", cmd)
err = exec.Command("sh", "-c", cmd).Run()
if err != nil {
return fmt.Errorf("cannot run hook command: %v", err)
}
}
}
time.Sleep(sleepTime)
}
}
Expand Down Expand Up @@ -587,7 +610,7 @@ func Select(db *sqlx.DB) (string, error) {
fmt.Fprintln(cmdIn, name)
}
cmdIn.Close()
selectedName, err := ioutil.ReadAll(cmdOut)
selectedName, err := io.ReadAll(cmdOut)

Check failure on line 613 in timefor.go

View workflow job for this annotation

GitHub Actions / test

undefined: io.ReadAll
if err != nil {
return "", err
}
Expand Down

0 comments on commit 8771afa

Please sign in to comment.