Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
3c25a93d4d | |||
930d064962 | |||
4a7ab1bc85 | |||
7b683d5c71 | |||
4ab1f201c5 |
6
.gitignore
vendored
6
.gitignore
vendored
@ -22,16 +22,10 @@ _cgo_export.*
|
||||
|
||||
_testmain.go
|
||||
|
||||
# Build Directory
|
||||
build
|
||||
|
||||
# Binaries
|
||||
*.exe
|
||||
boltbrowser
|
||||
boltbrowser.*
|
||||
|
||||
# Test Database
|
||||
test.db
|
||||
*.db
|
||||
|
||||
|
||||
|
12
README.md
12
README.md
@ -3,9 +3,9 @@ boltbrowser
|
||||
|
||||
A CLI Browser for BoltDB Files
|
||||
|
||||
![Image of About Screen](https://git.bullercodeworks.com/brian/boltbrowser/raw/branch/master/build/aboutscreen.png)
|
||||
![Image of About Screen](http://bullercodeworks.com/boltbrowser/ss2.png)
|
||||
|
||||
![Image of Main Browser](https://git.bullercodeworks.com/brian/boltbrowser/raw/branch/master/build/mainscreen.png)
|
||||
![Image of Main Browser](http://bullercodeworks.com/boltbrowser/ss1.png)
|
||||
|
||||
Installing
|
||||
----------
|
||||
@ -20,7 +20,13 @@ Then you'll have `boltbrowser` in your path.
|
||||
|
||||
Pre-built Binaries
|
||||
------------------
|
||||
Pre-build binaries are available on the [Releases Page](https://github.com/br0xen/boltbrowser/releases).
|
||||
Here are pre-built binaries:
|
||||
* [Linux 64-bit](https://git.bullercodeworks.com/brian/boltbrowser/releases/download/2.0/boltbrowser.linux64.zip)
|
||||
* [Linux 32-bit](https://git.bullercodeworks.com/brian/boltbrowser/releases/download/2.0/boltbrowser.linux386.zip)
|
||||
* [Linux Arm](https://git.bullercodeworks.com/brian/boltbrowser/releases/download/2.0/boltbrowser.linuxarm.zip)
|
||||
* [Windows 64-bit](https://git.bullercodeworks.com/brian/boltbrowser/releases/download/2.0/boltbrowser.win64.exe.zip)
|
||||
* [Windows 32-bit](https://git.bullercodeworks.com/brian/boltbrowser/releases/download/2.0/boltbrowser.win386.exe.zip)
|
||||
* [Mac OS](https://git.bullercodeworks.com/brian/boltbrowser/releases/download/2.0/boltbrowser.darwin64.zip)
|
||||
|
||||
Usage
|
||||
-----
|
||||
|
519
bolt_model.go
519
bolt_model.go
@ -1,12 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"go.etcd.io/bbolt"
|
||||
"github.com/boltdb/bolt"
|
||||
)
|
||||
|
||||
/*
|
||||
@ -16,29 +17,117 @@ type BoltDB struct {
|
||||
buckets []BoltBucket
|
||||
}
|
||||
|
||||
type PathNode struct {
|
||||
name []byte
|
||||
dataType Datatype
|
||||
}
|
||||
|
||||
/*
|
||||
BoltBucket is just a struct representation of a Bucket in the Bolt DB
|
||||
*/
|
||||
type BoltBucket struct {
|
||||
name string
|
||||
pairs []BoltPair
|
||||
buckets []BoltBucket
|
||||
parent *BoltBucket
|
||||
expanded bool
|
||||
errorFlag bool
|
||||
isRoot bool
|
||||
name []byte
|
||||
nameDatatype Datatype
|
||||
pairs []BoltPair
|
||||
buckets []BoltBucket
|
||||
parent *BoltBucket
|
||||
expanded bool
|
||||
errorFlag bool
|
||||
}
|
||||
|
||||
func NewBoltBucket(parent *BoltBucket, name []byte) *BoltBucket {
|
||||
ret := new(BoltBucket)
|
||||
ret.parent = parent
|
||||
ret.name = name
|
||||
for _, dtk := range dataTypeNameSlice {
|
||||
if _, err := datatypes[dtk].ToString(name); err == nil {
|
||||
ret.nameDatatype = datatypes[dtk]
|
||||
break
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func (b *BoltBucket) GetPathNode() PathNode {
|
||||
return PathNode{
|
||||
name: b.name,
|
||||
dataType: b.nameDatatype,
|
||||
}
|
||||
}
|
||||
|
||||
func (b *BoltBucket) SetName(nm []byte) {
|
||||
b.name = nm
|
||||
}
|
||||
|
||||
/*
|
||||
BoltPair is just a struct representation of a Pair in the Bolt DB
|
||||
*/
|
||||
type BoltPair struct {
|
||||
parent *BoltBucket
|
||||
key string
|
||||
val string
|
||||
parent *BoltBucket
|
||||
key []byte
|
||||
val []byte
|
||||
keyDatatype Datatype
|
||||
valDatatype Datatype
|
||||
}
|
||||
|
||||
func (bd *BoltDB) getGenericFromPath(path []string) (*BoltBucket, *BoltPair, error) {
|
||||
func NewBoltPair(parent *BoltBucket, key, val []byte) *BoltPair {
|
||||
tp := BoltPair{
|
||||
parent: parent,
|
||||
key: key,
|
||||
val: val,
|
||||
}
|
||||
for _, dtk := range dataTypeNameSlice {
|
||||
if _, err := datatypes[dtk].ToString(key); err == nil {
|
||||
tp.keyDatatype = datatypes[dtk]
|
||||
break
|
||||
}
|
||||
}
|
||||
for _, dtk := range dataTypeNameSlice {
|
||||
if _, err := datatypes[dtk].ToString(val); err == nil {
|
||||
tp.valDatatype = datatypes[dtk]
|
||||
break
|
||||
}
|
||||
}
|
||||
return &tp
|
||||
}
|
||||
|
||||
func (p *BoltPair) GetPathNode() PathNode {
|
||||
return PathNode{
|
||||
name: p.key,
|
||||
dataType: p.keyDatatype,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
GetStringKey returns the key of the pair after passing it through it's
|
||||
Datatype ToString function
|
||||
*/
|
||||
func (p *BoltPair) GetStringKey() string {
|
||||
ret, err := p.keyDatatype.ToString(p.key)
|
||||
if err != nil {
|
||||
return "ERROR"
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
GetStringVal returns the val of the pair after passing it through it's
|
||||
Datatype ToString function
|
||||
*/
|
||||
func (p *BoltPair) GetStringVal() string {
|
||||
ret, err := p.valDatatype.ToString(p.val)
|
||||
if err != nil {
|
||||
return "ERROR"
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func (bd *BoltDB) getGenericFromStringPath(path []PathNode) (*BoltBucket, *BoltPair, error) {
|
||||
return bd.getGenericFromPath(path)
|
||||
}
|
||||
|
||||
func (bd *BoltDB) getGenericFromPath(path []PathNode) (*BoltBucket, *BoltPair, error) {
|
||||
// Check if 'path' leads to a pair
|
||||
p, err := bd.getPairFromPath(path)
|
||||
if err == nil {
|
||||
@ -53,19 +142,19 @@ func (bd *BoltDB) getGenericFromPath(path []string) (*BoltBucket, *BoltPair, err
|
||||
return nil, nil, errors.New("Invalid Path")
|
||||
}
|
||||
|
||||
func (bd *BoltDB) getBucketFromPath(path []string) (*BoltBucket, error) {
|
||||
func (bd *BoltDB) getBucketFromPath(path []PathNode) (*BoltBucket, error) {
|
||||
if len(path) > 0 {
|
||||
// Find the BoltBucket with a path == path
|
||||
var b *BoltBucket
|
||||
var err error
|
||||
// Find the root bucket
|
||||
b, err = memBolt.getBucket(path[0])
|
||||
b, err = memBolt.getBucket(path[0].name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(path) > 1 {
|
||||
for p := 1; p < len(path); p++ {
|
||||
b, err = b.getBucket(path[p])
|
||||
b, err = b.getBucket(path[p].name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -76,7 +165,7 @@ func (bd *BoltDB) getBucketFromPath(path []string) (*BoltBucket, error) {
|
||||
return nil, errors.New("Invalid Path")
|
||||
}
|
||||
|
||||
func (bd *BoltDB) getPairFromPath(path []string) (*BoltPair, error) {
|
||||
func (bd *BoltDB) getPairFromPath(path []PathNode) (*BoltPair, error) {
|
||||
if len(path) <= 0 {
|
||||
return nil, errors.New("No Path")
|
||||
}
|
||||
@ -85,11 +174,11 @@ func (bd *BoltDB) getPairFromPath(path []string) (*BoltPair, error) {
|
||||
return nil, err
|
||||
}
|
||||
// Found the bucket, pull out the pair
|
||||
p, err := b.getPair(path[len(path)-1])
|
||||
p, err := b.getPair(path[len(path)-1].name)
|
||||
return p, err
|
||||
}
|
||||
|
||||
func (bd *BoltDB) getVisibleItemCount(path []string) (int, error) {
|
||||
func (bd *BoltDB) getVisibleItemCount(path []PathNode) (int, error) {
|
||||
vis := 0
|
||||
var retErr error
|
||||
if len(path) == 0 {
|
||||
@ -124,12 +213,12 @@ func (bd *BoltDB) getVisibleItemCount(path []string) (int, error) {
|
||||
return vis, retErr
|
||||
}
|
||||
|
||||
func (bd *BoltDB) buildVisiblePathSlice(filter string) ([][]string, error) {
|
||||
var retSlice [][]string
|
||||
func (bd *BoltDB) buildVisiblePathSlice() ([][]PathNode, error) {
|
||||
var retSlice [][]PathNode
|
||||
var retErr error
|
||||
// The root path, recurse for root buckets
|
||||
for i := range bd.buckets {
|
||||
bktS, bktErr := bd.buckets[i].buildVisiblePathSlice([]string{}, filter)
|
||||
bktS, bktErr := bd.buckets[i].buildVisiblePathSlice([]PathNode{})
|
||||
if bktErr == nil {
|
||||
retSlice = append(retSlice, bktS...)
|
||||
} else {
|
||||
@ -140,30 +229,8 @@ func (bd *BoltDB) buildVisiblePathSlice(filter string) ([][]string, error) {
|
||||
return retSlice, retErr
|
||||
}
|
||||
|
||||
func (bd *BoltDB) isVisiblePath(path []string, filter string) bool {
|
||||
visPaths, err := bd.buildVisiblePathSlice(filter)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
for _, pth := range visPaths {
|
||||
if len(pth) != len(path) {
|
||||
continue
|
||||
}
|
||||
isVisible := true
|
||||
for i := range path {
|
||||
if path[i] != pth[i] {
|
||||
isVisible = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if isVisible {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
func (bd *BoltDB) getPrevVisiblePath(path []string, filter string) []string {
|
||||
visPaths, err := bd.buildVisiblePathSlice(filter)
|
||||
func (bd *BoltDB) getPrevVisiblePath(path []PathNode) []PathNode {
|
||||
visPaths, err := bd.buildVisiblePathSlice()
|
||||
if path == nil {
|
||||
if len(visPaths) > 0 {
|
||||
return visPaths[len(visPaths)-1]
|
||||
@ -174,7 +241,7 @@ func (bd *BoltDB) getPrevVisiblePath(path []string, filter string) []string {
|
||||
for idx, pth := range visPaths {
|
||||
isCurPath := true
|
||||
for i := range path {
|
||||
if len(pth) <= i || path[i] != pth[i] {
|
||||
if len(pth) <= i || !bytes.Equal(path[i].name, pth[i].name) {
|
||||
isCurPath = false
|
||||
break
|
||||
}
|
||||
@ -186,8 +253,8 @@ func (bd *BoltDB) getPrevVisiblePath(path []string, filter string) []string {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (bd *BoltDB) getNextVisiblePath(path []string, filter string) []string {
|
||||
visPaths, err := bd.buildVisiblePathSlice(filter)
|
||||
func (bd *BoltDB) getNextVisiblePath(path []PathNode) []PathNode {
|
||||
visPaths, err := bd.buildVisiblePathSlice()
|
||||
if path == nil {
|
||||
if len(visPaths) > 0 {
|
||||
return visPaths[0]
|
||||
@ -198,7 +265,7 @@ func (bd *BoltDB) getNextVisiblePath(path []string, filter string) []string {
|
||||
for idx, pth := range visPaths {
|
||||
isCurPath := true
|
||||
for i := range path {
|
||||
if len(pth) <= i || path[i] != pth[i] {
|
||||
if len(pth) <= i || !bytes.Equal(path[i].name, pth[i].name) {
|
||||
isCurPath = false
|
||||
break
|
||||
}
|
||||
@ -211,7 +278,7 @@ func (bd *BoltDB) getNextVisiblePath(path []string, filter string) []string {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (bd *BoltDB) toggleOpenBucket(path []string) error {
|
||||
func (bd *BoltDB) toggleOpenBucket(path []PathNode) error {
|
||||
// Find the BoltBucket with a path == path
|
||||
b, err := bd.getBucketFromPath(path)
|
||||
if err == nil {
|
||||
@ -220,7 +287,7 @@ func (bd *BoltDB) toggleOpenBucket(path []string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (bd *BoltDB) closeBucket(path []string) error {
|
||||
func (bd *BoltDB) closeBucket(path []PathNode) error {
|
||||
// Find the BoltBucket with a path == path
|
||||
b, err := bd.getBucketFromPath(path)
|
||||
if err == nil {
|
||||
@ -229,7 +296,7 @@ func (bd *BoltDB) closeBucket(path []string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (bd *BoltDB) openBucket(path []string) error {
|
||||
func (bd *BoltDB) openBucket(path []PathNode) error {
|
||||
// Find the BoltBucket with a path == path
|
||||
b, err := bd.getBucketFromPath(path)
|
||||
if err == nil {
|
||||
@ -238,9 +305,9 @@ func (bd *BoltDB) openBucket(path []string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (bd *BoltDB) getBucket(k string) (*BoltBucket, error) {
|
||||
func (bd *BoltDB) getBucket(k []byte) (*BoltBucket, error) {
|
||||
for i := range bd.buckets {
|
||||
if bd.buckets[i].name == k {
|
||||
if bytes.Equal(bd.buckets[i].name, k) {
|
||||
return &bd.buckets[i], nil
|
||||
}
|
||||
}
|
||||
@ -258,7 +325,7 @@ func (bd *BoltDB) syncOpenBuckets(shadow *BoltDB) {
|
||||
// First test this bucket
|
||||
for i := range bd.buckets {
|
||||
for j := range shadow.buckets {
|
||||
if bd.buckets[i].name == shadow.buckets[j].name {
|
||||
if bytes.Equal(bd.buckets[i].name, shadow.buckets[j].name) {
|
||||
bd.buckets[i].syncOpenBuckets(&shadow.buckets[j])
|
||||
}
|
||||
}
|
||||
@ -268,56 +335,57 @@ func (bd *BoltDB) syncOpenBuckets(shadow *BoltDB) {
|
||||
func (bd *BoltDB) refreshDatabase() *BoltDB {
|
||||
// Reload the database into memBolt
|
||||
memBolt = new(BoltDB)
|
||||
db.View(func(tx *bbolt.Tx) error {
|
||||
err := tx.ForEach(func(nm []byte, b *bbolt.Bucket) error {
|
||||
bb, err := readBucket(b)
|
||||
db.View(func(tx *bolt.Tx) error {
|
||||
return tx.ForEach(func(nm []byte, b *bolt.Bucket) error {
|
||||
bb, err := readBucket(nil, b, nm)
|
||||
if err == nil {
|
||||
bb.name = string(nm)
|
||||
bb.SetName(nm)
|
||||
bb.expanded = false
|
||||
memBolt.buckets = append(memBolt.buckets, *bb)
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
memBolt = new(BoltDB)
|
||||
// Check if there are key/values directly in the root
|
||||
bb, err := readBucket(tx.Cursor().Bucket())
|
||||
if err == nil {
|
||||
bb.isRoot = true
|
||||
bb.expanded = true
|
||||
memBolt.buckets = append(memBolt.buckets, *bb)
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
return err
|
||||
})
|
||||
return memBolt
|
||||
}
|
||||
|
||||
/*
|
||||
GetPath returns the database path leading to this BoltBucket
|
||||
GetStringName returns the name of the bucket after passing it through it's
|
||||
Datatype ToString function
|
||||
*/
|
||||
func (b *BoltBucket) GetPath() []string {
|
||||
if b.parent != nil {
|
||||
return append(b.parent.GetPath(), b.name)
|
||||
func (b *BoltBucket) GetStringName() string {
|
||||
ret, err := b.nameDatatype.ToString(b.name)
|
||||
if err != nil {
|
||||
return "ERROR"
|
||||
}
|
||||
return []string{b.name}
|
||||
return ret
|
||||
}
|
||||
|
||||
/*
|
||||
buildVisiblePathSlice builds a slice of string slices containing all visible paths in this bucket
|
||||
GetPath returns the database path leading to this BoltBucket
|
||||
*/
|
||||
func (b *BoltBucket) GetPath() []PathNode {
|
||||
bktPath := b.GetPathNode()
|
||||
if b.parent != nil {
|
||||
return append(b.parent.GetPath(), bktPath)
|
||||
}
|
||||
return []PathNode{bktPath}
|
||||
}
|
||||
|
||||
/*
|
||||
buildVisiblePathSlice builds a slice of PathNode slices containing all visible paths in this bucket
|
||||
The passed prefix is the path leading to the current bucket
|
||||
*/
|
||||
func (b *BoltBucket) buildVisiblePathSlice(prefix []string, filter string) ([][]string, error) {
|
||||
var retSlice [][]string
|
||||
func (b *BoltBucket) buildVisiblePathSlice(prefix []PathNode) ([][]PathNode, error) {
|
||||
var retSlice [][]PathNode
|
||||
var retErr error
|
||||
retSlice = append(retSlice, append(prefix, b.name))
|
||||
bucketNode := b.GetPathNode()
|
||||
retSlice = append(retSlice, append(prefix, bucketNode))
|
||||
if b.expanded {
|
||||
// Add subbuckets
|
||||
for i := range b.buckets {
|
||||
bktS, bktErr := b.buckets[i].buildVisiblePathSlice(append(prefix, b.name), filter)
|
||||
bktS, bktErr := b.buckets[i].buildVisiblePathSlice(append(prefix, bucketNode))
|
||||
if bktErr != nil {
|
||||
return retSlice, bktErr
|
||||
}
|
||||
@ -325,10 +393,7 @@ func (b *BoltBucket) buildVisiblePathSlice(prefix []string, filter string) ([][]
|
||||
}
|
||||
// Add pairs
|
||||
for i := range b.pairs {
|
||||
if filter != "" && !strings.Contains(b.pairs[i].key, filter) {
|
||||
continue
|
||||
}
|
||||
retSlice = append(retSlice, append(append(prefix, b.name), b.pairs[i].key))
|
||||
retSlice = append(retSlice, append(append(prefix, bucketNode, b.pairs[i].GetPathNode())))
|
||||
}
|
||||
}
|
||||
return retSlice, retErr
|
||||
@ -339,7 +404,7 @@ func (b *BoltBucket) syncOpenBuckets(shadow *BoltBucket) {
|
||||
b.expanded = shadow.expanded
|
||||
for i := range b.buckets {
|
||||
for j := range shadow.buckets {
|
||||
if b.buckets[i].name == shadow.buckets[j].name {
|
||||
if bytes.Equal(b.buckets[i].name, shadow.buckets[j].name) {
|
||||
b.buckets[i].syncOpenBuckets(&shadow.buckets[j])
|
||||
}
|
||||
}
|
||||
@ -353,18 +418,18 @@ func (b *BoltBucket) openAllBuckets() {
|
||||
}
|
||||
}
|
||||
|
||||
func (b *BoltBucket) getBucket(k string) (*BoltBucket, error) {
|
||||
func (b *BoltBucket) getBucket(k []byte) (*BoltBucket, error) {
|
||||
for i := range b.buckets {
|
||||
if b.buckets[i].name == k {
|
||||
if bytes.Equal(b.buckets[i].name, k) {
|
||||
return &b.buckets[i], nil
|
||||
}
|
||||
}
|
||||
return nil, errors.New("Bucket Not Found")
|
||||
}
|
||||
|
||||
func (b *BoltBucket) getPair(k string) (*BoltPair, error) {
|
||||
func (b *BoltBucket) getPair(k []byte) (*BoltPair, error) {
|
||||
for i := range b.pairs {
|
||||
if b.pairs[i].key == k {
|
||||
if bytes.Equal(b.pairs[i].key, k) {
|
||||
return &b.pairs[i], nil
|
||||
}
|
||||
}
|
||||
@ -374,8 +439,14 @@ func (b *BoltBucket) getPair(k string) (*BoltPair, error) {
|
||||
/*
|
||||
GetPath Returns the path of the BoltPair
|
||||
*/
|
||||
func (p *BoltPair) GetPath() []string {
|
||||
return append(p.parent.GetPath(), p.key)
|
||||
func (p *BoltPair) GetPath() []PathNode {
|
||||
if p.parent == nil {
|
||||
return []PathNode{p.GetPathNode()}
|
||||
}
|
||||
return append(
|
||||
p.parent.GetPath(),
|
||||
p.GetPathNode(),
|
||||
)
|
||||
}
|
||||
|
||||
/* This is a go-between function (between the boltbrowser structs
|
||||
@ -385,9 +456,9 @@ func (p *BoltPair) GetPath() []string {
|
||||
* Mainly used for moving a bucket from one path to another
|
||||
* as in the 'renameBucket' function below.
|
||||
*/
|
||||
func addBucketFromBoltBucket(path []string, bb *BoltBucket) error {
|
||||
func addBucketFromBoltBucket(path []PathNode, bb *BoltBucket) error {
|
||||
if err := insertBucket(path, bb.name); err == nil {
|
||||
bucketPath := append(path, bb.name)
|
||||
bucketPath := append(path, bb.GetPathNode())
|
||||
for i := range bb.pairs {
|
||||
if err = insertPair(bucketPath, bb.pairs[i].key, bb.pairs[i].val); err != nil {
|
||||
return err
|
||||
@ -402,26 +473,22 @@ func addBucketFromBoltBucket(path []string, bb *BoltBucket) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func deleteKey(path []string) error {
|
||||
func deleteKey(path []PathNode) error {
|
||||
if AppArgs.ReadOnly {
|
||||
return errors.New("DB is in Read-Only Mode")
|
||||
}
|
||||
err := db.Update(func(tx *bbolt.Tx) error {
|
||||
err := db.Update(func(tx *bolt.Tx) error {
|
||||
// len(b.path)-1 is the key we need to delete,
|
||||
// the rest are buckets leading to that key
|
||||
if len(path) == 1 {
|
||||
// Deleting a root bucket
|
||||
return tx.DeleteBucket([]byte(path[0]))
|
||||
}
|
||||
b := tx.Bucket([]byte(path[0]))
|
||||
if b == nil {
|
||||
// Invalid path, try for the root bucket
|
||||
b = tx.Cursor().Bucket()
|
||||
return tx.DeleteBucket(path[0].name)
|
||||
}
|
||||
b := tx.Bucket(path[0].name)
|
||||
if b != nil {
|
||||
if len(path) > 1 {
|
||||
for i := range path[1 : len(path)-1] {
|
||||
b = b.Bucket([]byte(path[i+1]))
|
||||
b = b.Bucket(path[i+1].name)
|
||||
if b == nil {
|
||||
return errors.New("deleteKey: Invalid Path")
|
||||
}
|
||||
@ -429,11 +496,11 @@ func deleteKey(path []string) error {
|
||||
}
|
||||
// Now delete the last key in the path
|
||||
var err error
|
||||
if deleteBkt := b.Bucket([]byte(path[len(path)-1])); deleteBkt == nil {
|
||||
if deleteBkt := b.Bucket(path[len(path)-1].name); deleteBkt == nil {
|
||||
// Must be a pair
|
||||
err = b.Delete([]byte(path[len(path)-1]))
|
||||
err = b.Delete(path[len(path)-1].name)
|
||||
} else {
|
||||
err = b.DeleteBucket([]byte(path[len(path)-1]))
|
||||
err = b.DeleteBucket(path[len(path)-1].name)
|
||||
}
|
||||
return err
|
||||
}
|
||||
@ -442,78 +509,41 @@ func deleteKey(path []string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func readBucket(b *bbolt.Bucket) (*BoltBucket, error) {
|
||||
bb := new(BoltBucket)
|
||||
if b == nil {
|
||||
return nil, errors.New("No bucket passed")
|
||||
}
|
||||
// Recursively read a bucket and pairs from the DB
|
||||
func readBucket(parent *BoltBucket, b *bolt.Bucket, nm []byte) (*BoltBucket, error) {
|
||||
bb := NewBoltBucket(parent, nm)
|
||||
b.ForEach(func(k, v []byte) error {
|
||||
if v == nil {
|
||||
tb, err := readBucket(b.Bucket(k))
|
||||
tb.parent = bb
|
||||
tb, err := readBucket(bb, b.Bucket(k), k)
|
||||
if err == nil {
|
||||
tb.name = string(k)
|
||||
bb.buckets = append(bb.buckets, *tb)
|
||||
}
|
||||
} else {
|
||||
tp := BoltPair{key: string(k), val: string(v)}
|
||||
tp.parent = bb
|
||||
bb.pairs = append(bb.pairs, tp)
|
||||
bb.pairs = append(bb.pairs, *NewBoltPair(bb, k, v))
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return bb, nil
|
||||
}
|
||||
|
||||
func renameBucket(path []string, name string) error {
|
||||
if name == path[len(path)-1] {
|
||||
func renameBucket(path []PathNode, name []byte) error {
|
||||
if bytes.Equal(name, path[len(path)-1].name) {
|
||||
// No change requested
|
||||
return nil
|
||||
}
|
||||
var bb *BoltBucket // For caching the current bucket
|
||||
err := db.View(func(tx *bbolt.Tx) error {
|
||||
// len(b.path)-1 is the key we need to delete,
|
||||
// the rest are buckets leading to that key
|
||||
b := tx.Bucket([]byte(path[0]))
|
||||
if b == nil {
|
||||
// Invalid path, try for the root bucket
|
||||
b = tx.Cursor().Bucket()
|
||||
}
|
||||
if b != nil {
|
||||
if len(path) > 1 {
|
||||
for i := range path[1:] {
|
||||
b = b.Bucket([]byte(path[i+1]))
|
||||
if b == nil {
|
||||
return errors.New("renameBucket: Invalid Path")
|
||||
}
|
||||
}
|
||||
}
|
||||
var err error
|
||||
// Ok, cache b
|
||||
bb, err = readBucket(b)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return errors.New("renameBucket: Invalid Bucket")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if bb == nil {
|
||||
return errors.New("renameBucket: Couldn't find Bucket")
|
||||
}
|
||||
bb, err := memBolt.getBucketFromPath(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Ok, we have the bucket cached, now delete the current instance
|
||||
// Ok, we have the bucket, now delete the current instance
|
||||
if err = deleteKey(path); err != nil {
|
||||
return err
|
||||
}
|
||||
// Rechristen our cached bucket
|
||||
bb.name = name
|
||||
bb.SetName(name)
|
||||
// And re-add it
|
||||
|
||||
parentPath := path[:len(path)-1]
|
||||
if err = addBucketFromBoltBucket(parentPath, bb); err != nil {
|
||||
return err
|
||||
@ -521,33 +551,29 @@ func renameBucket(path []string, name string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func updatePairKey(path []string, k string) error {
|
||||
func updatePairKey(path []PathNode, k []byte) error {
|
||||
if AppArgs.ReadOnly {
|
||||
return errors.New("DB is in Read-Only Mode")
|
||||
}
|
||||
err := db.Update(func(tx *bbolt.Tx) error {
|
||||
err := db.Update(func(tx *bolt.Tx) error {
|
||||
// len(b.path)-1 is the key for the pair we're updating,
|
||||
// the rest are buckets leading to that key
|
||||
b := tx.Bucket([]byte(path[0]))
|
||||
if b == nil {
|
||||
// Invalid path, try for the root bucket
|
||||
b = tx.Cursor().Bucket()
|
||||
}
|
||||
b := tx.Bucket(path[0].name)
|
||||
if b != nil {
|
||||
if len(path) > 0 {
|
||||
for i := range path[1 : len(path)-1] {
|
||||
b = b.Bucket([]byte(path[i+1]))
|
||||
b = b.Bucket(path[i+1].name)
|
||||
if b == nil {
|
||||
return errors.New("updatePairValue: Invalid Path")
|
||||
}
|
||||
}
|
||||
}
|
||||
bk := []byte(path[len(path)-1])
|
||||
v := b.Get(bk)
|
||||
err := b.Delete(bk)
|
||||
bk := path[len(path)-1]
|
||||
v := b.Get(bk.name)
|
||||
err := b.Delete(bk.name)
|
||||
if err == nil {
|
||||
// Old pair has been deleted, now add the new one
|
||||
err = b.Put([]byte(k), v)
|
||||
err = b.Put(k, v)
|
||||
}
|
||||
// Now update the last key in the path
|
||||
return err
|
||||
@ -557,29 +583,25 @@ func updatePairKey(path []string, k string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func updatePairValue(path []string, v string) error {
|
||||
func updatePairValue(path []PathNode, v []byte) error {
|
||||
if AppArgs.ReadOnly {
|
||||
return errors.New("DB is in Read-Only Mode")
|
||||
}
|
||||
err := db.Update(func(tx *bbolt.Tx) error {
|
||||
err := db.Update(func(tx *bolt.Tx) error {
|
||||
// len(b.GetPath())-1 is the key for the pair we're updating,
|
||||
// the rest are buckets leading to that key
|
||||
b := tx.Bucket([]byte(path[0]))
|
||||
if b == nil {
|
||||
// Invalid path, try for the root bucket
|
||||
b = tx.Cursor().Bucket()
|
||||
}
|
||||
b := tx.Bucket(path[0].name)
|
||||
if b != nil {
|
||||
if len(path) > 0 {
|
||||
for i := range path[1 : len(path)-1] {
|
||||
b = b.Bucket([]byte(path[i+1]))
|
||||
b = b.Bucket(path[i+1].name)
|
||||
if b == nil {
|
||||
return errors.New("updatePairValue: Invalid Path")
|
||||
}
|
||||
}
|
||||
}
|
||||
// Now update the last key in the path
|
||||
err := b.Put([]byte(path[len(path)-1]), []byte(v))
|
||||
err := b.Put(path[len(path)-1].name, v)
|
||||
return err
|
||||
}
|
||||
return errors.New("updatePairValue: Invalid Path")
|
||||
@ -587,26 +609,26 @@ func updatePairValue(path []string, v string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func insertBucket(path []string, n string) error {
|
||||
func insertBucket(path []PathNode, n []byte) error {
|
||||
if AppArgs.ReadOnly {
|
||||
return errors.New("DB is in Read-Only Mode")
|
||||
}
|
||||
// Inserts a new bucket named 'n' at 'path'
|
||||
err := db.Update(func(tx *bbolt.Tx) error {
|
||||
if len(path) == 0 || path[0] == "" {
|
||||
err := db.Update(func(tx *bolt.Tx) error {
|
||||
if len(path) == 0 {
|
||||
// insert at root
|
||||
_, err := tx.CreateBucket([]byte(n))
|
||||
_, err := tx.CreateBucket(n)
|
||||
if err != nil {
|
||||
return fmt.Errorf("insertBucket: %s", err)
|
||||
}
|
||||
} else {
|
||||
rootBucket, path := path[0], path[1:]
|
||||
b := tx.Bucket([]byte(rootBucket))
|
||||
b := tx.Bucket(rootBucket.name)
|
||||
if b != nil {
|
||||
for len(path) > 0 {
|
||||
tstBucket := ""
|
||||
var tstBucket PathNode
|
||||
tstBucket, path = path[0], path[1:]
|
||||
nB := b.Bucket([]byte(tstBucket))
|
||||
nB := b.Bucket(tstBucket.name)
|
||||
if nB == nil {
|
||||
// Not a bucket, if we're out of path, just move on
|
||||
if len(path) != 0 {
|
||||
@ -617,7 +639,7 @@ func insertBucket(path []string, n string) error {
|
||||
b = nB
|
||||
}
|
||||
}
|
||||
_, err := b.CreateBucket([]byte(n))
|
||||
_, err := b.CreateBucket(n)
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("insertBucket: Invalid Path %s", rootBucket)
|
||||
@ -627,32 +649,28 @@ func insertBucket(path []string, n string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func insertPair(path []string, k string, v string) error {
|
||||
func insertPair(path []PathNode, k []byte, v []byte) error {
|
||||
if AppArgs.ReadOnly {
|
||||
return errors.New("DB is in Read-Only Mode")
|
||||
}
|
||||
// Insert a new pair k => v at path
|
||||
err := db.Update(func(tx *bbolt.Tx) error {
|
||||
err := db.Update(func(tx *bolt.Tx) error {
|
||||
if len(path) == 0 {
|
||||
// We cannot insert a pair at root
|
||||
return errors.New("insertPair: Cannot insert pair at root")
|
||||
}
|
||||
var err error
|
||||
b := tx.Bucket([]byte(path[0]))
|
||||
if b == nil {
|
||||
// Invalid path, try for the root bucket
|
||||
b = tx.Cursor().Bucket()
|
||||
}
|
||||
b := tx.Bucket(path[0].name)
|
||||
if b != nil {
|
||||
if len(path) > 0 {
|
||||
for i := 1; i < len(path); i++ {
|
||||
b = b.Bucket([]byte(path[i]))
|
||||
b = b.Bucket(path[i].name)
|
||||
if b == nil {
|
||||
return fmt.Errorf("insertPair: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
err := b.Put([]byte(k), []byte(v))
|
||||
err := b.Put(k, v)
|
||||
if err != nil {
|
||||
return fmt.Errorf("insertPair: %s", err)
|
||||
}
|
||||
@ -662,56 +680,80 @@ func insertPair(path []string, k string, v string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func exportValue(path []string, fName string) error {
|
||||
return db.View(func(tx *bbolt.Tx) error {
|
||||
func stringPathToBytePath(path []string) [][]byte {
|
||||
var ret [][]byte
|
||||
for _, v := range path {
|
||||
ret = append(ret, []byte(v))
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func bytePathToStringPath(path [][]byte) []string {
|
||||
var ret []string
|
||||
for _, v := range path {
|
||||
ret = append(ret, string(v))
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func nodePathToStringPath(path []PathNode) []string {
|
||||
var ret []string
|
||||
for _, v := range path {
|
||||
add, err := v.dataType.ToString(v.name)
|
||||
if err != nil {
|
||||
add = "ERROR"
|
||||
}
|
||||
ret = append(ret, add)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func exportValue(path []PathNode, fName string) error {
|
||||
return db.View(func(tx *bolt.Tx) error {
|
||||
// len(b.path)-1 is the key whose value we want to export
|
||||
// the rest are buckets leading to that key
|
||||
b := tx.Bucket([]byte(path[0]))
|
||||
if b == nil {
|
||||
// Invalid path, try for the root bucket
|
||||
b = tx.Cursor().Bucket()
|
||||
}
|
||||
b := tx.Bucket(path[0].name)
|
||||
if b != nil {
|
||||
if len(path) > 1 {
|
||||
for i := range path[1 : len(path)-1] {
|
||||
b = b.Bucket([]byte(path[i+1]))
|
||||
b = b.Bucket(path[i+1].name)
|
||||
if b == nil {
|
||||
return errors.New("exportValue: Invalid Path: " + strings.Join(path, "/"))
|
||||
return errors.New("exportValue: Invalid Path: " + strings.Join(nodePathToStringPath(path), "/"))
|
||||
}
|
||||
}
|
||||
}
|
||||
bk := []byte(path[len(path)-1])
|
||||
v := b.Get(bk)
|
||||
return writeToFile(fName, string(v), os.O_CREATE|os.O_WRONLY|os.O_TRUNC)
|
||||
bk := path[len(path)-1]
|
||||
v := b.Get(bk.name)
|
||||
return writeToFile(fName, string(v)+"\n", os.O_CREATE|os.O_WRONLY|os.O_TRUNC)
|
||||
}
|
||||
return errors.New("exportValue: Invalid Bucket")
|
||||
})
|
||||
}
|
||||
|
||||
func exportJSON(path []string, fName string) error {
|
||||
return db.View(func(tx *bbolt.Tx) error {
|
||||
func exportJSON(path []PathNode, fName string) error {
|
||||
return db.View(func(tx *bolt.Tx) error {
|
||||
// len(b.path)-1 is the key whose value we want to export
|
||||
// the rest are buckets leading to that key
|
||||
b := tx.Bucket([]byte(path[0]))
|
||||
if b == nil {
|
||||
// Invalid path, try for the root bucket
|
||||
b = tx.Cursor().Bucket()
|
||||
}
|
||||
b := tx.Bucket(path[0].name)
|
||||
if b != nil {
|
||||
if len(path) > 1 {
|
||||
for i := range path[1 : len(path)-1] {
|
||||
b = b.Bucket([]byte(path[i+1]))
|
||||
b = b.Bucket(path[i+1].name)
|
||||
if b == nil {
|
||||
return errors.New("exportValue: Invalid Path: " + strings.Join(path, "/"))
|
||||
return errors.New("exportValue: Invalid Path: " + strings.Join(nodePathToStringPath(path), "/"))
|
||||
}
|
||||
}
|
||||
}
|
||||
bk := []byte(path[len(path)-1])
|
||||
if v := b.Get(bk); v != nil {
|
||||
return writeToFile(fName, "{\""+string(bk)+"\":\""+string(v)+"\"}", os.O_CREATE|os.O_WRONLY|os.O_TRUNC)
|
||||
bk := path[len(path)-1]
|
||||
if v := b.Get(bk.name); v != nil {
|
||||
str, err := bk.dataType.ToString(bk.name)
|
||||
if err != nil {
|
||||
str = "ERROR"
|
||||
}
|
||||
return writeToFile(fName, "{\""+str+"\":\""+string(v)+"\"}", os.O_CREATE|os.O_WRONLY|os.O_TRUNC)
|
||||
}
|
||||
if b.Bucket(bk) != nil {
|
||||
return writeToFile(fName, genJSONString(b.Bucket(bk)), os.O_CREATE|os.O_WRONLY|os.O_TRUNC)
|
||||
if b.Bucket(bk.name) != nil {
|
||||
return writeToFile(fName, genJSONString(b.Bucket(bk.name)), os.O_CREATE|os.O_WRONLY|os.O_TRUNC)
|
||||
}
|
||||
return writeToFile(fName, genJSONString(b), os.O_CREATE|os.O_WRONLY|os.O_TRUNC)
|
||||
}
|
||||
@ -719,7 +761,7 @@ func exportJSON(path []string, fName string) error {
|
||||
})
|
||||
}
|
||||
|
||||
func genJSONString(b *bbolt.Bucket) string {
|
||||
func genJSONString(b *bolt.Bucket) string {
|
||||
ret := "{"
|
||||
b.ForEach(func(k, v []byte) error {
|
||||
ret = fmt.Sprintf("%s\"%s\":", ret, string(k))
|
||||
@ -756,36 +798,3 @@ func writeToFile(fn, s string, mode int) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func importValue(path []string, fName string) error {
|
||||
if AppArgs.ReadOnly {
|
||||
return errors.New("DB is in Read-Only Mode")
|
||||
}
|
||||
return db.Update(func(tx *bbolt.Tx) error {
|
||||
// len(b.GetPath())-1 is the key for the pair we're updating,
|
||||
// the rest are buckets leading to that key
|
||||
b := tx.Bucket([]byte(path[0]))
|
||||
if b == nil {
|
||||
// Invalid path, try for the root bucket
|
||||
b = tx.Cursor().Bucket()
|
||||
}
|
||||
if b != nil {
|
||||
if len(path) > 0 {
|
||||
for i := range path[1 : len(path)-1] {
|
||||
b = b.Bucket([]byte(path[i+1]))
|
||||
if b == nil {
|
||||
return errors.New("updatePairValue: Invalid Path")
|
||||
}
|
||||
}
|
||||
}
|
||||
// Now update the last key in the path
|
||||
bk := []byte(path[len(path)-1])
|
||||
v, err := os.ReadFile(fName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return b.Put(bk, v)
|
||||
}
|
||||
return errors.New("importValue: Invalid Bucket")
|
||||
})
|
||||
}
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 29 KiB |
Binary file not shown.
Before Width: | Height: | Size: 18 KiB |
9
datatype.go
Normal file
9
datatype.go
Normal file
@ -0,0 +1,9 @@
|
||||
package main
|
||||
|
||||
// A Datatype interface knows how to convert a byte slice into a
|
||||
// specific data type.
|
||||
type Datatype interface {
|
||||
Name() string // Type Name, for UI display
|
||||
ToString([]byte) (string, error) // ToString takes a []byte from the DB and returns the string to display
|
||||
FromString(string) ([]byte, error) // FromString takes a string and returns the []byte for the DB
|
||||
}
|
39
datatype_int.go
Normal file
39
datatype_int.go
Normal file
@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"encoding/binary"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Datatype_Int manages the int data type
|
||||
type Datatype_Int64 struct{}
|
||||
|
||||
func (t *Datatype_Int64) Name() string {
|
||||
return "int"
|
||||
}
|
||||
|
||||
func (t *Datatype_Int64) ToString(data []byte) (string, error) {
|
||||
return fmt.Sprintf("%d", t.btoi(data)), nil
|
||||
}
|
||||
|
||||
func (t *Datatype_Int64) FromString(val string) ([]byte, error) {
|
||||
v, err := strconv.ParseInt(val, 10, 64)
|
||||
if err != nil {
|
||||
return []byte{}, err
|
||||
}
|
||||
return t.itob(v), nil
|
||||
}
|
||||
|
||||
func (t *Datatype_Int64) itob(v int64) []byte {
|
||||
b := make([]byte, 8)
|
||||
binary.BigEndian.PutUint64(b, uint64(v))
|
||||
return b
|
||||
}
|
||||
|
||||
func (t *Datatype_Int64) btoi(b []byte) int64 {
|
||||
if len(b) < 8 {
|
||||
return 0
|
||||
}
|
||||
return int64(binary.BigEndian.Uint64(b))
|
||||
}
|
37
datatype_string.go
Normal file
37
datatype_string.go
Normal file
@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// Datatype_String is basically the default type for
|
||||
// boltbrowser (and the boltease library)
|
||||
type Datatype_String struct{}
|
||||
|
||||
func (t *Datatype_String) Name() string {
|
||||
return "string"
|
||||
}
|
||||
|
||||
func (t *Datatype_String) ToString(data []byte) (string, error) {
|
||||
if utf8.Valid(data) {
|
||||
ok := true
|
||||
for _, r := range string(data) {
|
||||
if r < 0x20 {
|
||||
ok = false
|
||||
break
|
||||
} else if r >= 0x7f && r <= 0x9f {
|
||||
ok = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if ok {
|
||||
return string(data), nil
|
||||
}
|
||||
}
|
||||
return "", errors.New("Invalid String Data")
|
||||
}
|
||||
|
||||
func (t *Datatype_String) FromString(val string) ([]byte, error) {
|
||||
return []byte(val), nil
|
||||
}
|
37
datatype_uint.go
Normal file
37
datatype_uint.go
Normal file
@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Datatype_Uint64 manages the uint64 data type
|
||||
// All conversions just use the binary package
|
||||
type Datatype_Uint64 struct{}
|
||||
|
||||
func (t *Datatype_Uint64) Name() string {
|
||||
return "uint64"
|
||||
}
|
||||
|
||||
func (t *Datatype_Uint64) ToString(data []byte) (string, error) {
|
||||
ret, bt := binary.Uvarint(data)
|
||||
if bt == 0 {
|
||||
return "", errors.New("Byte Buffer too small")
|
||||
} else if bt < 0 {
|
||||
return "", errors.New("Value larger than 64 bits")
|
||||
}
|
||||
return strconv.FormatUint(ret, 10), nil
|
||||
}
|
||||
|
||||
func (t *Datatype_Uint64) FromString(val string) ([]byte, error) {
|
||||
var err error
|
||||
var u uint64
|
||||
buf := make([]byte, binary.MaxVarintLen64)
|
||||
u, err = strconv.ParseUint(val, 10, 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
binary.PutUvarint(buf, u)
|
||||
return buf, nil
|
||||
}
|
13
go.mod
13
go.mod
@ -1,14 +1,11 @@
|
||||
module github.com/br0xen/boltbrowser
|
||||
|
||||
require (
|
||||
github.com/boltdb/bolt v1.3.1
|
||||
github.com/br0xen/termbox-util v0.0.0-20170904143325-de1d4c83380e
|
||||
github.com/nsf/termbox-go v1.1.1
|
||||
go.etcd.io/bbolt v1.3.7
|
||||
github.com/mattn/go-runewidth v0.0.4 // indirect
|
||||
github.com/nsf/termbox-go v0.0.0-20180819125858-b66b20ab708e
|
||||
golang.org/x/sys v0.0.0-20191002091554-b397fe3ad8ed // indirect
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/mattn/go-runewidth v0.0.9 // indirect
|
||||
golang.org/x/sys v0.8.0 // indirect
|
||||
)
|
||||
|
||||
go 1.20
|
||||
go 1.13
|
||||
|
20
go.sum
20
go.sum
@ -1,14 +1,10 @@
|
||||
github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4=
|
||||
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
|
||||
github.com/br0xen/termbox-util v0.0.0-20170904143325-de1d4c83380e h1:PF4gYXcZfTbAoAk5DPZcvjmq8gyg4gpcmWdT8W+0X1c=
|
||||
github.com/br0xen/termbox-util v0.0.0-20170904143325-de1d4c83380e/go.mod h1:x9wJlgOj74OFTOBwXOuO8pBguW37EgYNx51Dbjkfzo4=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
|
||||
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
||||
github.com/nsf/termbox-go v1.1.1 h1:nksUPLCb73Q++DwbYUBEglYBRPZyoXJdrj5L+TkjyZY=
|
||||
github.com/nsf/termbox-go v1.1.1/go.mod h1:T0cTdVuOwf7pHQNtfhnEbzHbcNyCEcVU4YPpouCbVxo=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||
go.etcd.io/bbolt v1.3.7 h1:j+zJOnnEjF/kyHlDDgGnVL/AIqIJPq8UoB2GSNfkUfQ=
|
||||
go.etcd.io/bbolt v1.3.7/go.mod h1:N9Mkw9X8x5fupy0IKsmuqVtoGDyxsaDlbk4Rd05IAQw=
|
||||
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
|
||||
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
|
||||
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
|
||||
github.com/nsf/termbox-go v0.0.0-20180819125858-b66b20ab708e h1:fvw0uluMptljaRKSU8459cJ4bmi3qUYyMs5kzpic2fY=
|
||||
github.com/nsf/termbox-go v0.0.0-20180819125858-b66b20ab708e/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ=
|
||||
golang.org/x/sys v0.0.0-20191002091554-b397fe3ad8ed h1:5TJcLJn2a55mJjzYk0yOoqN8X1OdvBDUnaZaKKyQtkY=
|
||||
golang.org/x/sys v0.0.0-20191002091554-b397fe3ad8ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
|
32
main.go
32
main.go
@ -7,17 +7,20 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/boltdb/bolt"
|
||||
"github.com/nsf/termbox-go"
|
||||
"go.etcd.io/bbolt"
|
||||
)
|
||||
|
||||
var ProgramName = "boltbrowser"
|
||||
var VersionNum = 2.0
|
||||
|
||||
var databaseFiles []string
|
||||
var db *bbolt.DB
|
||||
var db *bolt.DB
|
||||
var memBolt *BoltDB
|
||||
|
||||
var datatypes map[string]Datatype
|
||||
var dataTypeNameSlice []string
|
||||
|
||||
var currentFilename string
|
||||
|
||||
const DefaultDBOpenTimeout = time.Second
|
||||
@ -25,7 +28,6 @@ const DefaultDBOpenTimeout = time.Second
|
||||
var AppArgs struct {
|
||||
DBOpenTimeout time.Duration
|
||||
ReadOnly bool
|
||||
NoValue bool
|
||||
}
|
||||
|
||||
func init() {
|
||||
@ -64,10 +66,6 @@ func parseArgs() {
|
||||
if val == "true" {
|
||||
AppArgs.ReadOnly = true
|
||||
}
|
||||
case "-no-value":
|
||||
if val == "true" {
|
||||
AppArgs.NoValue = true
|
||||
}
|
||||
case "-help":
|
||||
printUsage(nil)
|
||||
default:
|
||||
@ -78,8 +76,6 @@ func parseArgs() {
|
||||
switch parms[i] {
|
||||
case "-readonly", "-ro":
|
||||
AppArgs.ReadOnly = true
|
||||
case "-no-value":
|
||||
AppArgs.NoValue = true
|
||||
case "-help":
|
||||
printUsage(nil)
|
||||
default:
|
||||
@ -96,13 +92,13 @@ func printUsage(err error) {
|
||||
fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS] <filename(s)>\nOptions:\n", ProgramName)
|
||||
fmt.Fprintf(os.Stderr, " -timeout=duration\n DB file open timeout (default 1s)\n")
|
||||
fmt.Fprintf(os.Stderr, " -ro, -readonly \n Open the DB in read-only mode\n")
|
||||
fmt.Fprintf(os.Stderr, " -no-value \n Do not display a value in left pane\n")
|
||||
}
|
||||
|
||||
func main() {
|
||||
var err error
|
||||
|
||||
parseArgs()
|
||||
initDataTypes()
|
||||
|
||||
err = termbox.Init()
|
||||
if err != nil {
|
||||
@ -114,8 +110,8 @@ func main() {
|
||||
|
||||
for _, databaseFile := range databaseFiles {
|
||||
currentFilename = databaseFile
|
||||
db, err = bbolt.Open(databaseFile, 0600, &bbolt.Options{Timeout: AppArgs.DBOpenTimeout})
|
||||
if err == bbolt.ErrTimeout {
|
||||
db, err = bolt.Open(databaseFile, 0600, &bolt.Options{Timeout: AppArgs.DBOpenTimeout})
|
||||
if err == bolt.ErrTimeout {
|
||||
termbox.Close()
|
||||
fmt.Printf("File %s is locked. Make sure it's not used by another app and try again\n", databaseFile)
|
||||
os.Exit(1)
|
||||
@ -142,3 +138,15 @@ func main() {
|
||||
defer db.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func initDataTypes() {
|
||||
datatypes = make(map[string]Datatype)
|
||||
for _, t := range []Datatype{
|
||||
&Datatype_String{},
|
||||
&Datatype_Int64{},
|
||||
&Datatype_Uint64{},
|
||||
} {
|
||||
dataTypeNameSlice = append(dataTypeNameSlice, t.Name())
|
||||
datatypes[t.Name()] = t
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,3 @@
|
||||
//go:build !windows
|
||||
// +build !windows
|
||||
|
||||
package main
|
||||
|
@ -1,5 +1,4 @@
|
||||
//go:build windows
|
||||
|
||||
// +build windows
|
||||
package main
|
||||
|
||||
// Windows doesn't support process backgrounding like *nix.
|
||||
|
@ -3,7 +3,7 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
termboxUtil "github.com/br0xen/termbox-util"
|
||||
"github.com/br0xen/termbox-util"
|
||||
"github.com/nsf/termbox-go"
|
||||
)
|
||||
|
||||
@ -111,9 +111,9 @@ func (screen *AboutScreen) drawScreen(style Style) {
|
||||
{"e", "edit value of pair"},
|
||||
{"r", "rename pair/bucket"},
|
||||
{"", ""},
|
||||
{"", ""},
|
||||
{"D", "delete item"},
|
||||
{"x,X", "export as string/json to file"},
|
||||
{"i", "import file to value of pair"},
|
||||
{"", ""},
|
||||
{"?", "this screen"},
|
||||
{"q", "quit program"},
|
||||
|
@ -1,9 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@ -29,10 +29,9 @@ type BrowserScreen struct {
|
||||
leftViewPort ViewPort
|
||||
rightViewPort ViewPort
|
||||
queuedCommand string
|
||||
currentPath []string
|
||||
currentPath []PathNode
|
||||
currentType int
|
||||
message string
|
||||
filter string
|
||||
mode BrowserMode
|
||||
inputModal *termboxUtil.InputModal
|
||||
confirmModal *termboxUtil.ConfirmModal
|
||||
@ -41,6 +40,7 @@ type BrowserScreen struct {
|
||||
|
||||
leftPaneBuffer []Line
|
||||
rightPaneBuffer []Line
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
@ -53,7 +53,6 @@ const (
|
||||
modeChange = 32 // 0000 0010 0000
|
||||
modeChangeKey = 33 // 0000 0010 0001
|
||||
modeChangeVal = 34 // 0000 0010 0010
|
||||
modeFilter = 35 // 0100 0010 0011
|
||||
modeInsert = 64 // 0000 0100 0000
|
||||
modeInsertBucket = 65 // 0000 0100 0001
|
||||
modeInsertPair = 68 // 0000 0100 0100
|
||||
@ -61,10 +60,9 @@ const (
|
||||
modeInsertPairVal = 70 // 0000 0100 0110
|
||||
modeDelete = 256 // 0001 0000 0000
|
||||
modeModToParent = 8 // 0000 0000 1000
|
||||
modeIO = 512 // 0010 0000 0000
|
||||
modeIOExportValue = 513 // 0010 0000 0001
|
||||
modeIOExportJSON = 514 // 0010 0000 0010
|
||||
modeIOImportValue = 516 // 0010 0000 0100
|
||||
modeExport = 512 // 0010 0000 0000
|
||||
modeExportValue = 513 // 0010 0000 0001
|
||||
modeExportJSON = 514 // 0010 0000 0010
|
||||
)
|
||||
|
||||
/*
|
||||
@ -89,8 +87,8 @@ func (screen *BrowserScreen) handleKeyEvent(event termbox.Event) int {
|
||||
return screen.handleInsertKeyEvent(event)
|
||||
} else if screen.mode == modeDelete {
|
||||
return screen.handleDeleteKeyEvent(event)
|
||||
} else if screen.mode&modeIO == modeIO {
|
||||
return screen.handleIOKeyEvent(event)
|
||||
} else if screen.mode&modeExport == modeExport {
|
||||
return screen.handleExportKeyEvent(event)
|
||||
}
|
||||
return BrowserScreenIndex
|
||||
}
|
||||
@ -106,11 +104,11 @@ func (screen *BrowserScreen) handleBrowseKeyEvent(event termbox.Event) int {
|
||||
|
||||
} else if event.Ch == 'g' {
|
||||
// Jump to Beginning
|
||||
screen.currentPath = screen.db.getNextVisiblePath(nil, screen.filter)
|
||||
screen.currentPath = screen.db.getNextVisiblePath(nil)
|
||||
|
||||
} else if event.Ch == 'G' {
|
||||
// Jump to End
|
||||
screen.currentPath = screen.db.getPrevVisiblePath(nil, screen.filter)
|
||||
screen.currentPath = screen.db.getPrevVisiblePath(nil)
|
||||
|
||||
} else if event.Key == termbox.KeyCtrlR {
|
||||
screen.refreshDatabase()
|
||||
@ -153,20 +151,18 @@ func (screen *BrowserScreen) handleBrowseKeyEvent(event termbox.Event) int {
|
||||
screen.startInsertItemAtParent(typeBucket)
|
||||
|
||||
} else if event.Ch == 'e' {
|
||||
b, p, _ := screen.db.getGenericFromPath(screen.currentPath)
|
||||
b, p, _ := screen.db.getGenericFromStringPath(screen.currentPath)
|
||||
if b != nil {
|
||||
screen.setMessage("Cannot edit a bucket, did you mean to (r)ename?")
|
||||
} else if p != nil {
|
||||
screen.startEditItem()
|
||||
}
|
||||
} else if event.Ch == '/' {
|
||||
screen.startFilter()
|
||||
|
||||
} else if event.Ch == 'r' {
|
||||
screen.startRenameItem()
|
||||
|
||||
} else if event.Key == termbox.KeyEnter {
|
||||
b, p, _ := screen.db.getGenericFromPath(screen.currentPath)
|
||||
b, p, _ := screen.db.getGenericFromStringPath(screen.currentPath)
|
||||
if b != nil {
|
||||
screen.db.toggleOpenBucket(screen.currentPath)
|
||||
} else if p != nil {
|
||||
@ -174,7 +170,7 @@ func (screen *BrowserScreen) handleBrowseKeyEvent(event termbox.Event) int {
|
||||
}
|
||||
|
||||
} else if event.Ch == 'l' || event.Key == termbox.KeyArrowRight {
|
||||
b, p, _ := screen.db.getGenericFromPath(screen.currentPath)
|
||||
b, p, _ := screen.db.getGenericFromStringPath(screen.currentPath)
|
||||
// Select the current item
|
||||
if b != nil {
|
||||
screen.db.toggleOpenBucket(screen.currentPath)
|
||||
@ -186,7 +182,7 @@ func (screen *BrowserScreen) handleBrowseKeyEvent(event termbox.Event) int {
|
||||
|
||||
} else if event.Ch == 'h' || event.Key == termbox.KeyArrowLeft {
|
||||
// If we are _on_ a bucket that's open, close it
|
||||
b, _, e := screen.db.getGenericFromPath(screen.currentPath)
|
||||
b, _, e := screen.db.getGenericFromStringPath(screen.currentPath)
|
||||
if e == nil && b != nil && b.expanded {
|
||||
screen.db.closeBucket(screen.currentPath)
|
||||
} else {
|
||||
@ -205,14 +201,32 @@ func (screen *BrowserScreen) handleBrowseKeyEvent(event termbox.Event) int {
|
||||
} else if event.Ch == 'D' {
|
||||
screen.startDeleteItem()
|
||||
} else if event.Ch == 'x' {
|
||||
// Export Value to a file
|
||||
// Export Value
|
||||
screen.startExportValue()
|
||||
} else if event.Ch == 'X' {
|
||||
// Export Key/Value (or Bucket) as JSON
|
||||
screen.startExportJSON()
|
||||
} else if event.Ch == 'i' {
|
||||
// Import value from a file
|
||||
screen.startImportValue()
|
||||
|
||||
} else if event.Ch == 't' {
|
||||
// TODO: change the type of the pair value being viewed
|
||||
p, err := screen.db.getPairFromPath(screen.currentPath)
|
||||
if err != nil {
|
||||
screen.setMessage("Cannot change pair type (did you mean 'T': Change pair key/bucket name type?)")
|
||||
return BrowserScreenIndex
|
||||
}
|
||||
p.valDatatype = findNextDatatype(p.valDatatype.Name())
|
||||
|
||||
} else if event.Ch == 'T' {
|
||||
b, p, err := screen.db.getGenericFromStringPath(screen.currentPath)
|
||||
if err != nil {
|
||||
screen.setMessage("Error reading path... Not sure what to do.")
|
||||
return BrowserScreenIndex
|
||||
}
|
||||
if b != nil {
|
||||
b.nameDatatype = findNextDatatype(b.nameDatatype.Name())
|
||||
} else if p != nil {
|
||||
p.keyDatatype = findNextDatatype(p.keyDatatype.Name())
|
||||
}
|
||||
}
|
||||
return BrowserScreenIndex
|
||||
}
|
||||
@ -224,44 +238,53 @@ func (screen *BrowserScreen) handleInputKeyEvent(event termbox.Event) int {
|
||||
} else {
|
||||
screen.inputModal.HandleEvent(event)
|
||||
if screen.inputModal.IsDone() {
|
||||
b, p, _ := screen.db.getGenericFromPath(screen.currentPath)
|
||||
if screen.mode == modeFilter {
|
||||
screen.filter = screen.inputModal.GetValue()
|
||||
if !screen.db.isVisiblePath(screen.currentPath, screen.filter) {
|
||||
screen.currentPath = screen.currentPath[:len(screen.currentPath)-1]
|
||||
}
|
||||
}
|
||||
b, p, _ := screen.db.getGenericFromStringPath(screen.currentPath)
|
||||
if b != nil {
|
||||
if screen.mode == modeChangeKey {
|
||||
newName := screen.inputModal.GetValue()
|
||||
if renameBucket(screen.currentPath, newName) != nil {
|
||||
screen.setMessage("Error renaming bucket.")
|
||||
} else {
|
||||
b.name = newName
|
||||
screen.currentPath[len(screen.currentPath)-1] = b.name
|
||||
screen.setMessage("Bucket Renamed!")
|
||||
screen.refreshDatabase()
|
||||
btName, err := b.nameDatatype.FromString(newName)
|
||||
if err != nil {
|
||||
screen.setMessage("Error renaming bucket: " + err.Error())
|
||||
} else {
|
||||
if renameBucket(screen.currentPath, btName) != nil {
|
||||
screen.setMessage("Error renaming bucket.")
|
||||
} else {
|
||||
b.name = btName
|
||||
screen.currentPath[len(screen.currentPath)-1].name = b.name
|
||||
screen.setMessage("Bucket Renamed!")
|
||||
screen.refreshDatabase()
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if p != nil {
|
||||
if screen.mode == modeChangeKey {
|
||||
newKey := screen.inputModal.GetValue()
|
||||
if err := updatePairKey(screen.currentPath, newKey); err != nil {
|
||||
screen.setMessage("Error occurred updating Pair.")
|
||||
} else {
|
||||
p.key = newKey
|
||||
screen.currentPath[len(screen.currentPath)-1] = p.key
|
||||
screen.setMessage("Pair updated!")
|
||||
screen.refreshDatabase()
|
||||
btKey, err := p.keyDatatype.FromString(newKey)
|
||||
if err != nil {
|
||||
screen.setMessage("Error updating pair: " + err.Error())
|
||||
} else {
|
||||
if updatePairKey(screen.currentPath, btKey) != nil {
|
||||
screen.setMessage("Error occurred updating Pair.")
|
||||
} else {
|
||||
p.key = btKey
|
||||
screen.currentPath[len(screen.currentPath)-1].name = p.key
|
||||
screen.setMessage("Pair updated!")
|
||||
screen.refreshDatabase()
|
||||
}
|
||||
}
|
||||
} else if screen.mode == modeChangeVal {
|
||||
newVal := screen.inputModal.GetValue()
|
||||
if err := updatePairValue(screen.currentPath, newVal); err != nil {
|
||||
screen.setMessage("Error occurred updating Pair.")
|
||||
} else {
|
||||
p.val = newVal
|
||||
screen.setMessage("Pair updated!")
|
||||
screen.refreshDatabase()
|
||||
btVal, err := p.valDatatype.FromString(newVal)
|
||||
if err != nil {
|
||||
screen.setMessage("Error updating pair: " + err.Error())
|
||||
} else {
|
||||
if updatePairValue(screen.currentPath, btVal) != nil {
|
||||
screen.setMessage("Error occurred updating Pair.")
|
||||
} else {
|
||||
p.val = btVal
|
||||
screen.setMessage("Pair updated!")
|
||||
screen.refreshDatabase()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -276,15 +299,15 @@ func (screen *BrowserScreen) handleDeleteKeyEvent(event termbox.Event) int {
|
||||
screen.confirmModal.HandleEvent(event)
|
||||
if screen.confirmModal.IsDone() {
|
||||
if screen.confirmModal.IsAccepted() {
|
||||
holdNextPath := screen.db.getNextVisiblePath(screen.currentPath, screen.filter)
|
||||
holdPrevPath := screen.db.getPrevVisiblePath(screen.currentPath, screen.filter)
|
||||
holdNextPath := screen.db.getNextVisiblePath(screen.currentPath)
|
||||
holdPrevPath := screen.db.getPrevVisiblePath(screen.currentPath)
|
||||
if deleteKey(screen.currentPath) == nil {
|
||||
screen.refreshDatabase()
|
||||
// Move the current path endpoint appropriately
|
||||
//found_new_path := false
|
||||
if holdNextPath != nil {
|
||||
if len(holdNextPath) > 2 {
|
||||
if holdNextPath[len(holdNextPath)-2] == screen.currentPath[len(screen.currentPath)-2] {
|
||||
if bytes.Equal(holdNextPath[len(holdNextPath)-2].name, screen.currentPath[len(screen.currentPath)-2].name) {
|
||||
screen.currentPath = holdNextPath
|
||||
} else if holdPrevPath != nil {
|
||||
screen.currentPath = holdPrevPath
|
||||
@ -320,12 +343,21 @@ func (screen *BrowserScreen) handleInsertKeyEvent(event termbox.Event) int {
|
||||
screen.inputModal.HandleEvent(event)
|
||||
if screen.inputModal.IsDone() {
|
||||
newVal := screen.inputModal.GetValue()
|
||||
// TODO: Just default datatype to string? Or should we infer it somehow?
|
||||
useType := datatypes["string"]
|
||||
nodeName, err := useType.FromString(newVal)
|
||||
if err != nil {
|
||||
screen.setMessage("Error Inserting new item. Data did not match datatype.")
|
||||
return BrowserScreenIndex
|
||||
}
|
||||
newNode := PathNode{name: nodeName, dataType: useType}
|
||||
screen.inputModal.Clear()
|
||||
var insertPath []string
|
||||
var insertPath []PathNode
|
||||
if len(screen.currentPath) > 0 {
|
||||
_, p, e := screen.db.getGenericFromPath(screen.currentPath)
|
||||
_, p, e := screen.db.getGenericFromStringPath(screen.currentPath)
|
||||
if e != nil {
|
||||
screen.setMessage("Error Inserting new item. Invalid Path.")
|
||||
return BrowserScreenIndex
|
||||
}
|
||||
insertPath = screen.currentPath
|
||||
// where are we inserting?
|
||||
@ -337,14 +369,14 @@ func (screen *BrowserScreen) handleInsertKeyEvent(event termbox.Event) int {
|
||||
if len(screen.currentPath) > 1 {
|
||||
insertPath = screen.currentPath[:len(screen.currentPath)-1]
|
||||
} else {
|
||||
insertPath = make([]string, 0)
|
||||
insertPath = make([]PathNode, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
parentB, _, _ := screen.db.getGenericFromPath(insertPath)
|
||||
parentB, _, _ := screen.db.getGenericFromStringPath(insertPath)
|
||||
if screen.mode&modeInsertBucket == modeInsertBucket {
|
||||
err := insertBucket(insertPath, newVal)
|
||||
err := insertBucket(insertPath, newNode.name)
|
||||
if err != nil {
|
||||
screen.setMessage(fmt.Sprintf("%s => %s", err, insertPath))
|
||||
} else {
|
||||
@ -352,13 +384,13 @@ func (screen *BrowserScreen) handleInsertKeyEvent(event termbox.Event) int {
|
||||
parentB.expanded = true
|
||||
}
|
||||
}
|
||||
screen.currentPath = append(insertPath, newVal)
|
||||
screen.currentPath = append(insertPath, newNode)
|
||||
|
||||
screen.refreshDatabase()
|
||||
screen.mode = modeBrowse
|
||||
screen.inputModal.Clear()
|
||||
} else if screen.mode&modeInsertPair == modeInsertPair {
|
||||
err := insertPair(insertPath, newVal, "")
|
||||
err := insertPair(insertPath, []byte(newVal), []byte{})
|
||||
if err != nil {
|
||||
screen.setMessage(fmt.Sprintf("%s => %s", err, insertPath))
|
||||
screen.refreshDatabase()
|
||||
@ -368,7 +400,7 @@ func (screen *BrowserScreen) handleInsertKeyEvent(event termbox.Event) int {
|
||||
if parentB != nil {
|
||||
parentB.expanded = true
|
||||
}
|
||||
screen.currentPath = append(insertPath, newVal)
|
||||
screen.currentPath = append(insertPath, newNode)
|
||||
screen.refreshDatabase()
|
||||
screen.startEditItem()
|
||||
}
|
||||
@ -378,42 +410,33 @@ func (screen *BrowserScreen) handleInsertKeyEvent(event termbox.Event) int {
|
||||
return BrowserScreenIndex
|
||||
}
|
||||
|
||||
func (screen *BrowserScreen) handleIOKeyEvent(event termbox.Event) int {
|
||||
func (screen *BrowserScreen) handleExportKeyEvent(event termbox.Event) int {
|
||||
if event.Key == termbox.KeyEsc {
|
||||
screen.mode = modeBrowse
|
||||
screen.inputModal.Clear()
|
||||
} else {
|
||||
screen.inputModal.HandleEvent(event)
|
||||
if screen.inputModal.IsDone() {
|
||||
b, p, _ := screen.db.getGenericFromPath(screen.currentPath)
|
||||
b, p, _ := screen.db.getGenericFromStringPath(screen.currentPath)
|
||||
fileName := screen.inputModal.GetValue()
|
||||
if screen.mode&modeIOExportValue == modeIOExportValue {
|
||||
if screen.mode&modeExportValue == modeExportValue {
|
||||
// Exporting the value
|
||||
if p != nil {
|
||||
if err := exportValue(screen.currentPath, fileName); err != nil {
|
||||
//screen.setMessage("Error exporting to file " + fileName + ".")
|
||||
//screen.setMessage("Error Exporting to file " + fileName + ".")
|
||||
screen.setMessage(err.Error())
|
||||
} else {
|
||||
screen.setMessage("Value exported to file: " + fileName)
|
||||
}
|
||||
}
|
||||
} else if screen.mode&modeIOExportJSON == modeIOExportJSON {
|
||||
} else if screen.mode&modeExportJSON == modeExportJSON {
|
||||
if b != nil || p != nil {
|
||||
if exportJSON(screen.currentPath, fileName) != nil {
|
||||
screen.setMessage("Error exporting to file " + fileName + ".")
|
||||
screen.setMessage("Error Exporting to file " + fileName + ".")
|
||||
} else {
|
||||
screen.setMessage("Value exported to file: " + fileName)
|
||||
}
|
||||
}
|
||||
} else if screen.mode&modeIOImportValue == modeIOImportValue {
|
||||
if p != nil {
|
||||
if err := importValue(screen.currentPath, fileName); err != nil {
|
||||
screen.setMessage(err.Error())
|
||||
} else {
|
||||
screen.setMessage("Value imported from file: " + fileName)
|
||||
screen.refreshDatabase()
|
||||
}
|
||||
}
|
||||
}
|
||||
screen.mode = modeBrowse
|
||||
screen.inputModal.Clear()
|
||||
@ -424,13 +447,13 @@ func (screen *BrowserScreen) handleIOKeyEvent(event termbox.Event) int {
|
||||
|
||||
func (screen *BrowserScreen) jumpCursorUp(distance int) bool {
|
||||
// Jump up 'distance' lines
|
||||
visPaths, err := screen.db.buildVisiblePathSlice(screen.filter)
|
||||
visPaths, err := screen.db.buildVisiblePathSlice()
|
||||
if err == nil {
|
||||
findPath := screen.currentPath
|
||||
for idx, pth := range visPaths {
|
||||
startJump := true
|
||||
for i := range pth {
|
||||
if len(screen.currentPath) > i && pth[i] != screen.currentPath[i] {
|
||||
if len(screen.currentPath) > i && !bytes.Equal(pth[i].name, screen.currentPath[i].name) {
|
||||
startJump = false
|
||||
}
|
||||
}
|
||||
@ -444,26 +467,26 @@ func (screen *BrowserScreen) jumpCursorUp(distance int) bool {
|
||||
}
|
||||
isCurPath := true
|
||||
for i := range screen.currentPath {
|
||||
if screen.currentPath[i] != findPath[i] {
|
||||
if !bytes.Equal(screen.currentPath[i].name, findPath[i].name) {
|
||||
isCurPath = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if isCurPath {
|
||||
screen.currentPath = screen.db.getNextVisiblePath(nil, screen.filter)
|
||||
screen.currentPath = screen.db.getNextVisiblePath(nil)
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
func (screen *BrowserScreen) jumpCursorDown(distance int) bool {
|
||||
visPaths, err := screen.db.buildVisiblePathSlice(screen.filter)
|
||||
visPaths, err := screen.db.buildVisiblePathSlice()
|
||||
if err == nil {
|
||||
findPath := screen.currentPath
|
||||
for idx, pth := range visPaths {
|
||||
startJump := true
|
||||
|
||||
for i := range pth {
|
||||
if len(screen.currentPath) > i && pth[i] != screen.currentPath[i] {
|
||||
if len(screen.currentPath) > i && !bytes.Equal(pth[i].name, screen.currentPath[i].name) {
|
||||
startJump = false
|
||||
}
|
||||
}
|
||||
@ -477,20 +500,20 @@ func (screen *BrowserScreen) jumpCursorDown(distance int) bool {
|
||||
}
|
||||
isCurPath := true
|
||||
for i := range screen.currentPath {
|
||||
if screen.currentPath[i] != findPath[i] {
|
||||
if !bytes.Equal(screen.currentPath[i].name, findPath[i].name) {
|
||||
isCurPath = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if isCurPath {
|
||||
screen.currentPath = screen.db.getNextVisiblePath(nil, screen.filter)
|
||||
screen.currentPath = screen.db.getNextVisiblePath(nil)
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (screen *BrowserScreen) moveCursorUp() bool {
|
||||
newPath := screen.db.getPrevVisiblePath(screen.currentPath, screen.filter)
|
||||
newPath := screen.db.getPrevVisiblePath(screen.currentPath)
|
||||
if newPath != nil {
|
||||
screen.currentPath = newPath
|
||||
return true
|
||||
@ -498,7 +521,7 @@ func (screen *BrowserScreen) moveCursorUp() bool {
|
||||
return false
|
||||
}
|
||||
func (screen *BrowserScreen) moveCursorDown() bool {
|
||||
newPath := screen.db.getNextVisiblePath(screen.currentPath, screen.filter)
|
||||
newPath := screen.db.getNextVisiblePath(screen.currentPath)
|
||||
if newPath != nil {
|
||||
screen.currentPath = newPath
|
||||
return true
|
||||
@ -548,22 +571,10 @@ func (screen *BrowserScreen) drawScreen(style Style) {
|
||||
|
||||
func (screen *BrowserScreen) drawHeader(style Style) {
|
||||
width, _ := termbox.Size()
|
||||
headerStringLen := func(fileName string) int {
|
||||
return len(ProgramName) + len(fileName) + 1
|
||||
}
|
||||
headerFileName := currentFilename
|
||||
if headerStringLen(headerFileName) > width {
|
||||
headerFileName = filepath.Base(headerFileName)
|
||||
}
|
||||
headerString := ProgramName + ": " + headerFileName
|
||||
count := ((width - len(headerString)) / 2) + 1
|
||||
if count < 0 {
|
||||
count = 0
|
||||
}
|
||||
spaces := strings.Repeat(" ", count)
|
||||
headerString := ProgramName + ": " + currentFilename
|
||||
spaces := strings.Repeat(" ", ((width-len(headerString))/2)+1)
|
||||
termboxUtil.DrawStringAtPoint(fmt.Sprintf("%s%s%s", spaces, headerString, spaces), 0, 0, style.titleFg, style.titleBg)
|
||||
}
|
||||
|
||||
func (screen *BrowserScreen) drawFooter(style Style) {
|
||||
if screen.messageTimeout > 0 && time.Since(screen.messageTime) > screen.messageTimeout {
|
||||
screen.clearMessage()
|
||||
@ -575,7 +586,7 @@ func (screen *BrowserScreen) drawFooter(style Style) {
|
||||
func (screen *BrowserScreen) buildLeftPane(style Style) {
|
||||
screen.leftPaneBuffer = nil
|
||||
if len(screen.currentPath) == 0 {
|
||||
screen.currentPath = screen.db.getNextVisiblePath(nil, screen.filter)
|
||||
screen.currentPath = screen.db.getNextVisiblePath(nil)
|
||||
}
|
||||
for i := range screen.db.buckets {
|
||||
screen.leftPaneBuffer = append(screen.leftPaneBuffer, screen.bucketToLines(&screen.db.buckets[i], style)...)
|
||||
@ -615,25 +626,29 @@ func (screen *BrowserScreen) drawLeftPane(style Style) {
|
||||
|
||||
func (screen *BrowserScreen) buildRightPane(style Style) {
|
||||
screen.rightPaneBuffer = nil
|
||||
b, p, err := screen.db.getGenericFromPath(screen.currentPath)
|
||||
|
||||
b, p, err := screen.db.getGenericFromStringPath(screen.currentPath)
|
||||
if err == nil {
|
||||
if b != nil {
|
||||
screen.rightPaneBuffer = append(screen.rightPaneBuffer,
|
||||
Line{fmt.Sprintf("Path: %s", strings.Join(stringifyPath(b.GetPath()), " → ")), style.defaultFg, style.defaultBg})
|
||||
Line{fmt.Sprintf("Path: %s", strings.Join(nodePathToStringPath(b.GetPath()), " → ")), style.defaultFg, style.defaultBg})
|
||||
screen.rightPaneBuffer = append(screen.rightPaneBuffer,
|
||||
Line{fmt.Sprintf("Buckets: %d", len(b.buckets)), style.defaultFg, style.defaultBg})
|
||||
screen.rightPaneBuffer = append(screen.rightPaneBuffer,
|
||||
Line{fmt.Sprintf("Pairs: %d", len(b.pairs)), style.defaultFg, style.defaultBg})
|
||||
screen.rightPaneBuffer = append(screen.rightPaneBuffer,
|
||||
Line{fmt.Sprintf("Name Datatype: %s", b.nameDatatype.Name()), style.defaultFg, style.defaultBg})
|
||||
|
||||
} else if p != nil {
|
||||
screen.rightPaneBuffer = append(screen.rightPaneBuffer,
|
||||
Line{fmt.Sprintf("Path: %s", strings.Join(stringifyPath(p.GetPath()), " → ")), style.defaultFg, style.defaultBg})
|
||||
Line{fmt.Sprintf("Path: %s", strings.Join(nodePathToStringPath(p.GetPath()), " → ")), style.defaultFg, style.defaultBg})
|
||||
screen.rightPaneBuffer = append(screen.rightPaneBuffer,
|
||||
Line{fmt.Sprintf("Key: %s", stringify([]byte(p.key))), style.defaultFg, style.defaultBg})
|
||||
Line{fmt.Sprintf("Key (%s): %s", p.keyDatatype.Name(), p.GetStringKey()), style.defaultFg, style.defaultBg})
|
||||
|
||||
value := strings.Split(string(formatValue([]byte(p.val))), "\n")
|
||||
if len(value) == 1 {
|
||||
screen.rightPaneBuffer = append(screen.rightPaneBuffer,
|
||||
Line{fmt.Sprintf("Value: %s", value[0]), style.defaultFg, style.defaultBg})
|
||||
Line{fmt.Sprintf("Value (%s): %s", p.valDatatype.Name(), value[0]), style.defaultFg, style.defaultBg})
|
||||
} else {
|
||||
screen.rightPaneBuffer = append(screen.rightPaneBuffer,
|
||||
Line{"Value:", style.defaultFg, style.defaultBg})
|
||||
@ -645,10 +660,12 @@ func (screen *BrowserScreen) buildRightPane(style Style) {
|
||||
}
|
||||
} else {
|
||||
screen.rightPaneBuffer = append(screen.rightPaneBuffer,
|
||||
Line{fmt.Sprintf("Path: %s", strings.Join(stringifyPath(screen.currentPath), " → ")), style.defaultFg, style.defaultBg})
|
||||
Line{fmt.Sprintf("Path: %s", strings.Join(nodePathToStringPath(screen.currentPath), " → ")), style.defaultFg, style.defaultBg})
|
||||
screen.rightPaneBuffer = append(screen.rightPaneBuffer,
|
||||
Line{err.Error(), termbox.ColorRed, termbox.ColorBlack})
|
||||
}
|
||||
screen.rightPaneBuffer = append(screen.rightPaneBuffer,
|
||||
Line{"", style.defaultFg, style.defaultBg})
|
||||
}
|
||||
|
||||
func (screen *BrowserScreen) drawRightPane(style Style) {
|
||||
@ -710,35 +727,27 @@ func (screen *BrowserScreen) bucketToLines(bkt *BoltBucket, style Style) []Line
|
||||
}
|
||||
bktPrefix := strings.Repeat(" ", len(bkt.GetPath())*2)
|
||||
if bkt.expanded {
|
||||
ret = append(ret, Line{bktPrefix + "- " + stringify([]byte(bkt.name)), bfg, bbg})
|
||||
ret = append(ret, Line{bktPrefix + "- " + bkt.GetStringName(), bfg, bbg})
|
||||
for i := range bkt.buckets {
|
||||
ret = append(ret, screen.bucketToLines(&bkt.buckets[i], style)...)
|
||||
}
|
||||
for _, bp := range bkt.pairs {
|
||||
if screen.filter != "" && !strings.Contains(bp.key, screen.filter) {
|
||||
continue
|
||||
}
|
||||
pfg, pbg := style.defaultFg, style.defaultBg
|
||||
if comparePaths(screen.currentPath, bp.GetPath()) {
|
||||
pfg, pbg = style.cursorFg, style.cursorBg
|
||||
}
|
||||
prPrefix := strings.Repeat(" ", len(bp.GetPath())*2)
|
||||
var pairString string
|
||||
if AppArgs.NoValue {
|
||||
pairString = fmt.Sprintf("%s%s", prPrefix, stringify([]byte(bp.key)))
|
||||
} else {
|
||||
pairString = fmt.Sprintf("%s%s: %s", prPrefix, stringify([]byte(bp.key)), stringify([]byte(bp.val)))
|
||||
}
|
||||
pairString := fmt.Sprintf("%s%s: %s", prPrefix, bp.GetStringKey(), bp.GetStringVal())
|
||||
ret = append(ret, Line{pairString, pfg, pbg})
|
||||
}
|
||||
} else {
|
||||
ret = append(ret, Line{bktPrefix + "+ " + stringify([]byte(bkt.name)), bfg, bbg})
|
||||
ret = append(ret, Line{bktPrefix + "+ " + bkt.GetStringName(), bfg, bbg})
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func (screen *BrowserScreen) startDeleteItem() bool {
|
||||
b, p, e := screen.db.getGenericFromPath(screen.currentPath)
|
||||
b, p, e := screen.db.getGenericFromStringPath(screen.currentPath)
|
||||
if e == nil {
|
||||
w, h := termbox.Size()
|
||||
inpW, inpH := (w / 2), 6
|
||||
@ -758,25 +767,8 @@ func (screen *BrowserScreen) startDeleteItem() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (screen *BrowserScreen) startFilter() bool {
|
||||
_, _, e := screen.db.getGenericFromPath(screen.currentPath)
|
||||
if e == nil {
|
||||
w, h := termbox.Size()
|
||||
inpW, inpH := (w / 2), 6
|
||||
inpX, inpY := ((w / 2) - (inpW / 2)), ((h / 2) - inpH)
|
||||
mod := termboxUtil.CreateInputModal("", inpX, inpY, inpW, inpH, termbox.ColorWhite, termbox.ColorBlack)
|
||||
mod.SetTitle(termboxUtil.AlignText("Filter", inpW, termboxUtil.AlignCenter))
|
||||
mod.SetValue(screen.filter)
|
||||
mod.Show()
|
||||
screen.inputModal = mod
|
||||
screen.mode = modeFilter
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (screen *BrowserScreen) startEditItem() bool {
|
||||
_, p, e := screen.db.getGenericFromPath(screen.currentPath)
|
||||
_, p, e := screen.db.getGenericFromStringPath(screen.currentPath)
|
||||
if e == nil {
|
||||
w, h := termbox.Size()
|
||||
inpW, inpH := (w / 2), 6
|
||||
@ -784,7 +776,7 @@ func (screen *BrowserScreen) startEditItem() bool {
|
||||
mod := termboxUtil.CreateInputModal("", inpX, inpY, inpW, inpH, termbox.ColorWhite, termbox.ColorBlack)
|
||||
if p != nil {
|
||||
mod.SetTitle(termboxUtil.AlignText(fmt.Sprintf("Input new value for '%s'", p.key), inpW, termboxUtil.AlignCenter))
|
||||
mod.SetValue(p.val)
|
||||
mod.SetValue(p.GetStringVal())
|
||||
}
|
||||
mod.Show()
|
||||
screen.inputModal = mod
|
||||
@ -795,18 +787,18 @@ func (screen *BrowserScreen) startEditItem() bool {
|
||||
}
|
||||
|
||||
func (screen *BrowserScreen) startRenameItem() bool {
|
||||
b, p, e := screen.db.getGenericFromPath(screen.currentPath)
|
||||
b, p, e := screen.db.getGenericFromStringPath(screen.currentPath)
|
||||
if e == nil {
|
||||
w, h := termbox.Size()
|
||||
inpW, inpH := (w / 2), 6
|
||||
inpX, inpY := ((w / 2) - (inpW / 2)), ((h / 2) - inpH)
|
||||
mod := termboxUtil.CreateInputModal("", inpX, inpY, inpW, inpH, termbox.ColorWhite, termbox.ColorBlack)
|
||||
if b != nil {
|
||||
mod.SetTitle(termboxUtil.AlignText(fmt.Sprintf("Rename Bucket '%s' to:", b.name), inpW, termboxUtil.AlignCenter))
|
||||
mod.SetValue(b.name)
|
||||
mod.SetTitle(termboxUtil.AlignText(fmt.Sprintf("Rename Bucket '%s' to:", b.GetStringName()), inpW, termboxUtil.AlignCenter))
|
||||
mod.SetValue(b.GetStringName())
|
||||
} else if p != nil {
|
||||
mod.SetTitle(termboxUtil.AlignText(fmt.Sprintf("Rename Key '%s' to:", p.key), inpW, termboxUtil.AlignCenter))
|
||||
mod.SetValue(p.key)
|
||||
mod.SetTitle(termboxUtil.AlignText(fmt.Sprintf("Rename Key '%s' to:", p.GetStringKey()), inpW, termboxUtil.AlignCenter))
|
||||
mod.SetValue(p.GetStringKey())
|
||||
}
|
||||
mod.Show()
|
||||
screen.inputModal = mod
|
||||
@ -835,11 +827,12 @@ func (screen *BrowserScreen) startInsertItemAtParent(tp BoltType) bool {
|
||||
}
|
||||
} else {
|
||||
var insPath string
|
||||
_, p, e := screen.db.getGenericFromPath(screen.currentPath[:len(screen.currentPath)-1])
|
||||
_, p, e := screen.db.getGenericFromStringPath(screen.currentPath[:len(screen.currentPath)-1])
|
||||
stringPath := nodePathToStringPath(screen.currentPath)
|
||||
if e == nil && p != nil {
|
||||
insPath = strings.Join(screen.currentPath[:len(screen.currentPath)-2], " → ") + " → "
|
||||
insPath = strings.Join(stringPath[:len(stringPath)-2], " → ") + " → "
|
||||
} else {
|
||||
insPath = strings.Join(screen.currentPath[:len(screen.currentPath)-1], " → ") + " → "
|
||||
insPath = strings.Join(stringPath[:len(stringPath)-1], " → ") + " → "
|
||||
}
|
||||
titlePrfx := ""
|
||||
if tp == typeBucket {
|
||||
@ -878,11 +871,12 @@ func (screen *BrowserScreen) startInsertItem(tp BoltType) bool {
|
||||
//mod.SetInputWrap(true)
|
||||
screen.inputModal = mod
|
||||
var insPath string
|
||||
_, p, e := screen.db.getGenericFromPath(screen.currentPath)
|
||||
_, p, e := screen.db.getGenericFromStringPath(screen.currentPath)
|
||||
strPath := nodePathToStringPath(screen.currentPath)
|
||||
if e == nil && p != nil {
|
||||
insPath = strings.Join(screen.currentPath[:len(screen.currentPath)-1], " → ") + " → "
|
||||
insPath = strings.Join(strPath[:len(strPath)-1], " → ") + " → "
|
||||
} else {
|
||||
insPath = strings.Join(screen.currentPath, " → ") + " → "
|
||||
insPath = strings.Join(strPath, " → ") + " → "
|
||||
}
|
||||
titlePrfx := ""
|
||||
if tp == typeBucket {
|
||||
@ -910,7 +904,7 @@ func (screen *BrowserScreen) startInsertItem(tp BoltType) bool {
|
||||
}
|
||||
|
||||
func (screen *BrowserScreen) startExportValue() bool {
|
||||
_, p, e := screen.db.getGenericFromPath(screen.currentPath)
|
||||
_, p, e := screen.db.getGenericFromStringPath(screen.currentPath)
|
||||
if e == nil && p != nil {
|
||||
w, h := termbox.Size()
|
||||
inpW, inpH := (w / 2), 6
|
||||
@ -920,15 +914,20 @@ func (screen *BrowserScreen) startExportValue() bool {
|
||||
mod.SetValue("")
|
||||
mod.Show()
|
||||
screen.inputModal = mod
|
||||
screen.mode = modeIOExportValue
|
||||
screen.mode = modeExportValue
|
||||
return true
|
||||
}
|
||||
screen.setMessage("Couldn't do string export on " + screen.currentPath[len(screen.currentPath)-1] + "(did you mean 'X'?)")
|
||||
expNode := screen.currentPath[len(screen.currentPath)-1]
|
||||
strVal, err := expNode.dataType.ToString(expNode.name)
|
||||
if err != nil {
|
||||
strVal = "ERROR"
|
||||
}
|
||||
screen.setMessage("Couldn't do string export on " + strVal + " (did you mean 'X'?)")
|
||||
return false
|
||||
}
|
||||
|
||||
func (screen *BrowserScreen) startExportJSON() bool {
|
||||
b, p, e := screen.db.getGenericFromPath(screen.currentPath)
|
||||
b, p, e := screen.db.getGenericFromStringPath(screen.currentPath)
|
||||
if e == nil {
|
||||
w, h := termbox.Size()
|
||||
inpW, inpH := (w / 2), 6
|
||||
@ -943,30 +942,12 @@ func (screen *BrowserScreen) startExportJSON() bool {
|
||||
}
|
||||
mod.Show()
|
||||
screen.inputModal = mod
|
||||
screen.mode = modeIOExportJSON
|
||||
screen.mode = modeExportJSON
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (screen *BrowserScreen) startImportValue() bool {
|
||||
_, p, e := screen.db.getGenericFromPath(screen.currentPath)
|
||||
if e == nil && p != nil {
|
||||
w, h := termbox.Size()
|
||||
inpW, inpH := (w / 2), 6
|
||||
inpX, inpY := ((w / 2) - (inpW / 2)), ((h / 2) - inpH)
|
||||
mod := termboxUtil.CreateInputModal("", inpX, inpY, inpW, inpH, termbox.ColorWhite, termbox.ColorBlack)
|
||||
mod.SetTitle(termboxUtil.AlignText(fmt.Sprintf("Import value of '%s' from:", p.key), inpW, termboxUtil.AlignCenter))
|
||||
mod.SetValue("")
|
||||
mod.Show()
|
||||
screen.inputModal = mod
|
||||
screen.mode = modeIOImportValue
|
||||
return true
|
||||
}
|
||||
screen.setMessage("Couldn't do import on " + screen.currentPath[len(screen.currentPath)-1] + ", must be a pair.")
|
||||
return false
|
||||
}
|
||||
|
||||
func (screen *BrowserScreen) setMessage(msg string) {
|
||||
screen.message = msg
|
||||
screen.messageTime = time.Now()
|
||||
@ -993,6 +974,31 @@ func (screen *BrowserScreen) refreshDatabase() {
|
||||
screen.db.syncOpenBuckets(shadowDB)
|
||||
}
|
||||
|
||||
func comparePaths(p1, p2 []string) bool {
|
||||
return strings.Join(p1, " → ") == strings.Join(p2, " → ")
|
||||
func findNextDatatype(curr string) Datatype {
|
||||
var first Datatype
|
||||
var found bool
|
||||
for k, v := range datatypes {
|
||||
if first == nil {
|
||||
first = v
|
||||
}
|
||||
if found {
|
||||
return v
|
||||
}
|
||||
if k == curr {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
return first
|
||||
}
|
||||
|
||||
func comparePaths(p1, p2 []PathNode) bool {
|
||||
if len(p1) != len(p2) {
|
||||
return false
|
||||
}
|
||||
for k := range p1 {
|
||||
if !bytes.Equal(p1[k].name, p2[k].name) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
15
stringify.go
15
stringify.go
@ -31,9 +31,14 @@ func stringify(v []byte) string {
|
||||
return fmt.Sprintf("%x", v)
|
||||
}
|
||||
|
||||
func stringifyPath(path []string) []string {
|
||||
for k, v := range path {
|
||||
path[k] = stringify([]byte(v))
|
||||
}
|
||||
return path
|
||||
func stringifyStringPath(path []string) []string {
|
||||
return stringifyPath(stringPathToBytePath(path))
|
||||
}
|
||||
|
||||
func stringifyPath(path [][]byte) []string {
|
||||
var ret []string
|
||||
for _, v := range path {
|
||||
ret = append(ret, stringify(v))
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user