canopeas/database/database.go

40 lines
625 B
Go

package database
import (
"go.etcd.io/bbolt"
)
var (
firedBucket = []byte("fired")
unbanBucket = []byte("unban")
trackBucket = []byte("track")
buckets = [][]byte{
firedBucket,
unbanBucket,
trackBucket,
}
)
type Database struct {
db *bbolt.DB
}
func Load(dbPath string) (*Database, error) {
db, err := bbolt.Open(dbPath, 0o600, bbolt.DefaultOptions)
if err != nil {
return nil, err
}
for _, b := range buckets {
if err := db.Update(func(tx *bbolt.Tx) error {
_, err := tx.CreateBucketIfNotExists(b)
return err
}); err != nil {
return nil, err
}
}
return &Database{
db: db,
}, nil
}