@@ -2,7 +2,6 @@ package cli
2
2
3
3
import (
4
4
"fmt"
5
- "strings"
6
5
"time"
7
6
8
7
"github.com/google/uuid"
@@ -14,14 +13,21 @@ import (
14
13
"github.com/coder/coder/codersdk"
15
14
)
16
15
16
+ // workspaceListRow is the type provided to the OutputFormatter. This is a bit
17
+ // dodgy but it's the only way to do complex display code for one format vs. the
18
+ // other.
17
19
type workspaceListRow struct {
18
- Workspace string `table:"workspace"`
19
- Template string `table:"template"`
20
- Status string `table:"status"`
21
- LastBuilt string `table:"last built"`
22
- Outdated bool `table:"outdated"`
23
- StartsAt string `table:"starts at"`
24
- StopsAfter string `table:"stops after"`
20
+ // For JSON format:
21
+ codersdk.Workspace `table:"-"`
22
+
23
+ // For table format:
24
+ WorkspaceName string `json:"-" table:"workspace,default_sort"`
25
+ Template string `json:"-" table:"template"`
26
+ Status string `json:"-" table:"status"`
27
+ LastBuilt string `json:"-" table:"last built"`
28
+ Outdated bool `json:"-" table:"outdated"`
29
+ StartsAt string `json:"-" table:"starts at"`
30
+ StopsAfter string `json:"-" table:"stops after"`
25
31
}
26
32
27
33
func workspaceListRowFromWorkspace (now time.Time , usersByID map [uuid.UUID ]codersdk.User , workspace codersdk.Workspace ) workspaceListRow {
@@ -47,24 +53,27 @@ func workspaceListRowFromWorkspace(now time.Time, usersByID map[uuid.UUID]coders
47
53
48
54
user := usersByID [workspace .OwnerID ]
49
55
return workspaceListRow {
50
- Workspace : user .Username + "/" + workspace .Name ,
51
- Template : workspace .TemplateName ,
52
- Status : status ,
53
- LastBuilt : durationDisplay (lastBuilt ),
54
- Outdated : workspace .Outdated ,
55
- StartsAt : autostartDisplay ,
56
- StopsAfter : autostopDisplay ,
56
+ Workspace : workspace ,
57
+ WorkspaceName : user .Username + "/" + workspace .Name ,
58
+ Template : workspace .TemplateName ,
59
+ Status : status ,
60
+ LastBuilt : durationDisplay (lastBuilt ),
61
+ Outdated : workspace .Outdated ,
62
+ StartsAt : autostartDisplay ,
63
+ StopsAfter : autostopDisplay ,
57
64
}
58
65
}
59
66
60
67
func list () * cobra.Command {
61
68
var (
62
69
all bool
63
- columns []string
64
70
defaultQuery = "owner:me"
65
71
searchQuery string
66
- me bool
67
72
displayWorkspaces []workspaceListRow
73
+ formatter = cliui .NewOutputFormatter (
74
+ cliui .TableFormat ([]workspaceListRow {}, nil ),
75
+ cliui .JSONFormat (),
76
+ )
68
77
)
69
78
cmd := & cobra.Command {
70
79
Annotations : workspaceCommand ,
@@ -85,14 +94,6 @@ func list() *cobra.Command {
85
94
filter .FilterQuery = ""
86
95
}
87
96
88
- if me {
89
- myUser , err := client .User (cmd .Context (), codersdk .Me )
90
- if err != nil {
91
- return err
92
- }
93
- filter .Owner = myUser .Username
94
- }
95
-
96
97
res , err := client .Workspaces (cmd .Context (), filter )
97
98
if err != nil {
98
99
return err
@@ -121,7 +122,7 @@ func list() *cobra.Command {
121
122
displayWorkspaces [i ] = workspaceListRowFromWorkspace (now , usersByID , workspace )
122
123
}
123
124
124
- out , err := cliui . DisplayTable ( displayWorkspaces , "workspace" , columns )
125
+ out , err := formatter . Format ( cmd . Context (), displayWorkspaces )
125
126
if err != nil {
126
127
return err
127
128
}
@@ -131,14 +132,10 @@ func list() *cobra.Command {
131
132
},
132
133
}
133
134
134
- // TODO: fix this
135
- availColumns := []string {"name" }
136
- columnString := strings .Join (availColumns [:], ", " )
137
-
138
135
cmd .Flags ().BoolVarP (& all , "all" , "a" , false ,
139
136
"Specifies whether all workspaces will be listed or not." )
140
- cmd .Flags ().StringArrayVarP (& columns , "column" , "c" , nil ,
141
- fmt .Sprintf ("Specify a column to filter in the table. Available columns are: %v" , columnString ))
142
137
cmd .Flags ().StringVar (& searchQuery , "search" , defaultQuery , "Search for a workspace with a query." )
138
+
139
+ formatter .AttachFlags (cmd )
143
140
return cmd
144
141
}
0 commit comments