-
Notifications
You must be signed in to change notification settings - Fork 2
/
db.go
66 lines (55 loc) · 1.22 KB
/
db.go
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
package main
import (
"os"
"path/filepath"
"github.com/urfave/cli"
bolt "go.etcd.io/bbolt"
)
var settings = []byte("settings")
var wallpaper = []byte("wallpaper")
var metaData = []byte("metadata")
type db struct {
db *bolt.DB
}
func getDbFromCtx(c *cli.Context) (*db, error) {
return getDb(getConfigPathFromCtx(c))
}
func getDb(cpath string) (*db, error) {
path := getConfigPath(cpath)
err := os.MkdirAll(path, os.ModePerm)
if err != nil {
return nil, err
}
dbc, err := bolt.Open(filepath.Join(path, "sway-wallhaven.db"), 0600, nil)
if err != nil {
return nil, err
}
db := db{db: dbc}
return &db, nil
}
func (d *db) setWallpaper(path string) error {
return d.db.Update(func(tx *bolt.Tx) error {
bucket, err := tx.CreateBucketIfNotExists(settings)
if err != nil {
return err
}
return bucket.Put(wallpaper, []byte(path))
})
}
func (d *db) getWallpaper() (string, error) {
var out []byte
err := d.db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket(settings)
tmp := bucket.Get(wallpaper)
out = make([]byte, len(tmp))
copy(out, tmp)
return nil
})
return string(out), err
}
func (d *db) setMetaData(path string, meta []byte) error {
return nil
}
func (d *db) close() error {
return d.db.Close()
}