diff options
| author | demo <demo@antix1> | 2026-05-29 11:27:00 -0400 |
|---|---|---|
| committer | demo <demo@antix1> | 2026-05-29 11:27:00 -0400 |
| commit | e0e6d43095e93d65833dcebe79ec85778c29eb32 (patch) | |
| tree | fb6395385a29baa2b041201ed56f8d979b78c8bf /main.go | |
| parent | df480b02bceb31039e25c12357404248ba13b8f8 (diff) | |
wip: implement business logic (rough draft)
There are a ton of visual warts, but the actual logic seems sound so
far.
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 39 |
1 files changed, 36 insertions, 3 deletions
@@ -8,9 +8,42 @@ import ( "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{ { @@ -21,7 +54,7 @@ func main() { args := cmd.Args().Slice() task := strings.Join(args, " ") - return cmdAdd(task) + return ctrl.cmdAdd(task) }, }, { @@ -45,7 +78,7 @@ func main() { return fmt.Errorf("invalid 'do' argument: %d", taskIndex) } - return cmdDo(taskIndex) + return ctrl.cmdDo(taskIndex) }, }, { @@ -53,7 +86,7 @@ func main() { Aliases: []string{"l"}, Usage: "list pending tasks", Action: func(ctx context.Context, cmd *cli.Command) error { - return cmdList() + return ctrl.cmdList() }, }, }, |
