refactor(list): extracts ListArgs construction into its own list_flags.go file

This commit is contained in:
Lewis Wynne 2025-11-08 14:57:51 +00:00
parent b99655a293
commit 983c07c829
2 changed files with 78 additions and 75 deletions

View file

@ -42,22 +42,6 @@ var listCmd = &cobra.Command{
RunE: list,
}
type ListArgs struct {
header bool
key bool
value bool
ttl bool
binary bool
secrets bool
render func(table.Writer)
}
type listFormat struct {
limitColumns bool
style *table.Style
render func(table.Writer)
}
func list(cmd *cobra.Command, args []string) error {
store := &Store{}
targetDB := "@default"
@ -162,37 +146,6 @@ func list(cmd *cobra.Command, args []string) error {
return nil
}
// formatEnum implements pflag.Value
type formatEnum string
func (e *formatEnum) String() string {
return string(*e)
}
func (e *formatEnum) Set(v string) error {
switch v {
case "table", "csv", "html", "markdown":
*e = formatEnum(v)
return nil
default:
return errors.New(`must be one of "table", "csv", "html", or "markdown"`)
}
}
func (e *formatEnum) Type() string {
return "format"
}
var (
binary bool = false
secret bool = false
noKeys bool = false
noValues bool = false
ttl bool = false
noHeader bool = false
format formatEnum = "table"
)
func init() {
listCmd.Flags().BoolVarP(&binary, "binary", "b", false, "include binary data in text output")
listCmd.Flags().BoolVarP(&secret, "secret", "S", false, "display values marked as secret")
@ -204,34 +157,6 @@ func init() {
rootCmd.AddCommand(listCmd)
}
func parseFlags(cmd *cobra.Command) (ListArgs, error) {
var renderFunc func(tw table.Writer)
switch format.String() {
case "csv":
renderFunc = func(tw table.Writer) { tw.RenderCSV() }
case "html":
renderFunc = func(tw table.Writer) { tw.RenderHTML() }
case "markdown":
renderFunc = func(tw table.Writer) { tw.RenderMarkdown() }
default:
renderFunc = func(tw table.Writer) { tw.Render() }
}
if noKeys && noValues && !ttl {
return ListArgs{}, fmt.Errorf("no columns selected; disable --no-keys/--no-values or pass --ttl")
}
return ListArgs{
header: !noHeader,
key: !noKeys,
value: !noValues,
ttl: ttl,
binary: binary,
render: renderFunc,
secrets: secret,
}, nil
}
type columnKind int
const (

78
cmd/list_flags.go Normal file
View file

@ -0,0 +1,78 @@
package cmd
import (
"fmt"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
)
// ListArgs tracks the resolved flag configuration for the list command.
type ListArgs struct {
header bool
key bool
value bool
ttl bool
binary bool
secrets bool
render func(table.Writer)
}
// formatEnum implements pflag.Value for format selection.
type formatEnum string
func (e *formatEnum) String() string {
return string(*e)
}
func (e *formatEnum) Set(v string) error {
switch v {
case "table", "csv", "html", "markdown":
*e = formatEnum(v)
return nil
default:
return fmt.Errorf("must be one of \"table\", \"csv\", \"html\", or \"markdown\"")
}
}
func (e *formatEnum) Type() string {
return "format"
}
var (
binary bool = false
secret bool = false
noKeys bool = false
noValues bool = false
ttl bool = false
noHeader bool = false
format formatEnum = "table"
)
func parseFlags(cmd *cobra.Command) (ListArgs, error) {
var renderFunc func(tw table.Writer)
switch format.String() {
case "csv":
renderFunc = func(tw table.Writer) { tw.RenderCSV() }
case "html":
renderFunc = func(tw table.Writer) { tw.RenderHTML() }
case "markdown":
renderFunc = func(tw table.Writer) { tw.RenderMarkdown() }
default:
renderFunc = func(tw table.Writer) { tw.Render() }
}
if noKeys && noValues && !ttl {
return ListArgs{}, fmt.Errorf("no columns selected; disable --no-keys/--no-values or pass --ttl")
}
return ListArgs{
header: !noHeader,
key: !noKeys,
value: !noValues,
ttl: ttl,
binary: binary,
render: renderFunc,
secrets: secret,
}, nil
}