Skip to content

Commit c65ae0b

Browse files
committed
Added db package for boltDB ops
1 parent 2c1df31 commit c65ae0b

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

db/tasks.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package db
2+
3+
import (
4+
"time"
5+
6+
"github.com/boltdb/bolt"
7+
)
8+
9+
var taskBucket = []byte("task")
10+
var db *bolt.DB
11+
12+
type Task struct {
13+
Key int
14+
Value string
15+
}
16+
17+
// Init : won't run automatically
18+
func Init(dbPath string) error {
19+
20+
var err error
21+
db, err = bolt.Open(dbPath, 0600, &bolt.Options{Timeout: 1 * time.Second}) // Can't use := because scope will change to local instead of package leve
22+
23+
if err != nil {
24+
panic(err)
25+
}
26+
27+
defer db.Close()
28+
29+
return db.Update(func(tx *bolt.Tx) error {
30+
_, err := tx.CreateBucketIfNotExists(taskBucket)
31+
return err
32+
})
33+
34+
}

main.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
package main
22

3-
import "github.com/todo-cli/cmd"
3+
import (
4+
"path/filepath"
5+
6+
"github.com/mitchellh/go-homedir"
7+
"github.com/todo-cli/cmd"
8+
"github.com/todo-cli/db"
9+
)
410

511
func main() {
12+
home, _ := homedir.Dir()
13+
dbPath := filepath.Join(home, "tasks.db")
14+
err := db.Init(dbPath)
15+
if err != nil {
16+
panic(err)
17+
}
618
cmd.RootCmd.Execute()
719
}

my.db

16 KB
Binary file not shown.

todo-cli

445 KB
Binary file not shown.

0 commit comments

Comments
 (0)