Skip to content

Commit 1ad2a4c

Browse files
committed
fixup! feat: add JSON output format to many CLI commands
1 parent e33bde8 commit 1ad2a4c

File tree

3 files changed

+54
-60
lines changed

3 files changed

+54
-60
lines changed

cli/cliui/table_test.go

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (s stringWrapper) String() string {
2424
}
2525

2626
type tableTest1 struct {
27-
Name string `table:"name"`
27+
Name string `table:"name,default_sort"`
2828
NotIncluded string // no table tag
2929
Age int `table:"age"`
3030
Roles []string `table:"roles"`
@@ -39,21 +39,45 @@ type tableTest1 struct {
3939
}
4040

4141
type tableTest2 struct {
42-
Name stringWrapper `table:"name"`
42+
Name stringWrapper `table:"name,default_sort"`
4343
Age int `table:"age"`
4444
NotIncluded string `table:"-"`
4545
}
4646

4747
type tableTest3 struct {
4848
NotIncluded string // no table tag
49-
Sub tableTest2 `table:"inner,recursive"`
49+
Sub tableTest2 `table:"inner,recursive,default_sort"`
5050
}
5151

5252
func Test_DisplayTable(t *testing.T) {
5353
t.Parallel()
5454

5555
someTime := time.Date(2022, 8, 2, 15, 49, 10, 0, time.UTC)
56+
57+
// Not sorted by name or age to test sorting.
5658
in := []tableTest1{
59+
{
60+
Name: "bar",
61+
Age: 20,
62+
Roles: []string{"a"},
63+
Sub1: tableTest2{
64+
Name: stringWrapper{str: "bar1"},
65+
Age: 21,
66+
},
67+
Sub2: nil,
68+
Sub3: tableTest3{
69+
Sub: tableTest2{
70+
Name: stringWrapper{str: "bar3"},
71+
Age: 23,
72+
},
73+
},
74+
Sub4: tableTest2{
75+
Name: stringWrapper{str: "bar4"},
76+
Age: 24,
77+
},
78+
Time: someTime,
79+
TimePtr: nil,
80+
},
5781
{
5882
Name: "foo",
5983
Age: 10,
@@ -79,28 +103,6 @@ func Test_DisplayTable(t *testing.T) {
79103
Time: someTime,
80104
TimePtr: &someTime,
81105
},
82-
{
83-
Name: "bar",
84-
Age: 20,
85-
Roles: []string{"a"},
86-
Sub1: tableTest2{
87-
Name: stringWrapper{str: "bar1"},
88-
Age: 21,
89-
},
90-
Sub2: nil,
91-
Sub3: tableTest3{
92-
Sub: tableTest2{
93-
Name: stringWrapper{str: "bar3"},
94-
Age: 23,
95-
},
96-
},
97-
Sub4: tableTest2{
98-
Name: stringWrapper{str: "bar4"},
99-
Age: 24,
100-
},
101-
Time: someTime,
102-
TimePtr: nil,
103-
},
104106
{
105107
Name: "baz",
106108
Age: 30,
@@ -132,9 +134,9 @@ func Test_DisplayTable(t *testing.T) {
132134

133135
expected := `
134136
NAME AGE ROLES SUB 1 NAME SUB 1 AGE SUB 2 NAME SUB 2 AGE SUB 3 INNER NAME SUB 3 INNER AGE SUB 4 TIME TIME PTR
135-
foo 10 [a b c] foo1 11 foo2 12 foo3 13 {foo4 14 } 2022-08-02T15:49:10Z 2022-08-02T15:49:10Z
136137
bar 20 [a] bar1 21 <nil> <nil> bar3 23 {bar4 24 } 2022-08-02T15:49:10Z <nil>
137138
baz 30 [] baz1 31 <nil> <nil> baz3 33 {baz4 34 } 2022-08-02T15:49:10Z <nil>
139+
foo 10 [a b c] foo1 11 foo2 12 foo3 13 {foo4 14 } 2022-08-02T15:49:10Z 2022-08-02T15:49:10Z
138140
`
139141

140142
// Test with non-pointer values.
@@ -154,17 +156,17 @@ baz 30 [] baz1 31 <nil> <nil> baz3
154156
compareTables(t, expected, out)
155157
})
156158

157-
t.Run("Sort", func(t *testing.T) {
159+
t.Run("CustomSort", func(t *testing.T) {
158160
t.Parallel()
159161

160162
expected := `
161163
NAME AGE ROLES SUB 1 NAME SUB 1 AGE SUB 2 NAME SUB 2 AGE SUB 3 INNER NAME SUB 3 INNER AGE SUB 4 TIME TIME PTR
164+
foo 10 [a b c] foo1 11 foo2 12 foo3 13 {foo4 14 } 2022-08-02T15:49:10Z 2022-08-02T15:49:10Z
162165
bar 20 [a] bar1 21 <nil> <nil> bar3 23 {bar4 24 } 2022-08-02T15:49:10Z <nil>
163166
baz 30 [] baz1 31 <nil> <nil> baz3 33 {baz4 34 } 2022-08-02T15:49:10Z <nil>
164-
foo 10 [a b c] foo1 11 foo2 12 foo3 13 {foo4 14 } 2022-08-02T15:49:10Z 2022-08-02T15:49:10Z
165167
`
166168

167-
out, err := cliui.DisplayTable(in, "name", nil)
169+
out, err := cliui.DisplayTable(in, "age", nil)
168170
log.Println("rendered table:\n" + out)
169171
require.NoError(t, err)
170172
compareTables(t, expected, out)
@@ -175,9 +177,9 @@ foo 10 [a b c] foo1 11 foo2 12 foo3
175177

176178
expected := `
177179
NAME SUB 1 NAME SUB 3 INNER NAME TIME
178-
foo foo1 foo3 2022-08-02T15:49:10Z
179180
bar bar1 bar3 2022-08-02T15:49:10Z
180181
baz baz1 baz3 2022-08-02T15:49:10Z
182+
foo foo1 foo3 2022-08-02T15:49:10Z
181183
`
182184

183185
out, err := cliui.DisplayTable(in, "", []string{"name", "sub_1_name", "sub_3 inner name", "time"})
@@ -327,28 +329,6 @@ baz baz1 baz3 2022-08-02T15:49:10Z
327329
})
328330
}
329331

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-
352332
// compareTables normalizes the incoming table lines
353333
func compareTables(t *testing.T, expected, out string) {
354334
t.Helper()

enterprise/cli/features.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ func featuresList() *cobra.Command {
5555
return err
5656
}
5757

58+
// This uses custom formatting as the JSON output outputs an object
59+
// as opposed to a list from the table output.
5860
out := ""
5961
switch outputFormat {
6062
case "table", "":
@@ -88,7 +90,7 @@ func featuresList() *cobra.Command {
8890
}
8991

9092
type featureRow struct {
91-
Name codersdk.FeatureName `table:"name"`
93+
Name codersdk.FeatureName `table:"name,default_sort"`
9294
Entitlement string `table:"entitlement"`
9395
Enabled bool `table:"enabled"`
9496
Limit *int64 `table:"limit"`

enterprise/cli/grouplist.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ import (
1414
)
1515

1616
func groupList() *cobra.Command {
17+
formatter := cliui.NewOutputFormatter(
18+
cliui.TableFormat([]groupTableRow{}, nil),
19+
cliui.JSONFormat(),
20+
)
21+
1722
cmd := &cobra.Command{
1823
Use: "list",
1924
Short: "List user groups",
@@ -44,7 +49,8 @@ func groupList() *cobra.Command {
4449
return nil
4550
}
4651

47-
out, err := displayGroups(groups...)
52+
rows := groupsToRows(groups...)
53+
out, err := formatter.Format(cmd.Context(), rows)
4854
if err != nil {
4955
return xerrors.Errorf("display groups: %w", err)
5056
}
@@ -53,17 +59,23 @@ func groupList() *cobra.Command {
5359
return nil
5460
},
5561
}
62+
63+
formatter.AttachFlags(cmd)
5664
return cmd
5765
}
5866

5967
type groupTableRow struct {
60-
Name string `table:"name"`
61-
OrganizationID uuid.UUID `table:"organization_id"`
62-
Members []string `table:"members"`
63-
AvatarURL string `table:"avatar_url"`
68+
// For json output:
69+
Group codersdk.Group `table:"-"`
70+
71+
// For table output:
72+
Name string `json:"-" table:"name,default_sort"`
73+
OrganizationID uuid.UUID `json:"-" table:"organization_id"`
74+
Members []string `json:"-" table:"members"`
75+
AvatarURL string `json:"-" table:"avatar_url"`
6476
}
6577

66-
func displayGroups(groups ...codersdk.Group) (string, error) {
78+
func groupsToRows(groups ...codersdk.Group) []groupTableRow {
6779
rows := make([]groupTableRow, 0, len(groups))
6880
for _, group := range groups {
6981
members := make([]string, 0, len(group.Members))
@@ -78,5 +90,5 @@ func displayGroups(groups ...codersdk.Group) (string, error) {
7890
})
7991
}
8092

81-
return cliui.DisplayTable(rows, "name", nil)
93+
return rows
8294
}

0 commit comments

Comments
 (0)