diff --git a/testcmd.yaml b/testcmd.yaml index 1346e6f..bca3dba 100644 --- a/testcmd.yaml +++ b/testcmd.yaml @@ -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: | diff --git a/timefor b/timefor index ebb7567..3d4d9f9 100755 Binary files a/timefor and b/timefor differ diff --git a/timefor.go b/timefor.go index 82cbb03..df46178 100644 --- a/timefor.go +++ b/timefor.go @@ -5,7 +5,7 @@ import ( "database/sql" "errors" "fmt" - "io/ioutil" + "io" "log" "os" "os/exec" @@ -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", @@ -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 { @@ -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) } } @@ -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) if err != nil { return "", err }