forked from hasit/bolter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bolter.go
255 lines (226 loc) · 5.38 KB
/
bolter.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"strings"
kval "github.com/kval-access-language/kval-boltdb"
"github.com/urfave/cli"
)
// Terminal lines...
const instructionLine = "> Enter bucket to explore (CTRL-X to quit, CTRL-B to go back, ENTER to go back to ROOT Bucket):"
const goingBack = "> Going back..."
func main() {
var file string
var noValues bool
var useMore bool
cli.AppHelpTemplate = `NAME:
{{.Name}} - {{.Usage}}
VERSION:
{{.Version}}
USAGE:
{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}
GLOBAL OPTIONS:
{{range .VisibleFlags}}{{.}}
{{end}}
AUTHOR:
{{range .Authors}}{{ . }}{{end}}
COPYRIGHT:
{{.Copyright}}
`
app := cli.NewApp()
app.Name = "bolter"
app.Usage = "view boltdb file interactively in your terminal"
app.Version = "2.0.1"
app.Authors = []*cli.Author{
&cli.Author{
Name: "Hasit Mistry",
Email: "hasitnm@gmail.com",
},
}
app.Copyright = "(c) 2016 Hasit Mistry"
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "file, f",
Usage: "boltdb `FILE` to view",
Destination: &file,
},
&cli.BoolFlag{
Name: "no-values",
Usage: "use if values are huge and/or not printable",
Destination: &noValues,
},
&cli.BoolFlag{
Name: "more",
Usage: "use `more` to print all listings. Should be available in path",
Destination: &useMore,
},
}
app.Action = func(c *cli.Context) error {
if file == "" {
cli.ShowAppHelp(c)
return nil
}
var formatter formatter = &tableFormatter{
noValues: noValues,
}
if useMore {
formatter = &moreWrapFormatter{
formatter: formatter,
}
}
var i impl
i = impl{fmt: formatter}
if _, err := os.Stat(file); os.IsNotExist(err) {
log.Fatal(err)
return err
}
i.initDB(file)
defer kval.Disconnect(i.kb)
i.readInput()
return nil
}
app.Run(os.Args)
}
func (i *impl) readInput() {
i.listBuckets()
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
bucket := scanner.Text()
fmt.Fprintln(os.Stdout, "")
switch bucket {
case "\x18":
return
case "\x02":
if !strings.Contains(i.loc, "") || !strings.Contains(i.loc, ">>") {
fmt.Fprintf(os.Stdout, "%s\n", goingBack)
i.loc = ""
i.listBuckets()
} else {
i.listBucketItems(bucket, true)
}
case "":
i.listBuckets()
default:
i.listBucketItems(bucket, false)
}
bucket = ""
}
}
type formatter interface {
DumpBuckets(io.Writer, []bucket)
DumpBucketItems(io.Writer, string, []item)
}
type impl struct {
kb kval.Kvalboltdb
fmt formatter
bucket string
loc string // navigation, what is our requested location in the store?
cache string // navigation, cache our last location to move back to
root bool // navigation, are we @ root bucket?
}
type item struct {
Key string
Value string
}
type bucket struct {
Name string
}
func (i *impl) initDB(file string) {
var err error
// Connect to KVAL using KVAL default mechanism
// Can also use regular open plus perms, and kval.Attach()
i.kb, err = kval.Connect(file)
if err != nil {
log.Fatal(err)
}
}
func (i *impl) updateLoc(bucket string, goBack bool) string {
// we've probably an invalid value and want to display
// ourselves again...
if bucket == i.cache {
i.loc = bucket
return i.loc
}
// handle goback
if goBack {
s := strings.Split(i.loc, ">>")
i.loc = strings.Join(s[:len(s)-1], ">>")
i.bucket = strings.Trim(s[len(s)-2], " ")
return i.loc
}
// handle location on merit...
if i.loc == "" {
i.loc = bucket
i.bucket = bucket
} else {
i.loc = i.loc + " >> " + bucket
i.bucket = bucket
}
return i.loc
}
func (i *impl) listBucketItems(bucket string, goBack bool) {
items := []item{}
getQuery := i.updateLoc(bucket, goBack)
if getQuery != "" {
fmt.Fprintf(os.Stdout, "Query: "+getQuery+"\n\n")
res, err := kval.Query(i.kb, "GET "+getQuery)
if err != nil {
if err.Error() == "No Keys: There are no key::value pairs in this bucket" {
// no values in this bucket
fmt.Fprintf(os.Stdout, "> There are no key::value pairs in this bucket\n")
if i.root == true {
i.listBuckets()
return
}
i.listBucketItems(i.loc, true)
} else if err.Error() != "Cannot GOTO bucket, bucket not found" {
log.Fatal(err)
} else {
fmt.Fprintf(os.Stdout, "> Bucket not found\n")
if i.root == true {
i.listBuckets()
return
}
i.listBucketItems(i.loc, true)
}
}
if len(res.Result) == 0 {
fmt.Fprintf(os.Stdout, "Invalid request.\n\n")
i.listBucketItems(i.cache, false)
return
}
for k, v := range res.Result {
if v == kval.Nestedbucket {
k = k + "*"
v = ""
}
items = append(items, item{Key: string(k), Value: string(v)})
}
fmt.Fprintf(os.Stdout, "Bucket: %s\n", bucket)
i.fmt.DumpBucketItems(os.Stdout, i.bucket, items)
i.root = false // success this far means we're not at ROOT
i.cache = getQuery // so we can also set the query cache for paging
outputInstructionline()
}
}
func (i *impl) listBuckets() {
i.root = true
i.loc = ""
buckets := []bucket{}
res, err := kval.Query(i.kb, "GET _") // KVAL: "GET _" will return ROOT
if err != nil {
log.Fatal(err)
}
for k := range res.Result {
buckets = append(buckets, bucket{Name: string(k) + "*"})
}
fmt.Fprint(os.Stdout, "DB Layout:\n\n")
i.fmt.DumpBuckets(os.Stdout, buckets)
outputInstructionline()
}
func outputInstructionline() {
fmt.Fprintf(os.Stdout, "\n%s\n\n", instructionLine)
}