Add support to importing of value from a file
This commit is contained in:
parent
ab21f99955
commit
8a508a45ee
@ -756,3 +756,36 @@ func writeToFile(fn, s string, mode int) error {
|
|||||||
}
|
}
|
||||||
return nil
|
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 *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()
|
||||||
|
}
|
||||||
|
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")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
@ -3,7 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/br0xen/termbox-util"
|
termboxUtil "github.com/br0xen/termbox-util"
|
||||||
"github.com/nsf/termbox-go"
|
"github.com/nsf/termbox-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -111,9 +111,9 @@ func (screen *AboutScreen) drawScreen(style Style) {
|
|||||||
{"e", "edit value of pair"},
|
{"e", "edit value of pair"},
|
||||||
{"r", "rename pair/bucket"},
|
{"r", "rename pair/bucket"},
|
||||||
{"", ""},
|
{"", ""},
|
||||||
{"", ""},
|
|
||||||
{"D", "delete item"},
|
{"D", "delete item"},
|
||||||
{"x,X", "export as string/json to file"},
|
{"x,X", "export as string/json to file"},
|
||||||
|
{"i", "import file to value of pair"},
|
||||||
{"", ""},
|
{"", ""},
|
||||||
{"?", "this screen"},
|
{"?", "this screen"},
|
||||||
{"q", "quit program"},
|
{"q", "quit program"},
|
||||||
|
@ -61,9 +61,10 @@ const (
|
|||||||
modeInsertPairVal = 70 // 0000 0100 0110
|
modeInsertPairVal = 70 // 0000 0100 0110
|
||||||
modeDelete = 256 // 0001 0000 0000
|
modeDelete = 256 // 0001 0000 0000
|
||||||
modeModToParent = 8 // 0000 0000 1000
|
modeModToParent = 8 // 0000 0000 1000
|
||||||
modeExport = 512 // 0010 0000 0000
|
modeIO = 512 // 0010 0000 0000
|
||||||
modeExportValue = 513 // 0010 0000 0001
|
modeIOExportValue = 513 // 0010 0000 0001
|
||||||
modeExportJSON = 514 // 0010 0000 0010
|
modeIOExportJSON = 514 // 0010 0000 0010
|
||||||
|
modeIOImportValue = 516 // 0010 0000 0100
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -88,8 +89,8 @@ func (screen *BrowserScreen) handleKeyEvent(event termbox.Event) int {
|
|||||||
return screen.handleInsertKeyEvent(event)
|
return screen.handleInsertKeyEvent(event)
|
||||||
} else if screen.mode == modeDelete {
|
} else if screen.mode == modeDelete {
|
||||||
return screen.handleDeleteKeyEvent(event)
|
return screen.handleDeleteKeyEvent(event)
|
||||||
} else if screen.mode&modeExport == modeExport {
|
} else if screen.mode&modeIO == modeIO {
|
||||||
return screen.handleExportKeyEvent(event)
|
return screen.handleIOKeyEvent(event)
|
||||||
}
|
}
|
||||||
return BrowserScreenIndex
|
return BrowserScreenIndex
|
||||||
}
|
}
|
||||||
@ -204,11 +205,14 @@ func (screen *BrowserScreen) handleBrowseKeyEvent(event termbox.Event) int {
|
|||||||
} else if event.Ch == 'D' {
|
} else if event.Ch == 'D' {
|
||||||
screen.startDeleteItem()
|
screen.startDeleteItem()
|
||||||
} else if event.Ch == 'x' {
|
} else if event.Ch == 'x' {
|
||||||
// Export Value
|
// Export Value to a file
|
||||||
screen.startExportValue()
|
screen.startExportValue()
|
||||||
} else if event.Ch == 'X' {
|
} else if event.Ch == 'X' {
|
||||||
// Export Key/Value (or Bucket) as JSON
|
// Export Key/Value (or Bucket) as JSON
|
||||||
screen.startExportJSON()
|
screen.startExportJSON()
|
||||||
|
} else if event.Ch == 'i' {
|
||||||
|
// Import value from a file
|
||||||
|
screen.startImportValue()
|
||||||
}
|
}
|
||||||
return BrowserScreenIndex
|
return BrowserScreenIndex
|
||||||
}
|
}
|
||||||
@ -374,7 +378,7 @@ func (screen *BrowserScreen) handleInsertKeyEvent(event termbox.Event) int {
|
|||||||
return BrowserScreenIndex
|
return BrowserScreenIndex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (screen *BrowserScreen) handleExportKeyEvent(event termbox.Event) int {
|
func (screen *BrowserScreen) handleIOKeyEvent(event termbox.Event) int {
|
||||||
if event.Key == termbox.KeyEsc {
|
if event.Key == termbox.KeyEsc {
|
||||||
screen.mode = modeBrowse
|
screen.mode = modeBrowse
|
||||||
screen.inputModal.Clear()
|
screen.inputModal.Clear()
|
||||||
@ -383,24 +387,33 @@ func (screen *BrowserScreen) handleExportKeyEvent(event termbox.Event) int {
|
|||||||
if screen.inputModal.IsDone() {
|
if screen.inputModal.IsDone() {
|
||||||
b, p, _ := screen.db.getGenericFromPath(screen.currentPath)
|
b, p, _ := screen.db.getGenericFromPath(screen.currentPath)
|
||||||
fileName := screen.inputModal.GetValue()
|
fileName := screen.inputModal.GetValue()
|
||||||
if screen.mode&modeExportValue == modeExportValue {
|
if screen.mode&modeIOExportValue == modeIOExportValue {
|
||||||
// Exporting the value
|
// Exporting the value
|
||||||
if p != nil {
|
if p != nil {
|
||||||
if err := exportValue(screen.currentPath, fileName); err != 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())
|
screen.setMessage(err.Error())
|
||||||
} else {
|
} else {
|
||||||
screen.setMessage("Value exported to file: " + fileName)
|
screen.setMessage("Value exported to file: " + fileName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if screen.mode&modeExportJSON == modeExportJSON {
|
} else if screen.mode&modeIOExportJSON == modeIOExportJSON {
|
||||||
if b != nil || p != nil {
|
if b != nil || p != nil {
|
||||||
if exportJSON(screen.currentPath, fileName) != nil {
|
if exportJSON(screen.currentPath, fileName) != nil {
|
||||||
screen.setMessage("Error Exporting to file " + fileName + ".")
|
screen.setMessage("Error exporting to file " + fileName + ".")
|
||||||
} else {
|
} else {
|
||||||
screen.setMessage("Value exported to file: " + fileName)
|
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.mode = modeBrowse
|
||||||
screen.inputModal.Clear()
|
screen.inputModal.Clear()
|
||||||
@ -907,7 +920,7 @@ func (screen *BrowserScreen) startExportValue() bool {
|
|||||||
mod.SetValue("")
|
mod.SetValue("")
|
||||||
mod.Show()
|
mod.Show()
|
||||||
screen.inputModal = mod
|
screen.inputModal = mod
|
||||||
screen.mode = modeExportValue
|
screen.mode = modeIOExportValue
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
screen.setMessage("Couldn't do string export on " + screen.currentPath[len(screen.currentPath)-1] + "(did you mean 'X'?)")
|
screen.setMessage("Couldn't do string export on " + screen.currentPath[len(screen.currentPath)-1] + "(did you mean 'X'?)")
|
||||||
@ -930,12 +943,30 @@ func (screen *BrowserScreen) startExportJSON() bool {
|
|||||||
}
|
}
|
||||||
mod.Show()
|
mod.Show()
|
||||||
screen.inputModal = mod
|
screen.inputModal = mod
|
||||||
screen.mode = modeExportJSON
|
screen.mode = modeIOExportJSON
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
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) {
|
func (screen *BrowserScreen) setMessage(msg string) {
|
||||||
screen.message = msg
|
screen.message = msg
|
||||||
screen.messageTime = time.Now()
|
screen.messageTime = time.Now()
|
||||||
|
Loading…
Reference in New Issue
Block a user