├── .gitignore ├── LICENSE ├── README.md ├── assets ├── listbuckets.gif └── viewbucket.gif ├── bolter.go ├── go.mod ├── go.sum ├── less_wrap_formatter.go ├── sample-db └── test-db.bolt └── table_formatter.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | 26 | .DS_STORE -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Hasit Mistry 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # bolter 2 | 3 | View BoltDB file in your terminal 4 | 5 | ![List all items](assets/viewbucket.gif) 6 | 7 | ## Install 8 | 9 | ``` 10 | $ go get -u github.com/hasit/bolter 11 | ``` 12 | 13 | ## Usage 14 | 15 | ``` 16 | $ bolter [global options] 17 | 18 | GLOBAL OPTIONS: 19 | --file FILE, -f FILE boltdb FILE to view 20 | --bucket BUCKET, -b BUCKET boltdb BUCKET to view 21 | --machine, -m key=value format 22 | --help, -h show help 23 | --version, -v print the version 24 | ``` 25 | 26 | ### List all buckets 27 | 28 | ``` 29 | $ bolter -f emails.db 30 | +---------------------------+ 31 | | BUCKETS | 32 | +---------------------------+ 33 | | john@doe.com | 34 | | jane@roe.com | 35 | | sample@example.com | 36 | | test@test.com | 37 | +---------------------------+ 38 | ``` 39 | 40 | ### List all items in bucket 41 | 42 | ``` 43 | $ bolter -f emails.db -b john@doe.com 44 | Bucket: john@doe.com 45 | +---------------+---------------------+ 46 | | KEY | VALUE | 47 | +---------------+---------------------+ 48 | | emailLastSent | | 49 | | subLocation | | 50 | | subTag | | 51 | | userActive | true | 52 | | userCreatedOn | 2016-10-28 07:21:49 | 53 | | userEmail | john@doe.com | 54 | | userFirstName | John | 55 | | userLastName | Doe | 56 | +---------------+---------------------+ 57 | ``` 58 | 59 | ### Nested buckets 60 | 61 | You can easily list all items in a nested bucket: 62 | 63 | ``` 64 | $ bolter -f my.db 65 | +-----------+ 66 | | BUCKETS | 67 | +-----------+ 68 | | root | 69 | +-----------+ 70 | 71 | $ bolter -f my.db -b root 72 | Bucket: root 73 | +---------+---------+ 74 | | KEY | VALUE | 75 | +---------+---------+ 76 | | nested* | | 77 | +---------+---------+ 78 | 79 | * means the key ('nested' in this case) is a bucket. 80 | 81 | $ bolter -f my.db -b root.nested 82 | Bucket: root.nested 83 | +---------+---------+ 84 | | KEY | VALUE | 85 | +---------+---------+ 86 | | mykey | myvalue | 87 | +---------+---------+ 88 | ``` 89 | 90 | ### Machine friendly output 91 | 92 | ``` 93 | $ bolter -f emails.db -m 94 | john@doe.com 95 | jane@roe.com 96 | sample@example.com 97 | test@test.com 98 | 99 | $ bolter -f emails.db -b john@doe.com -m 100 | emailLastSent= 101 | subLocation= 102 | subTag= 103 | userActive=true 104 | userCreatedOn=2016-10-28 07:21:49 105 | userEmail=john@doe.com 106 | userFirstName=John 107 | userLastName=Doe 108 | nested-bucket*= 109 | ``` 110 | 111 | ## Contribute 112 | 113 | Feel free to ask questions, post issues and open pull requests. My only requirement is that you run `gofmt` on your code before you send in a PR. 114 | -------------------------------------------------------------------------------- /assets/listbuckets.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hasit/bolter/68d80baa762063bfc42fd3cefd8c41089ba034b6/assets/listbuckets.gif -------------------------------------------------------------------------------- /assets/viewbucket.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hasit/bolter/68d80baa762063bfc42fd3cefd8c41089ba034b6/assets/viewbucket.gif -------------------------------------------------------------------------------- /bolter.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io" 7 | "log" 8 | "os" 9 | "strings" 10 | 11 | kval "github.com/kval-access-language/kval-boltdb" 12 | "github.com/urfave/cli" 13 | ) 14 | 15 | // Terminal lines... 16 | const instructionLine = "> Enter bucket to explore (CTRL-X to quit, CTRL-B to go back, ENTER to go back to ROOT Bucket):" 17 | const goingBack = "> Going back..." 18 | 19 | func main() { 20 | var file string 21 | var noValues bool 22 | var useMore bool 23 | 24 | cli.AppHelpTemplate = `NAME: 25 | {{.Name}} - {{.Usage}} 26 | 27 | VERSION: 28 | {{.Version}} 29 | 30 | USAGE: 31 | {{.HelpName}} {{if .VisibleFlags}}[global options]{{end}} 32 | 33 | GLOBAL OPTIONS: 34 | {{range .VisibleFlags}}{{.}} 35 | {{end}} 36 | AUTHOR: 37 | {{range .Authors}}{{ . }}{{end}} 38 | COPYRIGHT: 39 | {{.Copyright}} 40 | ` 41 | app := cli.NewApp() 42 | app.Name = "bolter" 43 | app.Usage = "view boltdb file interactively in your terminal" 44 | app.Version = "2.0.1" 45 | app.Authors = []*cli.Author{ 46 | &cli.Author{ 47 | Name: "Hasit Mistry", 48 | Email: "hasitnm@gmail.com", 49 | }, 50 | } 51 | app.Copyright = "(c) 2016 Hasit Mistry" 52 | app.Flags = []cli.Flag{ 53 | &cli.StringFlag{ 54 | Name: "file, f", 55 | Usage: "boltdb `FILE` to view", 56 | Destination: &file, 57 | }, 58 | &cli.BoolFlag{ 59 | Name: "no-values", 60 | Usage: "use if values are huge and/or not printable", 61 | Destination: &noValues, 62 | }, 63 | &cli.BoolFlag{ 64 | Name: "more", 65 | Usage: "use `more` to print all listings. Should be available in path", 66 | Destination: &useMore, 67 | }, 68 | } 69 | app.Action = func(c *cli.Context) error { 70 | if file == "" { 71 | cli.ShowAppHelp(c) 72 | return nil 73 | } 74 | 75 | var formatter formatter = &tableFormatter{ 76 | noValues: noValues, 77 | } 78 | if useMore { 79 | formatter = &moreWrapFormatter{ 80 | formatter: formatter, 81 | } 82 | } 83 | 84 | var i impl 85 | i = impl{fmt: formatter} 86 | if _, err := os.Stat(file); os.IsNotExist(err) { 87 | log.Fatal(err) 88 | return err 89 | } 90 | i.initDB(file) 91 | defer kval.Disconnect(i.kb) 92 | 93 | i.readInput() 94 | 95 | return nil 96 | } 97 | app.Run(os.Args) 98 | } 99 | 100 | func (i *impl) readInput() { 101 | i.listBuckets() 102 | scanner := bufio.NewScanner(os.Stdin) 103 | for scanner.Scan() { 104 | bucket := scanner.Text() 105 | fmt.Fprintln(os.Stdout, "") 106 | switch bucket { 107 | case "\x18": 108 | return 109 | case "\x02": 110 | if !strings.Contains(i.loc, "") || !strings.Contains(i.loc, ">>") { 111 | fmt.Fprintf(os.Stdout, "%s\n", goingBack) 112 | i.loc = "" 113 | i.listBuckets() 114 | } else { 115 | i.listBucketItems(bucket, true) 116 | } 117 | case "": 118 | i.listBuckets() 119 | default: 120 | i.listBucketItems(bucket, false) 121 | } 122 | bucket = "" 123 | } 124 | } 125 | 126 | type formatter interface { 127 | DumpBuckets(io.Writer, []bucket) 128 | DumpBucketItems(io.Writer, string, []item) 129 | } 130 | 131 | type impl struct { 132 | kb kval.Kvalboltdb 133 | fmt formatter 134 | bucket string 135 | loc string // navigation, what is our requested location in the store? 136 | cache string // navigation, cache our last location to move back to 137 | root bool // navigation, are we @ root bucket? 138 | } 139 | 140 | type item struct { 141 | Key string 142 | Value string 143 | } 144 | 145 | type bucket struct { 146 | Name string 147 | } 148 | 149 | func (i *impl) initDB(file string) { 150 | var err error 151 | // Connect to KVAL using KVAL default mechanism 152 | // Can also use regular open plus perms, and kval.Attach() 153 | i.kb, err = kval.Connect(file) 154 | if err != nil { 155 | log.Fatal(err) 156 | } 157 | } 158 | 159 | func (i *impl) updateLoc(bucket string, goBack bool) string { 160 | 161 | // we've probably an invalid value and want to display 162 | // ourselves again... 163 | if bucket == i.cache { 164 | i.loc = bucket 165 | return i.loc 166 | } 167 | 168 | // handle goback 169 | if goBack { 170 | s := strings.Split(i.loc, ">>") 171 | i.loc = strings.Join(s[:len(s)-1], ">>") 172 | i.bucket = strings.Trim(s[len(s)-2], " ") 173 | return i.loc 174 | } 175 | 176 | // handle location on merit... 177 | if i.loc == "" { 178 | i.loc = bucket 179 | i.bucket = bucket 180 | } else { 181 | i.loc = i.loc + " >> " + bucket 182 | i.bucket = bucket 183 | } 184 | return i.loc 185 | } 186 | 187 | func (i *impl) listBucketItems(bucket string, goBack bool) { 188 | items := []item{} 189 | getQuery := i.updateLoc(bucket, goBack) 190 | if getQuery != "" { 191 | fmt.Fprintf(os.Stdout, "Query: "+getQuery+"\n\n") 192 | res, err := kval.Query(i.kb, "GET "+getQuery) 193 | if err != nil { 194 | if err.Error() == "No Keys: There are no key::value pairs in this bucket" { 195 | // no values in this bucket 196 | fmt.Fprintf(os.Stdout, "> There are no key::value pairs in this bucket\n") 197 | if i.root == true { 198 | i.listBuckets() 199 | return 200 | } 201 | i.listBucketItems(i.loc, true) 202 | } else if err.Error() != "Cannot GOTO bucket, bucket not found" { 203 | log.Fatal(err) 204 | } else { 205 | fmt.Fprintf(os.Stdout, "> Bucket not found\n") 206 | if i.root == true { 207 | i.listBuckets() 208 | return 209 | } 210 | i.listBucketItems(i.loc, true) 211 | } 212 | } 213 | if len(res.Result) == 0 { 214 | fmt.Fprintf(os.Stdout, "Invalid request.\n\n") 215 | i.listBucketItems(i.cache, false) 216 | return 217 | } 218 | 219 | for k, v := range res.Result { 220 | if v == kval.Nestedbucket { 221 | k = k + "*" 222 | v = "" 223 | } 224 | items = append(items, item{Key: string(k), Value: string(v)}) 225 | } 226 | fmt.Fprintf(os.Stdout, "Bucket: %s\n", bucket) 227 | i.fmt.DumpBucketItems(os.Stdout, i.bucket, items) 228 | i.root = false // success this far means we're not at ROOT 229 | i.cache = getQuery // so we can also set the query cache for paging 230 | outputInstructionline() 231 | } 232 | } 233 | 234 | func (i *impl) listBuckets() { 235 | i.root = true 236 | i.loc = "" 237 | 238 | buckets := []bucket{} 239 | 240 | res, err := kval.Query(i.kb, "GET _") // KVAL: "GET _" will return ROOT 241 | if err != nil { 242 | log.Fatal(err) 243 | } 244 | for k := range res.Result { 245 | buckets = append(buckets, bucket{Name: string(k) + "*"}) 246 | } 247 | 248 | fmt.Fprint(os.Stdout, "DB Layout:\n\n") 249 | i.fmt.DumpBuckets(os.Stdout, buckets) 250 | outputInstructionline() 251 | } 252 | 253 | func outputInstructionline() { 254 | fmt.Fprintf(os.Stdout, "\n%s\n\n", instructionLine) 255 | } 256 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/hasit/bolter 2 | 3 | go 1.13 4 | 5 | require ( 6 | github.com/boltdb/bolt v1.3.1 // indirect 7 | github.com/kval-access-language/kval-boltdb v0.0.0-20170330045345-f3797777c95e 8 | github.com/kval-access-language/kval-parse v0.0.0-20170504112528-b96aa5a26330 // indirect 9 | github.com/kval-access-language/kval-scanner v0.0.0-20170504112421-4f097cacd289 // indirect 10 | github.com/olekukonko/tablewriter v0.0.2 11 | github.com/pkg/errors v0.8.1 // indirect 12 | github.com/urfave/cli v1.22.1 13 | golang.org/x/sys v0.0.0-20200519105757-fe76b779f299 // indirect 14 | ) 15 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= 2 | github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4= 3 | github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps= 4 | github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= 5 | github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= 6 | github.com/kval-access-language/kval-boltdb v0.0.0-20170330045345-f3797777c95e h1:6eQQkLvMgIY9ecZKVtEsKdUeApg6qkNl8IwJp6cZBNg= 7 | github.com/kval-access-language/kval-boltdb v0.0.0-20170330045345-f3797777c95e/go.mod h1:CJfrSSteer+JJgIkdw/LU7Ss1CiZ6G/+oIfo4nJ1P/s= 8 | github.com/kval-access-language/kval-parse v0.0.0-20170504112528-b96aa5a26330 h1:u+bu63lKw/77wcyhzRB9YwipGEa4l3et/hu4xkcX2cQ= 9 | github.com/kval-access-language/kval-parse v0.0.0-20170504112528-b96aa5a26330/go.mod h1:sIViryZLFGwtty1cz01GVfqCzHR2j5O7ZCcuYiGJAJA= 10 | github.com/kval-access-language/kval-scanner v0.0.0-20170504112421-4f097cacd289 h1:xp6dE/eMUsoTqp/A4p846HSDlETgYPcW+gmtSW7G0tc= 11 | github.com/kval-access-language/kval-scanner v0.0.0-20170504112421-4f097cacd289/go.mod h1:YAawD4QhDnNbLB6455T6SheCYnZh90kII4jhq5HWzAQ= 12 | github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y= 13 | github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= 14 | github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo= 15 | github.com/olekukonko/tablewriter v0.0.2 h1:sq53g+DWf0J6/ceFUHpQ0nAEb6WgM++fq16MZ91cS6o= 16 | github.com/olekukonko/tablewriter v0.0.2/go.mod h1:rSAaSIOAGT9odnlyGlUfAJaoc5w2fSBUmeGDbRWPxyQ= 17 | github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= 18 | github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 19 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 20 | github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= 21 | github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= 22 | github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= 23 | github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= 24 | github.com/urfave/cli v1.22.1 h1:+mkCCcOFKPnCmVYVcURKps1Xe+3zP90gSYGNfRkjoIY= 25 | github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= 26 | golang.org/x/sys v0.0.0-20200519105757-fe76b779f299 h1:DYfZAGf2WMFjMxbgTjaC+2HC7NkNAQs+6Q8b9WEB/F4= 27 | golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 28 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 29 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 30 | -------------------------------------------------------------------------------- /less_wrap_formatter.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "io" 5 | "os/exec" 6 | ) 7 | 8 | type moreWrapFormatter struct { 9 | formatter formatter 10 | } 11 | 12 | func (mf moreWrapFormatter) wrapDump(w io.Writer, dump func(io.Writer)) { 13 | lessCmd := exec.Command("more") 14 | pipeR, pipeW := io.Pipe() 15 | go func() { 16 | dump(pipeW) 17 | pipeW.Close() 18 | }() 19 | lessCmd.Stdin = pipeR 20 | lessCmd.Stdout = w 21 | lessCmd.Run() 22 | } 23 | 24 | func (mf moreWrapFormatter) DumpBuckets(w io.Writer, buckets []bucket) { 25 | mf.wrapDump(w, func(w io.Writer) { 26 | mf.formatter.DumpBuckets(w, buckets) 27 | }) 28 | } 29 | 30 | func (mf moreWrapFormatter) DumpBucketItems(w io.Writer, bucket string, items []item) { 31 | mf.wrapDump(w, func(w io.Writer) { 32 | mf.formatter.DumpBucketItems(w, bucket, items) 33 | }) 34 | } 35 | -------------------------------------------------------------------------------- /sample-db/test-db.bolt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hasit/bolter/68d80baa762063bfc42fd3cefd8c41089ba034b6/sample-db/test-db.bolt -------------------------------------------------------------------------------- /table_formatter.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "io" 5 | 6 | "github.com/olekukonko/tablewriter" 7 | ) 8 | 9 | type tableFormatter struct { 10 | noValues bool 11 | } 12 | 13 | func (tf tableFormatter) DumpBuckets(w io.Writer, buckets []bucket) { 14 | table := tablewriter.NewWriter(w) 15 | table.SetHeader([]string{"Buckets"}) 16 | for _, b := range buckets { 17 | row := []string{b.Name} 18 | table.Append(row) 19 | } 20 | table.Render() 21 | } 22 | 23 | func (tf tableFormatter) DumpBucketItems(w io.Writer, bucket string, items []item) { 24 | table := tablewriter.NewWriter(w) 25 | table.SetHeader([]string{"Key", "Value"}) 26 | for _, item := range items { 27 | var row []string 28 | if tf.noValues { 29 | row = []string{item.Key, ""} 30 | } else { 31 | row = []string{item.Key, item.Value} 32 | } 33 | table.Append(row) 34 | } 35 | table.Render() 36 | } 37 | --------------------------------------------------------------------------------