Skip to content

Commit 4b1da9b

Browse files
authored
feat(cli): preserve table column order (coder#16843)
Fixes: coder#16055
1 parent 075e5f4 commit 4b1da9b

11 files changed

+42
-16
lines changed

cli/cliui/table.go

+29-3
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,33 @@ func Table() table.Writer {
3131
// e.g. `[]any{someRow, TableSeparator, someRow}`
3232
type TableSeparator struct{}
3333

34-
// filterTableColumns returns configurations to hide columns
34+
// filterHeaders filters the headers to only include the columns
35+
// that are provided in the array. If the array is empty, all
36+
// headers are included.
37+
func filterHeaders(header table.Row, columns []string) table.Row {
38+
if len(columns) == 0 {
39+
return header
40+
}
41+
42+
filteredHeaders := make(table.Row, len(columns))
43+
for i, column := range columns {
44+
column = strings.ReplaceAll(column, "_", " ")
45+
46+
for _, headerTextRaw := range header {
47+
headerText, _ := headerTextRaw.(string)
48+
if strings.EqualFold(column, headerText) {
49+
filteredHeaders[i] = headerText
50+
break
51+
}
52+
}
53+
}
54+
return filteredHeaders
55+
}
56+
57+
// createColumnConfigs returns configuration to hide columns
3558
// that are not provided in the array. If the array is empty,
3659
// no filtering will occur!
37-
func filterTableColumns(header table.Row, columns []string) []table.ColumnConfig {
60+
func createColumnConfigs(header table.Row, columns []string) []table.ColumnConfig {
3861
if len(columns) == 0 {
3962
return nil
4063
}
@@ -157,10 +180,13 @@ func DisplayTable(out any, sort string, filterColumns []string) (string, error)
157180
func renderTable(out any, sort string, headers table.Row, filterColumns []string) (string, error) {
158181
v := reflect.Indirect(reflect.ValueOf(out))
159182

183+
headers = filterHeaders(headers, filterColumns)
184+
columnConfigs := createColumnConfigs(headers, filterColumns)
185+
160186
// Setup the table formatter.
161187
tw := Table()
162188
tw.AppendHeader(headers)
163-
tw.SetColumnConfigs(filterTableColumns(headers, filterColumns))
189+
tw.SetColumnConfigs(columnConfigs)
164190
if sort != "" {
165191
tw.SortBy([]table.SortBy{{
166192
Name: sort,

cli/provisionerjobs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (r *RootCmd) provisionerJobsList() *serpent.Command {
4141
client = new(codersdk.Client)
4242
orgContext = NewOrganizationContext()
4343
formatter = cliui.NewOutputFormatter(
44-
cliui.TableFormat([]provisionerJobRow{}, []string{"created at", "id", "organization", "status", "type", "queue", "tags"}),
44+
cliui.TableFormat([]provisionerJobRow{}, []string{"created at", "id", "type", "template display name", "status", "queue", "tags"}),
4545
cliui.JSONFormat(),
4646
)
4747
status []string

cli/provisioners.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (r *RootCmd) provisionerList() *serpent.Command {
3636
client = new(codersdk.Client)
3737
orgContext = NewOrganizationContext()
3838
formatter = cliui.NewOutputFormatter(
39-
cliui.TableFormat([]provisionerDaemonRow{}, []string{"name", "organization", "status", "key name", "created at", "last seen at", "version", "tags"}),
39+
cliui.TableFormat([]provisionerDaemonRow{}, []string{"created at", "last seen at", "key name", "name", "version", "status", "tags"}),
4040
cliui.JSONFormat(),
4141
)
4242
limit int64
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
ID CREATED AT STATUS TAGS TYPE ORGANIZATION QUEUE
2-
==========[version job ID]========== ====[timestamp]===== succeeded map[owner: scope:organization] template_version_import Coder
3-
======[workspace build job ID]====== ====[timestamp]===== succeeded map[owner: scope:organization] workspace_build Coder
1+
CREATED AT ID TYPE TEMPLATE DISPLAY NAME STATUS QUEUE TAGS
2+
====[timestamp]===== ==========[version job ID]========== template_version_import succeeded map[owner: scope:organization]
3+
====[timestamp]===== ======[workspace build job ID]====== workspace_build succeeded map[owner: scope:organization]

cli/testdata/coder_provisioner_jobs_list_--help.golden

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ OPTIONS:
1111
-O, --org string, $CODER_ORGANIZATION
1212
Select which organization (uuid or name) to use.
1313

14-
-c, --column [id|created at|started at|completed at|canceled at|error|error code|status|worker id|file id|tags|queue position|queue size|organization id|template version id|workspace build id|type|available workers|template version name|template id|template name|template display name|template icon|workspace id|workspace name|organization|queue] (default: created at,id,organization,status,type,queue,tags)
14+
-c, --column [id|created at|started at|completed at|canceled at|error|error code|status|worker id|file id|tags|queue position|queue size|organization id|template version id|workspace build id|type|available workers|template version name|template id|template name|template display name|template icon|workspace id|workspace name|organization|queue] (default: created at,id,type,template display name,status,queue,tags)
1515
Columns to display in table output.
1616

1717
-l, --limit int, $CODER_PROVISIONER_JOB_LIST_LIMIT (default: 50)
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
CREATED AT LAST SEEN AT NAME VERSION TAGS KEY NAME STATUS ORGANIZATION
2-
====[timestamp]===== ====[timestamp]===== test v0.0.0-devel map[owner: scope:organization] built-in idle Coder
1+
CREATED AT LAST SEEN AT KEY NAME NAME VERSION STATUS TAGS
2+
====[timestamp]===== ====[timestamp]===== built-in test v0.0.0-devel idle map[owner: scope:organization]

cli/testdata/coder_provisioner_list_--help.golden

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ OPTIONS:
1111
-O, --org string, $CODER_ORGANIZATION
1212
Select which organization (uuid or name) to use.
1313

14-
-c, --column [id|organization id|created at|last seen at|name|version|api version|tags|key name|status|current job id|current job status|current job template name|current job template icon|current job template display name|previous job id|previous job status|previous job template name|previous job template icon|previous job template display name|organization] (default: name,organization,status,key name,created at,last seen at,version,tags)
14+
-c, --column [id|organization id|created at|last seen at|name|version|api version|tags|key name|status|current job id|current job status|current job template name|current job template icon|current job template display name|previous job id|previous job status|previous job template name|previous job template icon|previous job template display name|organization] (default: created at,last seen at,key name,name,version,status,tags)
1515
Columns to display in table output.
1616

1717
-l, --limit int, $CODER_PROVISIONER_LIST_LIMIT (default: 50)

docs/reference/cli/provisioner_jobs_list.md

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/cli/provisioner_list.md

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

enterprise/cli/testdata/coder_provisioner_jobs_list_--help.golden

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ OPTIONS:
1111
-O, --org string, $CODER_ORGANIZATION
1212
Select which organization (uuid or name) to use.
1313

14-
-c, --column [id|created at|started at|completed at|canceled at|error|error code|status|worker id|file id|tags|queue position|queue size|organization id|template version id|workspace build id|type|available workers|template version name|template id|template name|template display name|template icon|workspace id|workspace name|organization|queue] (default: created at,id,organization,status,type,queue,tags)
14+
-c, --column [id|created at|started at|completed at|canceled at|error|error code|status|worker id|file id|tags|queue position|queue size|organization id|template version id|workspace build id|type|available workers|template version name|template id|template name|template display name|template icon|workspace id|workspace name|organization|queue] (default: created at,id,type,template display name,status,queue,tags)
1515
Columns to display in table output.
1616

1717
-l, --limit int, $CODER_PROVISIONER_JOB_LIST_LIMIT (default: 50)

enterprise/cli/testdata/coder_provisioner_list_--help.golden

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ OPTIONS:
1111
-O, --org string, $CODER_ORGANIZATION
1212
Select which organization (uuid or name) to use.
1313

14-
-c, --column [id|organization id|created at|last seen at|name|version|api version|tags|key name|status|current job id|current job status|current job template name|current job template icon|current job template display name|previous job id|previous job status|previous job template name|previous job template icon|previous job template display name|organization] (default: name,organization,status,key name,created at,last seen at,version,tags)
14+
-c, --column [id|organization id|created at|last seen at|name|version|api version|tags|key name|status|current job id|current job status|current job template name|current job template icon|current job template display name|previous job id|previous job status|previous job template name|previous job template icon|previous job template display name|organization] (default: created at,last seen at,key name,name,version,status,tags)
1515
Columns to display in table output.
1616

1717
-l, --limit int, $CODER_PROVISIONER_LIST_LIMIT (default: 50)

0 commit comments

Comments
 (0)