File tree 4 files changed +78
-0
lines changed
4 files changed +78
-0
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ require (
13
13
github.com/spf13/pflag v1.0.6 // indirect
14
14
github.com/valyala/bytebufferpool v1.0.0 // indirect
15
15
github.com/valyala/fasthttp v1.59.0 // indirect
16
+ go.etcd.io/bbolt v1.4.0 // indirect
16
17
golang.org/x/sys v0.30.0 // indirect
17
18
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
18
19
)
Original file line number Diff line number Diff line change @@ -26,6 +26,8 @@ github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyC
26
26
github.com/valyala/fasthttp v1.59.0 h1:Qu0qYHfXvPk1mSLNqcFtEk6DpxgA26hy6bmydotDpRI =
27
27
github.com/valyala/fasthttp v1.59.0 /go.mod h1:GTxNb9Bc6r2a9D0TWNSPwDz78UxnTGBViY3xZNEqyYU =
28
28
github.com/xyproto/randomstring v1.0.5 /go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E =
29
+ go.etcd.io/bbolt v1.4.0 h1:TU77id3TnN/zKr7CO/uk+fBCwF2jGcMuw2B/FMAzYIk =
30
+ go.etcd.io/bbolt v1.4.0 /go.mod h1:AsD+OCi/qPN1giOX1aiLAha3o1U8rAz65bvN4j0sRuk =
29
31
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ =
30
32
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 /go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg =
31
33
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc =
Original file line number Diff line number Diff line change
1
+ package database
2
+
3
+ import (
4
+ "encoding/json"
5
+ "errors"
6
+ "os"
7
+ "path/filepath"
8
+ "time"
9
+
10
+ bolt "go.etcd.io/bbolt"
11
+ )
12
+
13
+ var dbPath = filepath .Join (os .Getenv ("HOME" ), ".config/zeroctl/zero.db" )
14
+ var DB * bolt.DB
15
+
16
+ func InitBoltDB () error {
17
+ if err := os .MkdirAll (filepath .Dir (dbPath ), 0755 ); err != nil {
18
+ return err
19
+ }
20
+
21
+ var err error
22
+ DB , err = bolt .Open (dbPath , 0600 , & bolt.Options {Timeout : 1 * time .Second })
23
+ if err != nil {
24
+ return err
25
+ }
26
+
27
+ return DB .Update (func (tx * bolt.Tx ) error {
28
+ _ , err := tx .CreateBucketIfNotExists ([]byte ("cache" ))
29
+ return err
30
+ })
31
+ }
32
+
33
+ func CloseBoltDB () {
34
+ if DB != nil {
35
+ DB .Close ()
36
+ }
37
+ }
38
+
39
+ func StoreJsonData (key string , value interface {}) error {
40
+ data , err := json .Marshal (value )
41
+ if err != nil {
42
+ return err
43
+ }
44
+
45
+ return DB .Update (func (tx * bolt.Tx ) error {
46
+ b := tx .Bucket ([]byte ("cache" ))
47
+ if b == nil {
48
+ return errors .New ("cache bucket does not exist" )
49
+ }
50
+ return b .Put ([]byte (key ), data )
51
+ })
52
+ }
53
+
54
+ func GetJsonData (key string , dest interface {}) error {
55
+ return DB .View (func (tx * bolt.Tx ) error {
56
+ b := tx .Bucket ([]byte ("cache" ))
57
+ if b == nil {
58
+ return errors .New ("cache bucket does not exist" )
59
+ }
60
+
61
+ data := b .Get ([]byte (key ))
62
+ if data == nil {
63
+ return errors .New ("key not found" )
64
+ }
65
+
66
+ return json .Unmarshal (data , dest )
67
+ })
68
+ }
Original file line number Diff line number Diff line change 5
5
"os"
6
6
"zeroctl/src/config"
7
7
"zeroctl/src/daemon"
8
+ "zeroctl/src/database"
8
9
"zeroctl/src/types"
9
10
10
11
"github.com/spf13/cobra"
@@ -16,6 +17,12 @@ func main() {
16
17
os .Exit (1 )
17
18
}
18
19
20
+ if err := database .InitBoltDB (); err != nil {
21
+ fmt .Println ("Error:" , err )
22
+ os .Exit (1 )
23
+ }
24
+ defer database .CloseBoltDB ()
25
+
19
26
rootCmd := & cobra.Command {
20
27
Use : "zeroctl" ,
21
28
Short : "zeroctl is a custom CLI tool" ,
You can’t perform that action at this time.
0 commit comments