Added export functions

* (x) - Export value as string
* (X) - Export key/value or bucket as JSON
This commit is contained in:
Brian Buller 2016-12-29 15:23:58 -06:00
parent 6a2dc9f776
commit e1430823a8
3 changed files with 162 additions and 3 deletions

View File

@ -559,17 +559,89 @@ func insertPair(path []string, k string, v string) error {
return err
}
func exportValue(path []string, 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 {
if len(path) > 1 {
for i := range path[1 : len(path)-1] {
b = b.Bucket([]byte(path[i+1]))
if b == nil {
return errors.New("exportValue: Invalid Path: " + strings.Join(path, "/"))
}
}
}
bk := []byte(path[len(path)-1])
v := b.Get(bk)
return writeToFile(fName, string(v)+"\n", os.O_CREATE|os.O_WRONLY|os.O_TRUNC)
} else {
return errors.New("exportValue: Invalid Bucket")
}
return nil
})
}
func exportJSON(path []string, 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 {
if len(path) > 1 {
for i := range path[1 : len(path)-1] {
b = b.Bucket([]byte(path[i+1]))
if b == nil {
return errors.New("exportValue: Invalid Path: " + strings.Join(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)
}
if b.Bucket(bk) != nil {
return writeToFile(fName, genJSONString(b.Bucket(bk)), os.O_CREATE|os.O_WRONLY|os.O_TRUNC)
}
return writeToFile(fName, genJSONString(b), os.O_CREATE|os.O_WRONLY|os.O_TRUNC)
} else {
return errors.New("exportValue: Invalid Bucket")
}
return nil
})
}
func genJSONString(b *bolt.Bucket) string {
ret := "{"
b.ForEach(func(k, v []byte) error {
ret = fmt.Sprintf("%s\"%s\":", ret, string(k))
if v == nil {
ret = fmt.Sprintf("%s%s,", ret, genJSONString(b.Bucket(k)))
} else {
ret = fmt.Sprintf("%s\"%s\",", ret, string(v))
}
return nil
})
ret = fmt.Sprintf("%s}", ret[:len(ret)-1])
return ret
}
var f *os.File
func logToFile(s string) error {
return writeToFile("bolt-log", s+"\n", os.O_RDWR|os.O_APPEND)
}
func writeToFile(fn, s string, mode int) error {
var err error
if f == nil {
f, err = os.OpenFile("bolt-log", os.O_RDWR|os.O_APPEND, 0660)
f, err = os.OpenFile(fn, mode, 0660)
}
if err != nil {
return err
}
if _, err = f.WriteString(s + "\n"); err != nil {
if _, err = f.WriteString(s); err != nil {
return err
}
if err = f.Sync(); err != nil {

View File

@ -112,6 +112,7 @@ 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"},
{"?", "this screen"},
{"q", "quit program"},

View File

@ -49,9 +49,12 @@ const (
modeInsertBucket = 65 // 0000 0100 0001
modeInsertPair = 68 // 0000 0100 0100
modeInsertPairKey = 69 // 0000 0100 0101
modeInserPairVal = 70 // 0000 0100 0110
modeInsertPairVal = 70 // 0000 0100 0110
modeDelete = 256 // 0001 0000 0000
modeModToParent = 8 // 0000 0000 1000
modeExport = 512 // 0010 0000 0000
modeExportValue = 513 // 0010 0000 0001
modeExportJSON = 514 // 0010 0000 0010
)
/*
@ -76,6 +79,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&modeExport == modeExport {
return screen.handleExportKeyEvent(event)
}
return BrowserScreenIndex
}
@ -181,6 +186,12 @@ func (screen *BrowserScreen) handleBrowseKeyEvent(event termbox.Event) int {
} else if event.Ch == 'D' {
screen.startDeleteItem()
} else if event.Ch == 'x' {
// Export Value
screen.startExportValue()
} else if event.Ch == 'X' {
// Export Key/Value (or Bucket) as JSON
screen.startExportJSON()
}
return BrowserScreenIndex
}
@ -340,6 +351,41 @@ func (screen *BrowserScreen) handleInsertKeyEvent(event termbox.Event) int {
return BrowserScreenIndex
}
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)
fileName := screen.inputModal.GetValue()
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(err.Error())
} else {
screen.setMessage("Value exported to file: " + fileName)
}
}
} else if screen.mode&modeExportJSON == modeExportJSON {
if b != nil || p != nil {
if exportJSON(screen.currentPath, fileName) != nil {
screen.setMessage("Error Exporting to file " + fileName + ".")
} else {
screen.setMessage("Value exported to file: " + fileName)
}
}
}
screen.mode = modeBrowse
screen.inputModal.Clear()
}
}
return BrowserScreenIndex
}
func (screen *BrowserScreen) jumpCursorUp(distance int) bool {
// Jump up 'distance' lines
visPaths, err := screen.db.buildVisiblePathSlice()
@ -762,6 +808,46 @@ func (screen *BrowserScreen) startInsertItem(tp BoltType) bool {
return false
}
func (screen *BrowserScreen) startExportValue() 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("Export value of '%s' to:", p.key), inpW, termboxUtil.AlignCenter))
mod.SetValue("")
mod.Show()
screen.inputModal = mod
screen.mode = modeExportValue
return true
}
screen.setMessage("Couldn't do string export on " + screen.currentPath[len(screen.currentPath)-1] + "(did you mean 'X'?)")
return false
}
func (screen *BrowserScreen) startExportJSON() bool {
b, p, 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)
if b != nil {
mod.SetTitle(termboxUtil.AlignText(fmt.Sprintf("Export JSON of '%s' to:", b.name), inpW, termboxUtil.AlignCenter))
mod.SetValue("")
} else if p != nil {
mod.SetTitle(termboxUtil.AlignText(fmt.Sprintf("Export JSON of '%s' to:", p.key), inpW, termboxUtil.AlignCenter))
mod.SetValue("")
}
mod.Show()
screen.inputModal = mod
screen.mode = modeExportJSON
return true
}
return false
}
// Print text on multiple lines, if needed
// msg - What to print
// mlPadding - number of spaces to pad lines after the first