@@ -12,17 +12,15 @@ import (
1212// ListCmd defines a command for fetching and displaying a list response.
1313// T is the full response proto message (e.g. *clusterv1.ListClustersResponse).
1414//
15- // For table output, prefer OutputTable over PrintText. When OutputTable is set,
16- // the base automatically registers --no-headers and handles header suppression.
17- // PrintText is used as a fallback when OutputTable is not set.
15+ // OutputTable must be set. The base automatically registers --no-headers and
16+ // handles header suppression.
1817type ListCmd [T any ] struct {
1918 Use string
2019 Short string
2120 Long string
2221 Example string
2322 Fetch func (s * state.State , cmd * cobra.Command ) (T , error )
24- OutputTable func (cmd * cobra.Command , out io.Writer , resp T ) output.TableRenderer
25- PrintText func (cmd * cobra.Command , out io.Writer , resp T ) error
23+ OutputTable func (cmd * cobra.Command , out io.Writer , resp T ) (output.TableRenderer , error )
2624 ValidArgsFunction func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective )
2725}
2826
@@ -42,22 +40,20 @@ func (lc ListCmd[T]) CobraCommand(s *state.State) *cobra.Command {
4240 if s .Config .JSONOutput () {
4341 return output .PrintJSON (cmd .OutOrStdout (), resp )
4442 }
45- if lc .OutputTable != nil {
46- r := lc .OutputTable (cmd , cmd .OutOrStdout (), resp )
47- noHeaders , _ := cmd .Flags ().GetBool ("no-headers" )
48- r .SetNoHeaders (noHeaders )
49- r .Render ()
50- return nil
43+ if lc .OutputTable == nil {
44+ panic ("ListCmd: OutputTable must be set" )
5145 }
52- if lc .PrintText == nil {
53- panic ("ListCmd: either OutputTable or PrintText must be set" )
46+ r , err := lc .OutputTable (cmd , cmd .OutOrStdout (), resp )
47+ if err != nil {
48+ return err
5449 }
55- return lc .PrintText (cmd , cmd .OutOrStdout (), resp )
50+ noHeaders , _ := cmd .Flags ().GetBool ("no-headers" )
51+ r .SetNoHeaders (noHeaders )
52+ r .Render ()
53+ return nil
5654 },
5755 }
58- if lc .OutputTable != nil {
59- cmd .Flags ().Bool ("no-headers" , false , "Do not print column headers" )
60- }
56+ cmd .Flags ().Bool ("no-headers" , false , "Do not print column headers" )
6157 if lc .ValidArgsFunction != nil {
6258 cmd .ValidArgsFunction = lc .ValidArgsFunction
6359 }
0 commit comments