Skip to content

Commit 8771afa

Browse files
committed
Add a hook argument for the daemon command
1 parent 7a9c9f0 commit 8771afa

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

testcmd.yaml

+14
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,23 @@
3434
Flags:
3535
--break-time duration time for a break reminder (default 1h20m0s)
3636
-h, --help help for daemon
37+
--hook string a hook command template
3738
--repeat-time duration time to repeat a break reminder (default 10m0s)
3839
--sleep-time duration sleep time in the loop (default 30s)
3940
41+
- name: daemon--bad-hook-template
42+
cmd: daemon --sleep-time 1s --hook 'echo "{{if}}"'
43+
code: 1
44+
output: |
45+
Error: cannot render hook command: failed to parse template: template: tpl:1: missing value for if
46+
47+
- name: daemon--err-in-hook-cmd
48+
cmd: daemon --sleep-time 1s --hook 'exit 1'
49+
code: 1
50+
output: |
51+
running hook command: exit 1
52+
Error: cannot run hook command: exit status 1
53+
4054
- name: report--inactive
4155
cmd: report
4256
output: |

timefor

179 KB
Binary file not shown.

timefor.go

+28-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"database/sql"
66
"errors"
77
"fmt"
8-
"io/ioutil"
8+
"io"
99
"log"
1010
"os"
1111
"os/exec"
@@ -186,13 +186,21 @@ func newCmd(db *sqlx.DB) *cobra.Command {
186186
if err != nil {
187187
return err
188188
}
189-
Daemon(db, sleepTime, breakTime, repeatTime)
189+
hook, err := cmd.Flags().GetString("hook")
190+
if err != nil {
191+
return err
192+
}
193+
err = Daemon(db, sleepTime, breakTime, repeatTime, hook)
194+
if err != nil {
195+
return err
196+
}
190197
return nil
191198
},
192199
}
193200
daemonCmd.Flags().Duration("sleep-time", sleepTimeForDaemon, "sleep time in the loop")
194201
daemonCmd.Flags().Duration("break-time", breakTimeForDaemon, "time for a break reminder")
195202
daemonCmd.Flags().Duration("repeat-time", repeatTimeForDaemon, "time to repeat a break reminder")
203+
daemonCmd.Flags().StringP("hook", "", "", "a hook command template")
196204

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

429437
// Daemon updates the duration of current activity then sleeps for a while
430-
func Daemon(db *sqlx.DB, sleepTime time.Duration, breakTime time.Duration, repeatTime time.Duration) error {
438+
func Daemon(db *sqlx.DB, sleepTime time.Duration, breakTime time.Duration, repeatTime time.Duration, hook string) error {
431439
var notified time.Time
440+
var lastHook string
432441
for {
433442
_, err := UpdateIfExists(db, "", false)
434443
if err != nil {
@@ -455,10 +464,24 @@ func Daemon(db *sqlx.DB, sleepTime time.Duration, breakTime time.Duration, repea
455464
}
456465
err := exec.Command("notify-send", args...).Run()
457466
if err != nil {
458-
log.Printf("cannot send notification: %v", err)
467+
fmt.Printf("cannot send notification: %v", err)
459468
}
460469
notified = time.Now()
461470
}
471+
if hook != "" {
472+
cmd, err := activity.Format(hook)
473+
if err != nil {
474+
return fmt.Errorf("cannot render hook command: %v", err)
475+
}
476+
if lastHook != cmd {
477+
lastHook = cmd
478+
fmt.Printf("running hook command: %s\n", cmd)
479+
err = exec.Command("sh", "-c", cmd).Run()
480+
if err != nil {
481+
return fmt.Errorf("cannot run hook command: %v", err)
482+
}
483+
}
484+
}
462485
time.Sleep(sleepTime)
463486
}
464487
}
@@ -587,7 +610,7 @@ func Select(db *sqlx.DB) (string, error) {
587610
fmt.Fprintln(cmdIn, name)
588611
}
589612
cmdIn.Close()
590-
selectedName, err := ioutil.ReadAll(cmdOut)
613+
selectedName, err := io.ReadAll(cmdOut)
591614
if err != nil {
592615
return "", err
593616
}

0 commit comments

Comments
 (0)