Skip to content

Commit fa703f9

Browse files
committed
Add BoltDB integration for data storage and retrieval
1 parent cf78de6 commit fa703f9

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/spf13/pflag v1.0.6 // indirect
1414
github.com/valyala/bytebufferpool v1.0.0 // indirect
1515
github.com/valyala/fasthttp v1.59.0 // indirect
16+
go.etcd.io/bbolt v1.4.0 // indirect
1617
golang.org/x/sys v0.30.0 // indirect
1718
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
1819
)

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyC
2626
github.com/valyala/fasthttp v1.59.0 h1:Qu0qYHfXvPk1mSLNqcFtEk6DpxgA26hy6bmydotDpRI=
2727
github.com/valyala/fasthttp v1.59.0/go.mod h1:GTxNb9Bc6r2a9D0TWNSPwDz78UxnTGBViY3xZNEqyYU=
2828
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=
2931
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
3032
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3133
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=

src/database/database.go

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
}

src/main.go

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"zeroctl/src/config"
77
"zeroctl/src/daemon"
8+
"zeroctl/src/database"
89
"zeroctl/src/types"
910

1011
"github.com/spf13/cobra"
@@ -16,6 +17,12 @@ func main() {
1617
os.Exit(1)
1718
}
1819

20+
if err := database.InitBoltDB(); err != nil {
21+
fmt.Println("Error:", err)
22+
os.Exit(1)
23+
}
24+
defer database.CloseBoltDB()
25+
1926
rootCmd := &cobra.Command{
2027
Use: "zeroctl",
2128
Short: "zeroctl is a custom CLI tool",

0 commit comments

Comments
 (0)