-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
61 lines (54 loc) · 1.41 KB
/
main.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
package main
import (
"fmt"
"github.com/Sora233/buntdb-cli/cli"
"github.com/Sora233/buntdb-cli/db"
"github.com/alecthomas/kong"
"github.com/c-bata/go-prompt"
"os"
"path"
"strings"
)
var CLI struct {
Path string `arg:"" optional:"" help:"buntudb file path, default a tempfile"`
Debug bool `optional:"" help:"enable debug output"`
Version version `optional:"" short:"v" help:"print version"`
}
func main() {
kong.Parse(&CLI, kong.UsageOnError(), kong.Name("buntdb-cli"))
if CLI.Debug {
cli.Debug = true
}
if CLI.Path == "" {
CLI.Path = db.GetTempDbPath("buntdb-cli")
}
err := db.InitBuntDB(CLI.Path)
if err != nil {
fmt.Printf("ERR: %v\n", err)
os.Exit(1)
}
defer db.Close()
p := prompt.New(
cli.BuntdbExecutor,
cli.BuntdbCompleter,
prompt.OptionTitle("buntdb-cli: an interactive buntdb shell client"),
prompt.OptionLivePrefix(func() (prefix string, useLivePrefix bool) {
tx, rw := db.GetCurrentTransaction()
if tx != nil {
return path.Base(db.GetDbPath()) + fmt.Sprintf("(%v)", db.RWDescribe(rw)) + "> ", true
} else {
return path.Base(db.GetDbPath()) + "> ", true
}
}),
prompt.OptionSetExitCheckerOnInput(func(in string, breakline bool) bool {
return strings.TrimSpace(in) == "exit" && breakline
}),
)
p.Run()
tx, _ := db.GetCurrentTransaction()
if tx != nil {
fmt.Println("WARN: current transaction will rollback")
db.Rollback()
}
fmt.Println("bye")
}