feat(dump): dump on a per-entry basis, using whichever encoding works. or force all into a specified type
This commit is contained in:
parent
571e692ea5
commit
86867a097b
1 changed files with 33 additions and 8 deletions
39
cmd/dump.go
39
cmd/dump.go
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
"github.com/dgraph-io/badger/v4"
|
"github.com/dgraph-io/badger/v4"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
@ -42,12 +43,14 @@ func dump(cmd *cobra.Command, args []string) error {
|
||||||
targetDB = "@" + dbName
|
targetDB = "@" + dbName
|
||||||
}
|
}
|
||||||
|
|
||||||
encoding, err := cmd.Flags().GetString("encoding")
|
mode, err := cmd.Flags().GetString("encoding")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if encoding != "base64" && encoding != "text" {
|
switch mode {
|
||||||
return fmt.Errorf("unsupported encoding %q", encoding)
|
case "auto", "base64", "text":
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unsupported encoding %q", mode)
|
||||||
}
|
}
|
||||||
|
|
||||||
trans := TransactionArgs{
|
trans := TransactionArgs{
|
||||||
|
|
@ -64,12 +67,20 @@ func dump(cmd *cobra.Command, args []string) error {
|
||||||
key := item.KeyCopy(nil)
|
key := item.KeyCopy(nil)
|
||||||
if err := item.Value(func(v []byte) error {
|
if err := item.Value(func(v []byte) error {
|
||||||
entry := dumpEntry{Key: string(key)}
|
entry := dumpEntry{Key: string(key)}
|
||||||
switch encoding {
|
switch mode {
|
||||||
case "base64":
|
case "base64":
|
||||||
entry.Value = base64.StdEncoding.EncodeToString(v)
|
encodeBase64(&entry, v)
|
||||||
entry.Encoding = "base64"
|
|
||||||
case "text":
|
case "text":
|
||||||
|
if err := encodeText(&entry, key, v); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case "auto":
|
||||||
|
if utf8.Valid(v) {
|
||||||
|
entry.Encoding = "text"
|
||||||
entry.Value = string(v)
|
entry.Value = string(v)
|
||||||
|
} else {
|
||||||
|
encodeBase64(&entry, v)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
payload, err := json.Marshal(entry)
|
payload, err := json.Marshal(entry)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -89,6 +100,20 @@ func dump(cmd *cobra.Command, args []string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
dumpCmd.Flags().StringP("encoding", "e", "base64", "value encoding: base64 or text")
|
dumpCmd.Flags().StringP("encoding", "e", "auto", "value encoding: auto, base64, or text")
|
||||||
rootCmd.AddCommand(dumpCmd)
|
rootCmd.AddCommand(dumpCmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func encodeBase64(entry *dumpEntry, v []byte) {
|
||||||
|
entry.Value = base64.StdEncoding.EncodeToString(v)
|
||||||
|
entry.Encoding = "base64"
|
||||||
|
}
|
||||||
|
|
||||||
|
func encodeText(entry *dumpEntry, key []byte, v []byte) error {
|
||||||
|
if !utf8.Valid(v) {
|
||||||
|
return fmt.Errorf("key %q contains non-UTF8 data; use --encoding=auto or base64", key)
|
||||||
|
}
|
||||||
|
entry.Value = string(v)
|
||||||
|
entry.Encoding = "text"
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue