summaryrefslogtreecommitdiff
path: root/internal/commands/out.go
blob: 1355f3d81ff5c02f5a7c9675f4b82c4bfea6dd5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package commands

import (
	"errors"
	"fmt"
	"time"

	"punchcard/internal/actions"
	punchctx "punchcard/internal/context"

	"github.com/spf13/cobra"
)

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")
			}

			a := actions.New(q)
			session, err := a.PunchOut(cmd.Context())
			if err != nil {
				if errors.Is(err, actions.ErrNoActiveTimer) {
					return ErrNoActiveTimer
				}
				return err
			}

			// Output success message
			cmd.Printf("Timer stopped. Session duration: %v\n", session.Duration.Round(time.Second))
			cmd.Printf("Time entry ID: %d\n", session.ID)

			return nil
		},
	}
}