-
Notifications
You must be signed in to change notification settings - Fork 5
/
boltcli.go
56 lines (48 loc) · 973 Bytes
/
boltcli.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
package main
import (
"flag"
"fmt"
"log"
"os"
bolt "go.etcd.io/bbolt"
)
var (
// DB is the global boltdb instance which will be inited in the beginning.
DB *bolt.DB
// DbPath is the path of given db file
DbPath string
shouldPrintVersion = flag.Bool("version", false, "Output version and exit.")
version = "1.0.0"
scriptPath = flag.String("e", "", "Eval the Lua script in given path")
)
func initDB(dbPath string) {
db, err := bolt.Open(dbPath, 0600, nil)
if err != nil {
log.Fatalf("Could not open %s: %v", dbPath, err)
}
DB = db
DbPath = dbPath
}
func printVersion() {
fmt.Printf("boltcli %s\n", version)
}
func main() {
flag.Parse()
if *shouldPrintVersion {
printVersion()
os.Exit(0)
}
if flag.NArg() < 1 {
log.Fatalf("database filename is required.")
}
initDB(flag.Arg(0))
defer DB.Close()
if *scriptPath != "" {
err := StartScript(*scriptPath)
if err != nil {
log.Fatalln(err)
}
} else {
StartCli()
}
}