refactor: remove --encoding flag from list/export commands
The auto-detection mode (encodeJsonEntry) is always correct — it uses text for valid UTF-8 and base64 for binary data. The explicit base64 and text modes added no practical value and had zero test coverage.
This commit is contained in:
parent
08025903ad
commit
2c9ecd7caf
4 changed files with 3 additions and 47 deletions
|
|
@ -43,6 +43,5 @@ var exportCmd = &cobra.Command{
|
||||||
func init() {
|
func init() {
|
||||||
exportCmd.Flags().StringSliceP("glob", "g", nil, "Filter keys with glob pattern (repeatable)")
|
exportCmd.Flags().StringSliceP("glob", "g", nil, "Filter keys with glob pattern (repeatable)")
|
||||||
exportCmd.Flags().String("glob-sep", "", fmt.Sprintf("Characters treated as separators for globbing (default %q)", defaultGlobSeparatorsDisplay()))
|
exportCmd.Flags().String("glob-sep", "", fmt.Sprintf("Characters treated as separators for globbing (default %q)", defaultGlobSeparatorsDisplay()))
|
||||||
exportCmd.Flags().StringVarP(&listEncoding, "encoding", "e", "auto", "value encoding: auto, base64, or text")
|
|
||||||
rootCmd.AddCommand(exportCmd)
|
rootCmd.AddCommand(exportCmd)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
45
cmd/list.go
45
cmd/list.go
|
|
@ -23,14 +23,12 @@ THE SOFTWARE.
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"unicode/utf8"
|
|
||||||
|
|
||||||
"github.com/jedib0t/go-pretty/v6/table"
|
"github.com/jedib0t/go-pretty/v6/table"
|
||||||
"github.com/jedib0t/go-pretty/v6/text"
|
"github.com/jedib0t/go-pretty/v6/text"
|
||||||
|
|
@ -60,9 +58,8 @@ var (
|
||||||
listNoKeys bool
|
listNoKeys bool
|
||||||
listNoValues bool
|
listNoValues bool
|
||||||
listTTL bool
|
listTTL bool
|
||||||
listHeader bool
|
listHeader bool
|
||||||
listFormat formatEnum = "table"
|
listFormat formatEnum = "table"
|
||||||
listEncoding string
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type columnKind int
|
type columnKind int
|
||||||
|
|
@ -155,16 +152,8 @@ func list(cmd *cobra.Command, args []string) error {
|
||||||
|
|
||||||
// NDJSON format: emit JSON lines directly
|
// NDJSON format: emit JSON lines directly
|
||||||
if listFormat.String() == "ndjson" {
|
if listFormat.String() == "ndjson" {
|
||||||
enc := listEncoding
|
|
||||||
if enc == "" {
|
|
||||||
enc = "auto"
|
|
||||||
}
|
|
||||||
for _, e := range filtered {
|
for _, e := range filtered {
|
||||||
je, err := encodeJsonEntryWithEncoding(e, enc)
|
data, err := json.Marshal(encodeJsonEntry(e))
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("cannot ls '%s': %v", targetDB, err)
|
|
||||||
}
|
|
||||||
data, err := json.Marshal(je)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("cannot ls '%s': %v", targetDB, err)
|
return fmt.Errorf("cannot ls '%s': %v", targetDB, err)
|
||||||
}
|
}
|
||||||
|
|
@ -310,33 +299,6 @@ func renderTable(tw table.Writer) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// encodeJsonEntryWithEncoding encodes an Entry to jsonEntry respecting the encoding mode.
|
|
||||||
func encodeJsonEntryWithEncoding(e Entry, mode string) (jsonEntry, error) {
|
|
||||||
switch mode {
|
|
||||||
case "base64":
|
|
||||||
je := jsonEntry{Key: e.Key, Encoding: "base64"}
|
|
||||||
je.Value = base64.StdEncoding.EncodeToString(e.Value)
|
|
||||||
if e.ExpiresAt > 0 {
|
|
||||||
ts := int64(e.ExpiresAt)
|
|
||||||
je.ExpiresAt = &ts
|
|
||||||
}
|
|
||||||
return je, nil
|
|
||||||
case "text":
|
|
||||||
if !utf8.Valid(e.Value) {
|
|
||||||
return jsonEntry{}, fmt.Errorf("key %q contains non-UTF8 data; use --encoding=auto or base64", e.Key)
|
|
||||||
}
|
|
||||||
je := jsonEntry{Key: e.Key, Encoding: "text"}
|
|
||||||
je.Value = string(e.Value)
|
|
||||||
if e.ExpiresAt > 0 {
|
|
||||||
ts := int64(e.ExpiresAt)
|
|
||||||
je.ExpiresAt = &ts
|
|
||||||
}
|
|
||||||
return je, nil
|
|
||||||
default: // "auto"
|
|
||||||
return encodeJsonEntry(e), nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
listCmd.Flags().BoolVarP(&listBinary, "binary", "b", false, "include binary data in text output")
|
listCmd.Flags().BoolVarP(&listBinary, "binary", "b", false, "include binary data in text output")
|
||||||
listCmd.Flags().BoolVar(&listNoKeys, "no-keys", false, "suppress the key column")
|
listCmd.Flags().BoolVar(&listNoKeys, "no-keys", false, "suppress the key column")
|
||||||
|
|
@ -346,6 +308,5 @@ func init() {
|
||||||
listCmd.Flags().VarP(&listFormat, "format", "o", "output format (table|tsv|csv|markdown|html|ndjson)")
|
listCmd.Flags().VarP(&listFormat, "format", "o", "output format (table|tsv|csv|markdown|html|ndjson)")
|
||||||
listCmd.Flags().StringSliceP("glob", "g", nil, "Filter keys with glob pattern (repeatable)")
|
listCmd.Flags().StringSliceP("glob", "g", nil, "Filter keys with glob pattern (repeatable)")
|
||||||
listCmd.Flags().String("glob-sep", "", fmt.Sprintf("Characters treated as separators for globbing (default %q)", defaultGlobSeparatorsDisplay()))
|
listCmd.Flags().String("glob-sep", "", fmt.Sprintf("Characters treated as separators for globbing (default %q)", defaultGlobSeparatorsDisplay()))
|
||||||
listCmd.Flags().StringVarP(&listEncoding, "encoding", "e", "auto", "value encoding for ndjson format: auto, base64, or text")
|
|
||||||
rootCmd.AddCommand(listCmd)
|
rootCmd.AddCommand(listCmd)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
2
testdata/help__dump__ok.ct
vendored
2
testdata/help__dump__ok.ct
vendored
|
|
@ -9,7 +9,6 @@ Aliases:
|
||||||
export, dump
|
export, dump
|
||||||
|
|
||||||
Flags:
|
Flags:
|
||||||
-e, --encoding string value encoding: auto, base64, or text (default "auto")
|
|
||||||
-g, --glob strings Filter keys with glob pattern (repeatable)
|
-g, --glob strings Filter keys with glob pattern (repeatable)
|
||||||
--glob-sep string Characters treated as separators for globbing (default "/-_.@: ")
|
--glob-sep string Characters treated as separators for globbing (default "/-_.@: ")
|
||||||
-h, --help help for export
|
-h, --help help for export
|
||||||
|
|
@ -22,7 +21,6 @@ Aliases:
|
||||||
export, dump
|
export, dump
|
||||||
|
|
||||||
Flags:
|
Flags:
|
||||||
-e, --encoding string value encoding: auto, base64, or text (default "auto")
|
|
||||||
-g, --glob strings Filter keys with glob pattern (repeatable)
|
-g, --glob strings Filter keys with glob pattern (repeatable)
|
||||||
--glob-sep string Characters treated as separators for globbing (default "/-_.@: ")
|
--glob-sep string Characters treated as separators for globbing (default "/-_.@: ")
|
||||||
-h, --help help for export
|
-h, --help help for export
|
||||||
|
|
|
||||||
2
testdata/help__list__ok.ct
vendored
2
testdata/help__list__ok.ct
vendored
|
|
@ -10,7 +10,6 @@ Aliases:
|
||||||
|
|
||||||
Flags:
|
Flags:
|
||||||
-b, --binary include binary data in text output
|
-b, --binary include binary data in text output
|
||||||
-e, --encoding string value encoding for ndjson format: auto, base64, or text (default "auto")
|
|
||||||
-o, --format format output format (table|tsv|csv|markdown|html|ndjson) (default table)
|
-o, --format format output format (table|tsv|csv|markdown|html|ndjson) (default table)
|
||||||
-g, --glob strings Filter keys with glob pattern (repeatable)
|
-g, --glob strings Filter keys with glob pattern (repeatable)
|
||||||
--glob-sep string Characters treated as separators for globbing (default "/-_.@: ")
|
--glob-sep string Characters treated as separators for globbing (default "/-_.@: ")
|
||||||
|
|
@ -29,7 +28,6 @@ Aliases:
|
||||||
|
|
||||||
Flags:
|
Flags:
|
||||||
-b, --binary include binary data in text output
|
-b, --binary include binary data in text output
|
||||||
-e, --encoding string value encoding for ndjson format: auto, base64, or text (default "auto")
|
|
||||||
-o, --format format output format (table|tsv|csv|markdown|html|ndjson) (default table)
|
-o, --format format output format (table|tsv|csv|markdown|html|ndjson) (default table)
|
||||||
-g, --glob strings Filter keys with glob pattern (repeatable)
|
-g, --glob strings Filter keys with glob pattern (repeatable)
|
||||||
--glob-sep string Characters treated as separators for globbing (default "/-_.@: ")
|
--glob-sep string Characters treated as separators for globbing (default "/-_.@: ")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue