feat: include summary of omitted binary data

This commit is contained in:
Lewis Wynne 2026-02-11 13:17:23 +00:00
parent d63c1fd77b
commit 07330be10b
9 changed files with 57 additions and 30 deletions

View file

@ -100,7 +100,7 @@ func get(cmd *cobra.Command, args []string) error {
}
v := entry.Value
binary, err := cmd.Flags().GetBool("include-binary")
binary, err := cmd.Flags().GetBool("base64")
if err != nil {
return fmt.Errorf("cannot get '%s': %v", args[0], err)
}
@ -235,12 +235,12 @@ func run(cmd *cobra.Command, args []string) error {
var runFlag bool
func init() {
getCmd.Flags().BoolP("include-binary", "b", false, "include binary data in text output")
getCmd.Flags().BoolP("base64", "b", false, "view binary data as base64")
getCmd.Flags().BoolVarP(&runFlag, "run", "c", false, "execute the result as a shell command")
getCmd.Flags().Bool("no-template", false, "directly output template syntax")
rootCmd.AddCommand(getCmd)
runCmd.Flags().BoolP("include-binary", "b", false, "include binary data in text output")
runCmd.Flags().BoolP("base64", "b", false, "view binary data as base64")
runCmd.Flags().Bool("no-template", false, "directly output template syntax")
rootCmd.AddCommand(runCmd)
}

View file

@ -55,7 +55,7 @@ func (e *formatEnum) Set(v string) error {
func (e *formatEnum) Type() string { return "format" }
var (
listBinary bool
listBase64 bool
listNoKeys bool
listNoValues bool
listTTL bool
@ -194,7 +194,7 @@ func list(cmd *cobra.Command, args []string) error {
if e.Locked {
valueStr = "locked (identity file missing)"
} else {
valueStr = store.FormatBytes(listBinary, e.Value)
valueStr = store.FormatBytes(listBase64, e.Value)
}
}
row := make(table.Row, 0, len(columns))
@ -315,7 +315,7 @@ func renderTable(tw table.Writer) {
}
func init() {
listCmd.Flags().BoolVarP(&listBinary, "binary", "b", false, "include binary data in text output")
listCmd.Flags().BoolVarP(&listBase64, "base64", "b", false, "view binary data as base64")
listCmd.Flags().BoolVar(&listNoKeys, "no-keys", false, "suppress the key column")
listCmd.Flags().BoolVar(&listNoValues, "no-values", false, "suppress the value column")
listCmd.Flags().BoolVarP(&listTTL, "ttl", "t", false, "append a TTL column when entries expire")

View file

@ -23,8 +23,10 @@ THE SOFTWARE.
package cmd
import (
"encoding/base64"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
@ -67,14 +69,39 @@ func (s *Store) FormatBytes(includeBinary bool, v []byte) string {
return s.formatBytes(includeBinary, v)
}
func (s *Store) formatBytes(includeBinary bool, v []byte) string {
tty := term.IsTerminal(int(os.Stdout.Fd()))
if tty && !includeBinary && !utf8.Valid(v) {
return "(omitted binary data)"
func (s *Store) formatBytes(base64Flag bool, v []byte) string {
if !utf8.Valid(v) {
tty := term.IsTerminal(int(os.Stdout.Fd()))
if !tty {
return string(v)
}
if base64Flag {
return base64.StdEncoding.EncodeToString(v)
}
mime := http.DetectContentType(v)
return fmt.Sprintf("(binary: %s, %s)", formatSize(len(v)), mime)
}
return string(v)
}
func formatSize(n int) string {
const (
kb = 1024
mb = 1024 * kb
gb = 1024 * mb
)
switch {
case n < kb:
return fmt.Sprintf("%d B", n)
case n < mb:
return fmt.Sprintf("%.1f KB", float64(n)/float64(kb))
case n < gb:
return fmt.Sprintf("%.1f MB", float64(n)/float64(mb))
default:
return fmt.Sprintf("%.1f GB", float64(n)/float64(gb))
}
}
func (s *Store) storePath(name string) (string, error) {
if name == "" {
name = config.Store.DefaultStoreName