summaryrefslogtreecommitdiff
path: root/main.go
blob: 7838fcbd908f4b39aca9d4b3e6a22c203e7ff494 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"strings"

	"github.com/urfave/cli/v3"
	"go.etcd.io/bbolt"
)

type controller struct {
	db              *bbolt.DB
	tasksBucketName []byte
}

func newController(filename string) (controller, error) {
	db, err := bbolt.Open(filename, 0644, nil)
	if err != nil {
		return controller{}, fmt.Errorf("can't open database file '%s': %w", filename, err)
	}

	tasksBucketName := []byte("tasks")

	// Create the tasks bucket for the first time. From here on,
	// all transactions can assume this bucket exists.
	db.Update(func(tx *bbolt.Tx) error {
		_, err := tx.CreateBucket(tasksBucketName)
		if err != nil {
			return fmt.Errorf("cant' create tasks bucket: %w", err)
		}

		return nil
	})

	return controller{db, tasksBucketName}, nil
}

func main() {
	ctrl, err := newController("tasks.db")
	if err != nil {
		log.Fatal(err)
	}

	cmd := &cli.Command{
		Commands: []*cli.Command{
			{
				Name:    "add",
				Aliases: []string{"a"},
				Usage:   "add a task to the list",
				Action: func(ctx context.Context, cmd *cli.Command) error {
					args := cmd.Args().Slice()
					task := strings.Join(args, " ")

					return ctrl.cmdAdd(task)
				},
			},
			{
				Name:    "do",
				Aliases: []string{"d"},
				Usage:   "complete a task on the list",
				Arguments: []cli.Argument{
					&cli.IntArg{
						Name: "taskIndex",
					},
				},
				Action: func(ctx context.Context, cmd *cli.Command) error {
					taskIndex := cmd.IntArg("taskIndex")

					// If taskIndex is 0, it's
					// likely missing; any other
					// non-positive value is a
					// deliberate
					// misconfiguration.
					if taskIndex <= 0 {
						return fmt.Errorf("invalid 'do' argument: %d", taskIndex)
					}

					return ctrl.cmdDo(taskIndex)
				},
			},
			{
				Name:    "undo",
				Aliases: []string{"u"},
				Usage:   "reset a task on the list",
				Arguments: []cli.Argument{
					&cli.IntArg{
						Name: "taskIndex",
					},
				},
				Action: func(ctx context.Context, cmd *cli.Command) error {
					taskIndex := cmd.IntArg("taskIndex")

					if taskIndex <= 0 {
						return fmt.Errorf("invalid 'undo' argument: %d", taskIndex)
					}

					return ctrl.cmdUndo(taskIndex)
				},
			},
			{
				Name:    "list",
				Aliases: []string{"l"},
				Usage:   "list pending tasks",
				Action: func(ctx context.Context, cmd *cli.Command) error {
					return ctrl.cmdList()
				},
			},
		},
	}

	if err := cmd.Run(context.Background(), os.Args); err != nil {
		log.Fatal(err)
	}
}