feat(list): output is now tabwritten
This commit is contained in:
parent
cf8a19cba0
commit
1300bb76bf
2 changed files with 125 additions and 35 deletions
136
cmd/list.go
136
cmd/list.go
|
|
@ -24,6 +24,8 @@ package cmd
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"text/tabwriter"
|
||||||
|
|
||||||
"github.com/dgraph-io/badger/v4"
|
"github.com/dgraph-io/badger/v4"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
@ -60,9 +62,6 @@ func list(cmd *cobra.Command, args []string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if delimiter == "" {
|
|
||||||
delimiter = "\t\t"
|
|
||||||
}
|
|
||||||
|
|
||||||
includeSecret, err := cmd.Flags().GetBool("include-secret")
|
includeSecret, err := cmd.Flags().GetBool("include-secret")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -80,8 +79,34 @@ func list(cmd *cobra.Command, args []string) error {
|
||||||
if keysOnly && valuesOnly {
|
if keysOnly && valuesOnly {
|
||||||
return fmt.Errorf("--only-keys and --only-values are mutually exclusive")
|
return fmt.Errorf("--only-keys and --only-values are mutually exclusive")
|
||||||
}
|
}
|
||||||
|
showExpiry, err := cmd.Flags().GetBool("show-expiry")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
binary, err := cmd.Flags().GetBool("include-binary")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
includeKey := !valuesOnly
|
||||||
|
includeValue := !keysOnly
|
||||||
|
prefetchVals := includeValue
|
||||||
|
|
||||||
|
columnKinds := selectColumns(includeKey, includeValue, showExpiry)
|
||||||
|
if len(columnKinds) == 0 {
|
||||||
|
return fmt.Errorf("no columns selected; enable keys or values")
|
||||||
|
}
|
||||||
|
|
||||||
|
delimiterBytes := []byte(delimiter)
|
||||||
|
columnCount := len(columnKinds)
|
||||||
|
if len(delimiterBytes) > 0 && columnCount > 1 {
|
||||||
|
columnCount = columnCount*2 - 1
|
||||||
|
}
|
||||||
|
format := buildTabbedFormat(columnCount)
|
||||||
|
|
||||||
|
writer := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 2, ' ', 0)
|
||||||
|
defer writer.Flush()
|
||||||
|
|
||||||
prefetchVals := !keysOnly
|
|
||||||
placeholder := []byte("[secret: pass --include-secret to view]")
|
placeholder := []byte("[secret: pass --include-secret to view]")
|
||||||
|
|
||||||
trans := TransactionArgs{
|
trans := TransactionArgs{
|
||||||
|
|
@ -89,48 +114,43 @@ func list(cmd *cobra.Command, args []string) error {
|
||||||
readonly: true,
|
readonly: true,
|
||||||
sync: true,
|
sync: true,
|
||||||
transact: func(tx *badger.Txn, k []byte) error {
|
transact: func(tx *badger.Txn, k []byte) error {
|
||||||
binary, err := cmd.Flags().GetBool("include-binary")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
format := fmt.Sprintf("%%s%s%%s\n", delimiter)
|
|
||||||
opts := badger.DefaultIteratorOptions
|
opts := badger.DefaultIteratorOptions
|
||||||
opts.PrefetchSize = 10
|
opts.PrefetchSize = 10
|
||||||
opts.PrefetchValues = prefetchVals
|
opts.PrefetchValues = prefetchVals
|
||||||
it := tx.NewIterator(opts)
|
it := tx.NewIterator(opts)
|
||||||
defer it.Close()
|
defer it.Close()
|
||||||
|
var valueBuf []byte
|
||||||
for it.Rewind(); it.Valid(); it.Next() {
|
for it.Rewind(); it.Valid(); it.Next() {
|
||||||
item := it.Item()
|
item := it.Item()
|
||||||
key := item.Key()
|
key := item.KeyCopy(nil)
|
||||||
meta := item.UserMeta()
|
meta := item.UserMeta()
|
||||||
if meta&metaSecret != 0 && !includeSecret {
|
isSecret := meta&metaSecret != 0
|
||||||
switch {
|
valueBuf = valueBuf[:0]
|
||||||
case keysOnly:
|
if includeValue && (!isSecret || includeSecret) {
|
||||||
store.Print("%s\n", false, key)
|
|
||||||
case valuesOnly:
|
|
||||||
store.Print("%s\n", false, placeholder)
|
|
||||||
default:
|
|
||||||
store.Print(format, false, key, placeholder)
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
var preparedValue []byte
|
|
||||||
if !keysOnly {
|
|
||||||
if err := item.Value(func(v []byte) error {
|
if err := item.Value(func(v []byte) error {
|
||||||
preparedValue = append([]byte(nil), v...)
|
valueBuf = append(valueBuf[:0], v...)
|
||||||
return nil
|
return nil
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
switch {
|
columns := make([][]byte, 0, len(columnKinds))
|
||||||
case keysOnly:
|
for _, column := range columnKinds {
|
||||||
store.Print("%s\n", false, key)
|
switch column {
|
||||||
case valuesOnly:
|
case columnKey:
|
||||||
store.Print("%s\n", binary, preparedValue)
|
columns = append(columns, key)
|
||||||
default:
|
case columnValue:
|
||||||
store.Print(format, binary, key, preparedValue)
|
if isSecret && !includeSecret {
|
||||||
|
columns = append(columns, placeholder)
|
||||||
|
} else {
|
||||||
|
columns = append(columns, valueBuf)
|
||||||
}
|
}
|
||||||
|
case columnExpiry:
|
||||||
|
columns = append(columns, []byte(formatExpiry(item.ExpiresAt())))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
row := insertDelimiters(columns, delimiterBytes)
|
||||||
|
store.PrintTo(writer, format, binary, row...)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
|
|
@ -141,9 +161,61 @@ func list(cmd *cobra.Command, args []string) error {
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
listCmd.Flags().BoolP("include-binary", "b", false, "include binary data in text output")
|
listCmd.Flags().BoolP("include-binary", "b", false, "include binary data in text output")
|
||||||
listCmd.Flags().StringP("delimiter", "d", "\t\t", "string written between key and value columns")
|
listCmd.Flags().StringP("delimiter", "d", "", "string inserted between columns")
|
||||||
listCmd.Flags().Bool("include-secret", false, "include entries marked as secret")
|
listCmd.Flags().Bool("include-secret", false, "include entries marked as secret")
|
||||||
listCmd.Flags().BoolP("only-keys", "k", false, "only print keys")
|
listCmd.Flags().BoolP("only-keys", "k", false, "only print keys")
|
||||||
listCmd.Flags().BoolP("only-values", "v", false, "only print values")
|
listCmd.Flags().BoolP("only-values", "v", false, "only print values")
|
||||||
|
listCmd.Flags().Bool("show-expiry", false, "append an expiry column when entries have TTLs")
|
||||||
rootCmd.AddCommand(listCmd)
|
rootCmd.AddCommand(listCmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type columnKind int
|
||||||
|
|
||||||
|
const (
|
||||||
|
columnKey columnKind = iota
|
||||||
|
columnValue
|
||||||
|
columnExpiry
|
||||||
|
)
|
||||||
|
|
||||||
|
func selectColumns(includeKey, includeValue, showExpiry bool) []columnKind {
|
||||||
|
var columns []columnKind
|
||||||
|
if includeKey {
|
||||||
|
columns = append(columns, columnKey)
|
||||||
|
}
|
||||||
|
if includeValue {
|
||||||
|
columns = append(columns, columnValue)
|
||||||
|
}
|
||||||
|
if showExpiry {
|
||||||
|
columns = append(columns, columnExpiry)
|
||||||
|
}
|
||||||
|
return columns
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildTabbedFormat(cols int) string {
|
||||||
|
if cols <= 0 {
|
||||||
|
return "\n"
|
||||||
|
}
|
||||||
|
var b strings.Builder
|
||||||
|
for i := 0; i < cols; i++ {
|
||||||
|
if i > 0 {
|
||||||
|
b.WriteByte('\t')
|
||||||
|
}
|
||||||
|
b.WriteString("%s")
|
||||||
|
}
|
||||||
|
b.WriteByte('\n')
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func insertDelimiters(columns [][]byte, delimiter []byte) [][]byte {
|
||||||
|
if len(delimiter) == 0 || len(columns) <= 1 {
|
||||||
|
return columns
|
||||||
|
}
|
||||||
|
out := make([][]byte, 0, len(columns)*2-1)
|
||||||
|
for i, col := range columns {
|
||||||
|
out = append(out, col)
|
||||||
|
if i < len(columns)-1 {
|
||||||
|
out = append(out, delimiter)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,9 +23,11 @@ package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
"github.com/agnivade/levenshtein"
|
"github.com/agnivade/levenshtein"
|
||||||
|
|
@ -92,6 +94,10 @@ func (s *Store) Transaction(args TransactionArgs) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) Print(pf string, includeBinary bool, vs ...[]byte) {
|
func (s *Store) Print(pf string, includeBinary bool, vs ...[]byte) {
|
||||||
|
s.PrintTo(os.Stdout, pf, includeBinary, vs...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Store) PrintTo(w io.Writer, pf string, includeBinary bool, vs ...[]byte) {
|
||||||
nb := "(omitted binary data)"
|
nb := "(omitted binary data)"
|
||||||
fvs := make([]any, 0, len(vs))
|
fvs := make([]any, 0, len(vs))
|
||||||
tty := term.IsTerminal(int(os.Stdout.Fd()))
|
tty := term.IsTerminal(int(os.Stdout.Fd()))
|
||||||
|
|
@ -102,9 +108,9 @@ func (s *Store) Print(pf string, includeBinary bool, vs ...[]byte) {
|
||||||
fvs = append(fvs, string(v))
|
fvs = append(fvs, string(v))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Printf(pf, fvs...)
|
fmt.Fprintf(w, pf, fvs...)
|
||||||
if tty && !strings.HasSuffix(pf, "\n") {
|
if w == os.Stdout && tty && !strings.HasSuffix(pf, "\n") {
|
||||||
fmt.Println()
|
fmt.Fprintln(os.Stdout)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -228,3 +234,15 @@ func (s *Store) suggestStores(target string) ([]string, error) {
|
||||||
}
|
}
|
||||||
return suggestions, nil
|
return suggestions, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func formatExpiry(expiresAt uint64) string {
|
||||||
|
if expiresAt == 0 {
|
||||||
|
return "never"
|
||||||
|
}
|
||||||
|
expiry := time.Unix(int64(expiresAt), 0).UTC()
|
||||||
|
remaining := time.Until(expiry)
|
||||||
|
if remaining <= 0 {
|
||||||
|
return fmt.Sprintf("%s (expired)", expiry.Format(time.RFC3339))
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%s (in %s)", expiry.Format(time.RFC3339), remaining.Round(time.Second))
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue