Add support to importing of value from a file

This commit is contained in:
James Hewitt
2023-05-12 14:27:33 +01:00
parent ab21f99955
commit 8a508a45ee
3 changed files with 79 additions and 15 deletions
+33
View File
@@ -756,3 +756,36 @@ 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 *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")
})
}