diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-04 00:07:08 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-04 00:07:08 +0200 |
| commit | a5773ce86b88d0e1c1abbf03fba4aad0d965f77c (patch) | |
| tree | 5cf85b95b15adaf640d7a1d1a9b1ec1c28fb5753 | |
| parent | c6a9ee27141fd1a57813fcc7f54fe370408bae14 (diff) | |
cli: share timer elapsed-state check helper
| -rw-r--r-- | internal/cli/timer.go | 34 | ||||
| -rw-r--r-- | internal/cli/work.go | 9 |
2 files changed, 22 insertions, 21 deletions
diff --git a/internal/cli/timer.go b/internal/cli/timer.go index 22f1ce8..37e50d0 100644 --- a/internal/cli/timer.go +++ b/internal/cli/timer.go @@ -39,16 +39,12 @@ func newTimerStartCmd() *cobra.Command { Use: "start", Short: "Start the timer", RunE: func(cmd *cobra.Command, args []string) error { - rawStatus, err := timrTimer.GetRawStatus() + hasElapsed, err := timerHasElapsed() if err != nil { - return fmt.Errorf("get raw timer status: %w", err) - } - status, err := strconv.ParseFloat(rawStatus, 64) - if err != nil { - return fmt.Errorf("parse raw timer status %q: %w", rawStatus, err) + return err } - output, err := timrTimer.StartTimer(status > 0) + output, err := timrTimer.StartTimer(hasElapsed) if err != nil { return err } @@ -82,17 +78,13 @@ func newTimerContinueCmd() *cobra.Command { Use: "continue", Short: "Continue a stopped timer", RunE: func(cmd *cobra.Command, args []string) error { - rawStatus, err := timrTimer.GetRawStatus() + hasElapsed, err := timerHasElapsed() if err != nil { - return fmt.Errorf("get raw timer status: %w", err) - } - status, err := strconv.ParseFloat(rawStatus, 64) - if err != nil { - return fmt.Errorf("parse raw timer status %q: %w", rawStatus, err) + return err } output := "Timer is at 0, cannot continue." - if status > 0 { + if hasElapsed { output, err = timrTimer.StartTimer(true) if err != nil { return err @@ -248,3 +240,17 @@ func syncWorktimeWithTimer(cmd *cobra.Command, start bool) error { return err } + +func timerHasElapsed() (bool, error) { + rawStatus, err := timrTimer.GetRawStatus() + if err != nil { + return false, fmt.Errorf("get raw timer status: %w", err) + } + + status, err := strconv.ParseFloat(rawStatus, 64) + if err != nil { + return false, fmt.Errorf("parse raw timer status %q: %w", rawStatus, err) + } + + return status > 0, nil +} diff --git a/internal/cli/work.go b/internal/cli/work.go index 1f34cf9..22f0406 100644 --- a/internal/cli/work.go +++ b/internal/cli/work.go @@ -7,7 +7,6 @@ import ( "os" "os/exec" "path/filepath" - "strconv" "strings" "time" @@ -404,15 +403,11 @@ func workDBPath(dbDir, host string) string { } func startTimerFromWorkCommand() (string, error) { - rawStatus, err := timrTimer.GetRawStatus() + hasElapsed, err := timerHasElapsed() if err != nil { return "", err } - status, err := strconv.ParseFloat(rawStatus, 64) - if err != nil { - return "", err - } - return timrTimer.StartTimer(status > 0) + return timrTimer.StartTimer(hasElapsed) } func stopTimerFromWorkCommand() (string, error) { |
