summaryrefslogtreecommitdiff
path: root/internal/commands/out.go
diff options
context:
space:
mode:
authorT <t@tjp.lol>2025-08-02 17:25:59 -0600
committerT <t@tjp.lol>2025-08-04 09:34:14 -0600
commit8be5f93f5b2d4b6f438ca84094937a0f7101c59b (patch)
tree3cedb6379818a28179e269477c12ae06dd57ca36 /internal/commands/out.go
Initial commit of punchcard.
Contains working time tracking commands, and the stub of a command to generate reports.
Diffstat (limited to 'internal/commands/out.go')
-rw-r--r--internal/commands/out.go49
1 files changed, 49 insertions, 0 deletions
diff --git a/internal/commands/out.go b/internal/commands/out.go
new file mode 100644
index 0000000..f98a63c
--- /dev/null
+++ b/internal/commands/out.go
@@ -0,0 +1,49 @@
+package commands
+
+import (
+ "database/sql"
+ "errors"
+ "fmt"
+ "time"
+
+ punchctx "punchcard/internal/context"
+
+ "github.com/spf13/cobra"
+)
+
+var ErrNoActiveTimer = errors.New("no active timer found")
+
+func NewOutCmd() *cobra.Command {
+ return &cobra.Command{
+ Use: "out",
+ Short: "Stop the active timer",
+ Long: "Stop tracking time for the current work session by setting the end time of the active time entry.",
+ Args: cobra.NoArgs,
+ RunE: func(cmd *cobra.Command, args []string) error {
+ q := punchctx.GetDB(cmd.Context())
+ if q == nil {
+ return fmt.Errorf("database not available in context")
+ }
+
+ // Stop the active timer by setting end_time to now
+ stoppedEntry, err := q.StopTimeEntry(cmd.Context())
+ if err != nil {
+ if errors.Is(err, sql.ErrNoRows) {
+ return ErrNoActiveTimer
+ }
+ return fmt.Errorf("failed to stop timer: %w", err)
+ }
+
+ // Calculate duration
+ duration := stoppedEntry.EndTime.Time.Sub(stoppedEntry.StartTime)
+
+ // Output success message
+ cmd.Printf("Timer stopped. Session duration: %v\n", duration.Round(time.Second))
+
+ // Show entry ID for reference
+ cmd.Printf("Time entry ID: %d\n", stoppedEntry.ID)
+
+ return nil
+ },
+ }
+}