Skip to content

Commit f3a81a3

Browse files
committed
fix: bring back PrintText so that parallel work doesn't fail
1 parent c8925de commit f3a81a3

2 files changed

Lines changed: 36 additions & 13 deletions

File tree

internal/cmd/base/list.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@ 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-
// OutputTable must be set. The base automatically registers --no-headers and
16-
// handles header suppression.
15+
// At least one of OutputTable or PrintText must be set. OutputTable is
16+
// preferred; when set, --no-headers is automatically registered and handled.
17+
// PrintText is the legacy fallback for commands that have not yet migrated.
1718
type ListCmd[T any] struct {
1819
Use string
1920
Short string
2021
Long string
2122
Example string
2223
Fetch func(s *state.State, cmd *cobra.Command) (T, error)
2324
OutputTable func(cmd *cobra.Command, out io.Writer, resp T) (output.TableRenderer, error)
25+
PrintText func(cmd *cobra.Command, out io.Writer, resp T) error
2426
ValidArgsFunction func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective)
2527
}
2628

@@ -40,20 +42,25 @@ func (lc ListCmd[T]) CobraCommand(s *state.State) *cobra.Command {
4042
if s.Config.JSONOutput() {
4143
return output.PrintJSON(cmd.OutOrStdout(), resp)
4244
}
43-
if lc.OutputTable == nil {
44-
panic("ListCmd: OutputTable must be set")
45+
if lc.OutputTable != nil {
46+
r, err := lc.OutputTable(cmd, cmd.OutOrStdout(), resp)
47+
if err != nil {
48+
return err
49+
}
50+
noHeaders, _ := cmd.Flags().GetBool("no-headers")
51+
r.SetNoHeaders(noHeaders)
52+
r.Render()
53+
return nil
4554
}
46-
r, err := lc.OutputTable(cmd, cmd.OutOrStdout(), resp)
47-
if err != nil {
48-
return err
55+
if lc.PrintText != nil {
56+
return lc.PrintText(cmd, cmd.OutOrStdout(), resp)
4957
}
50-
noHeaders, _ := cmd.Flags().GetBool("no-headers")
51-
r.SetNoHeaders(noHeaders)
52-
r.Render()
53-
return nil
58+
panic("ListCmd: OutputTable or PrintText must be set")
5459
},
5560
}
56-
cmd.Flags().Bool("no-headers", false, "Do not print column headers")
61+
if lc.OutputTable != nil {
62+
cmd.Flags().Bool("no-headers", false, "Do not print column headers")
63+
}
5764
if lc.ValidArgsFunction != nil {
5865
cmd.ValidArgsFunction = lc.ValidArgsFunction
5966
}

internal/cmd/base/list_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package base_test
22

33
import (
44
"bytes"
5+
"fmt"
56
"io"
67
"testing"
78

@@ -63,7 +64,22 @@ func TestListCmd_OutputTable_NoHeaders(t *testing.T) {
6364
assert.Contains(t, stdout, "hello")
6465
}
6566

66-
func TestListCmd_NoOutputTable_Panics(t *testing.T) {
67+
func TestListCmd_PrintText(t *testing.T) {
68+
lc := base.ListCmd[string]{
69+
Use: "test",
70+
Fetch: fetchHello,
71+
PrintText: func(_ *cobra.Command, out io.Writer, resp string) error {
72+
_, err := fmt.Fprint(out, resp)
73+
return err
74+
},
75+
}
76+
77+
stdout, err := execListCmd(t, lc)
78+
require.NoError(t, err)
79+
assert.Equal(t, "hello", stdout)
80+
}
81+
82+
func TestListCmd_NeitherOutputTableNorPrintText_Panics(t *testing.T) {
6783
lc := base.ListCmd[string]{
6884
Use: "test",
6985
Fetch: fetchHello,

0 commit comments

Comments
 (0)