refactor(list): extracts ListArgs construction into its own list_flags.go file
This commit is contained in:
parent
b99655a293
commit
983c07c829
2 changed files with 78 additions and 75 deletions
75
cmd/list.go
75
cmd/list.go
|
|
@ -42,22 +42,6 @@ var listCmd = &cobra.Command{
|
||||||
RunE: list,
|
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 {
|
func list(cmd *cobra.Command, args []string) error {
|
||||||
store := &Store{}
|
store := &Store{}
|
||||||
targetDB := "@default"
|
targetDB := "@default"
|
||||||
|
|
@ -162,37 +146,6 @@ func list(cmd *cobra.Command, args []string) error {
|
||||||
return nil
|
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() {
|
func init() {
|
||||||
listCmd.Flags().BoolVarP(&binary, "binary", "b", false, "include binary data in text output")
|
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")
|
listCmd.Flags().BoolVarP(&secret, "secret", "S", false, "display values marked as secret")
|
||||||
|
|
@ -204,34 +157,6 @@ func init() {
|
||||||
rootCmd.AddCommand(listCmd)
|
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
|
type columnKind int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
||||||
78
cmd/list_flags.go
Normal file
78
cmd/list_flags.go
Normal 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
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue