diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-03 23:10:26 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-03 23:10:26 +0200 |
| commit | e96b0b370bcdd55ad2d5b20187e4bbae78785ff2 (patch) | |
| tree | 2475df7fb9119a3f1db95f54f9f7e718d65324b6 /internal/cli/timer.go | |
| parent | 28920ad225992386069d8513d0cf097dd50daeef (diff) | |
Task 352: integrate timer and worktime login sync
Diffstat (limited to 'internal/cli/timer.go')
| -rw-r--r-- | internal/cli/timer.go | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/internal/cli/timer.go b/internal/cli/timer.go index aafcda0..86c958f 100644 --- a/internal/cli/timer.go +++ b/internal/cli/timer.go @@ -6,10 +6,12 @@ import ( "math/rand/v2" "strconv" "strings" + "time" "codeberg.org/snonux/timr/internal/ascii" "codeberg.org/snonux/timr/internal/live" timrTimer "codeberg.org/snonux/timr/internal/timer" + "codeberg.org/snonux/timr/internal/worktime" tea "github.com/charmbracelet/bubbletea" "github.com/spf13/cobra" ) @@ -50,6 +52,9 @@ func newTimerStartCmd() *cobra.Command { if err != nil { return err } + if err := syncWorktimeWithTimer(true); err != nil { + return err + } return printOutput(cmd, output) }, } @@ -64,6 +69,9 @@ func newTimerStopCmd() *cobra.Command { if err != nil { return err } + if err := syncWorktimeWithTimer(false); err != nil { + return err + } return printOutput(cmd, output) }, } @@ -207,3 +215,32 @@ func printOutput(cmd *cobra.Command, output string) error { _, err := fmt.Fprintln(cmd.OutOrStdout(), output) return err } + +func syncWorktimeWithTimer(start bool) error { + cfg := CurrentConfig() + if !cfg.AutoWorktimeLogin { + return nil + } + + ctx, err := resolveWorkContext() + if err != nil { + return err + } + + now := time.Now() + if start { + _, err = worktime.Login(ctx.dbDir, ctx.host, "work", now, "auto timer start") + } else { + _, err = worktime.Logout(ctx.dbDir, ctx.host, "work", now, "auto timer stop") + } + if err == nil { + return nil + } + + // Avoid failing timer commands on no-op state sync mismatches. + if strings.Contains(err.Error(), "already logged in") || strings.Contains(err.Error(), "not logged in") { + return nil + } + + return err +} |
