Skip to content

Commit e6f568f

Browse files
authored
refactor: cli: address comments from #4240 (#4259)
1 parent 028a4ed commit e6f568f

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

cli/cliui/table.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func DisplayTable(out any, sort string, filterColumns []string) (string, error)
6767
}
6868

6969
// Get the list of table column headers.
70-
headersRaw, err := TypeToTableHeaders(v.Type().Elem())
70+
headersRaw, err := typeToTableHeaders(v.Type().Elem())
7171
if err != nil {
7272
return "", xerrors.Errorf("get table headers recursively for type %q: %w", v.Type().Elem().String(), err)
7373
}
@@ -207,10 +207,10 @@ func isStructOrStructPointer(t reflect.Type) bool {
207207
return t.Kind() == reflect.Struct || (t.Kind() == reflect.Pointer && t.Elem().Kind() == reflect.Struct)
208208
}
209209

210-
// TypeToTableHeaders converts a type to a slice of column names. If the given
210+
// typeToTableHeaders converts a type to a slice of column names. If the given
211211
// type is invalid (not a struct or a pointer to a struct, has invalid table
212212
// tags, etc.), an error is returned.
213-
func TypeToTableHeaders(t reflect.Type) ([]string, error) {
213+
func typeToTableHeaders(t reflect.Type) ([]string, error) {
214214
if !isStructOrStructPointer(t) {
215215
return nil, xerrors.Errorf("typeToTableHeaders called with a non-struct or a non-pointer-to-a-struct type")
216216
}
@@ -235,7 +235,7 @@ func TypeToTableHeaders(t reflect.Type) ([]string, error) {
235235
return nil, xerrors.Errorf("field %q in type %q is marked as recursive but does not contain a struct or a pointer to a struct", field.Name, t.String())
236236
}
237237

238-
childNames, err := TypeToTableHeaders(fieldType)
238+
childNames, err := typeToTableHeaders(fieldType)
239239
if err != nil {
240240
return nil, xerrors.Errorf("get child field header names for field %q in type %q: %w", field.Name, fieldType.String(), err)
241241
}
@@ -305,3 +305,18 @@ func valueToTableMap(val reflect.Value) (map[string]any, error) {
305305

306306
return row, nil
307307
}
308+
309+
// TableHeaders returns the table header names of all
310+
// fields in tSlice. tSlice must be a slice of some type.
311+
func TableHeaders(tSlice any) ([]string, error) {
312+
v := reflect.Indirect(reflect.ValueOf(tSlice))
313+
rawHeaders, err := typeToTableHeaders(v.Type().Elem())
314+
if err != nil {
315+
return nil, xerrors.Errorf("type to table headers: %w", err)
316+
}
317+
out := make([]string, 0, len(rawHeaders))
318+
for _, hdr := range rawHeaders {
319+
out = append(out, strings.Replace(hdr, " ", "_", -1))
320+
}
321+
return out, nil
322+
}

cli/cliui/table_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,28 @@ baz baz1 baz3 Aug 2 15:49:10
327327
})
328328
}
329329

330+
func Test_TableHeaders(t *testing.T) {
331+
t.Parallel()
332+
s := []tableTest1{}
333+
expectedFields := []string{
334+
"name",
335+
"age",
336+
"roles",
337+
"sub_1_name",
338+
"sub_1_age",
339+
"sub_2_name",
340+
"sub_2_age",
341+
"sub_3_inner_name",
342+
"sub_3_inner_age",
343+
"sub_4",
344+
"time",
345+
"time_ptr",
346+
}
347+
headers, err := cliui.TableHeaders(s)
348+
require.NoError(t, err)
349+
require.EqualValues(t, expectedFields, headers)
350+
}
351+
330352
// compareTables normalizes the incoming table lines
331353
func compareTables(t *testing.T, expected, out string) {
332354
t.Helper()

cli/list.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cli
22

33
import (
44
"fmt"
5-
"reflect"
65
"strings"
76
"time"
87

@@ -128,14 +127,10 @@ func list() *cobra.Command {
128127
},
129128
}
130129

131-
v := reflect.Indirect(reflect.ValueOf(displayWorkspaces))
132-
availColumns, err := cliui.TypeToTableHeaders(v.Type().Elem())
130+
availColumns, err := cliui.TableHeaders(displayWorkspaces)
133131
if err != nil {
134132
panic(err)
135133
}
136-
for i, s := range availColumns {
137-
availColumns[i] = strings.Replace(s, " ", "_", -1)
138-
}
139134
columnString := strings.Join(availColumns[:], ", ")
140135

141136
cmd.Flags().BoolVarP(&all, "all", "a", false,

0 commit comments

Comments
 (0)