restructured data storage, values now have their own sub bucket

This commit is contained in:
2024-12-29 18:29:43 +01:00
parent c144e99b41
commit a4b6a3cfdf
12 changed files with 330 additions and 151 deletions

View File

@@ -20,17 +20,19 @@ import (
"fmt"
"io"
"os"
"strings"
"unicode/utf8"
)
type DbAttr struct {
Key string
Val string
Bin []byte
Preview string
Val []byte
Args []string
Tags []string
File string
Encrypted bool
Binary bool
}
func (attr *DbAttr) ParseKV() error {
@@ -43,7 +45,7 @@ func (attr *DbAttr) ParseKV() error {
}
case 2:
attr.Key = attr.Args[0]
attr.Val = attr.Args[1]
attr.Val = []byte(attr.Args[1])
if attr.Args[1] == "-" {
attr.File = "-"
@@ -51,7 +53,29 @@ func (attr *DbAttr) ParseKV() error {
}
if attr.File != "" {
return attr.GetFileValue()
if err := attr.GetFileValue(); err != nil {
return err
}
}
if attr.Binary {
attr.Preview = "<encrypted-content>"
} else {
if len(attr.Val) > MaxValueWidth {
attr.Preview = string(attr.Val)[0:MaxValueWidth] + "..."
if strings.Contains(attr.Preview, "\n") {
parts := strings.Split(attr.Preview, "\n")
if len(parts) > 0 {
attr.Preview = parts[0]
}
}
} else {
attr.Preview = string(attr.Val)
}
}
if attr.Encrypted {
attr.Preview = "<encrypted-content>"
}
return nil
@@ -82,11 +106,12 @@ func (attr *DbAttr) GetFileValue() error {
}
// poor man's text file test
sdata := string(data)
if utf8.ValidString(sdata) {
attr.Val = sdata
attr.Val = data
if utf8.ValidString(string(data)) {
attr.Binary = false
} else {
attr.Bin = data
attr.Binary = true
}
} else {
// read from console stdin
@@ -101,7 +126,7 @@ func (attr *DbAttr) GetFileValue() error {
data += input + "\n"
}
attr.Val = data
attr.Val = []byte(data)
}
return nil