summaryrefslogtreecommitdiff
path: root/internal/commands/root.go
blob: 6e619802dbf03bfc59b073372f5f0dd6350e3d04 (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
43
44
45
46
47
48
49
50
51
package commands

import (
	"context"
	"database/sql"

	punchctx "git.tjp.lol/punchcard/internal/context"
	"git.tjp.lol/punchcard/internal/database"

	"github.com/spf13/cobra"
)

func NewRootCmd() *cobra.Command {
	cmd := &cobra.Command{
		Use:   "punch",
		Short: "A simple time tracking CLI tool",
		Long:  "Punchcard helps you track your work hours and generate professional invoices and timesheets.",
		RunE:  NewTUICmd().RunE,
	}

	cmd.AddCommand(NewAddCmd())
	cmd.AddCommand(NewInCmd())
	cmd.AddCommand(NewOutCmd())
	cmd.AddCommand(NewStatusCmd())
	cmd.AddCommand(NewImportCmd())
	cmd.AddCommand(NewReportCmd())
	cmd.AddCommand(NewSetCmd())
	cmd.AddCommand(NewTUICmd())
	cmd.AddCommand(NewArchiveCmd())
	cmd.AddCommand(NewUnarchiveCmd())

	return cmd
}

func Execute() error {
	// Get database connection
	q, err := database.GetDB()
	if err != nil {
		return err
	}
	defer func() {
		if db, ok := q.DBTX().(*sql.DB); ok {
			_ = db.Close()
		}
	}()

	// Create context with database
	ctx := punchctx.WithDB(context.Background(), q)

	return NewRootCmd().ExecuteContext(ctx)
}