-
Notifications
You must be signed in to change notification settings - Fork 1
/
storage.go
134 lines (110 loc) · 2.56 KB
/
storage.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package main
import (
"fmt"
"path/filepath"
"github.com/boltdb/bolt"
"github.com/pkg/errors"
)
const (
dbBucket = "2faKeys"
)
type Storage struct {
db *bolt.DB
filename string
folder string
}
func NewStorage(folder string, filename string) Storage {
storage := Storage{
folder: folder,
filename: filename,
}
return storage
}
func (s *Storage) Init() error {
db, err := bolt.Open(filepath.Join(s.folder, s.filename), 0600, nil)
if err != nil {
return fmt.Errorf("cannot open database: %w", err)
}
s.db = db
err = db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte(dbBucket))
if err != nil {
return fmt.Errorf("cannot create bucket: %w", err)
}
return nil
})
if err != nil {
return err
}
return nil
}
func (s *Storage) Close() {
s.db.Close()
}
func (s *Storage) AddKey(key string, value []byte) (bool, error) {
err := s.db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(dbBucket))
err := bucket.Put([]byte(key), value)
if err != nil {
return fmt.Errorf("cannot put: %w", err)
}
return nil
})
if err != nil {
return false, err
}
return true, nil
}
func (s *Storage) ListKey() ([]string, error) {
var keys []string
err := s.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(dbBucket))
c := b.Cursor()
for k, _ := c.First(); k != nil; k, _ = c.Next() {
// fmt.Printf("key=%s, value=%s\n", k, v)
keys = append(keys, string(k))
}
return nil
})
if err != nil {
return []string{}, err
}
return keys, nil
}
func (s *Storage) GetKey(key string) ([]byte, error) {
var value []byte
err := s.db.View(func(tx *bolt.Tx) error {
// Assume bucket exists and has keys
bucket := tx.Bucket([]byte(dbBucket))
if bucket == nil {
return errors.New(fmt.Sprintf("bucket %s not found", dbBucket))
}
value = bucket.Get([]byte(key))
return nil
})
if err != nil {
return []byte{}, err
}
return value, nil
}
func (s *Storage) RemoveKey(key string) error {
err := s.db.Update(func(tx *bolt.Tx) error {
// Assume bucket exists and has keys
bucket := tx.Bucket([]byte(dbBucket))
if bucket == nil {
return errors.New(fmt.Sprintf("bucket %s not found", dbBucket))
}
err := bucket.Delete([]byte(key))
if err != nil {
return errors.New(fmt.Sprintf("cannot delete %s: %s", key, err))
}
return nil
})
if err != nil {
return err
}
return nil
}