99 "github.com/jedib0t/go-pretty/v6/table"
1010
1111 "github.com/coder/coder/coderd/database"
12+
1213 "github.com/coder/coder/codersdk"
1314)
1415
@@ -23,12 +24,12 @@ type WorkspaceResourcesOptions struct {
2324// ┌────────────────────────────────────────────────────────────────────────────┐
2425// │ RESOURCE STATUS ACCESS │
2526// ├────────────────────────────────────────────────────────────────────────────┤
26- // │ google_compute_disk.root persistent │
27+ // │ google_compute_disk.root │
2728// ├────────────────────────────────────────────────────────────────────────────┤
28- // │ google_compute_instance.dev ephemeral │
29+ // │ google_compute_instance.dev │
2930// │ └─ dev (linux, amd64) ⦾ connecting [10s] coder ssh dev.dev │
3031// ├────────────────────────────────────────────────────────────────────────────┤
31- // │ kubernetes_pod.dev ephemeral │
32+ // │ kubernetes_pod.dev │
3233// │ ├─ go (linux, amd64) ⦿ connected coder ssh dev.go │
3334// │ └─ postgres (linux, amd64) ⦾ disconnected [4s] coder ssh dev.postgres │
3435// └────────────────────────────────────────────────────────────────────────────┘
@@ -38,26 +39,16 @@ func WorkspaceResources(writer io.Writer, resources []codersdk.WorkspaceResource
3839 return resources [i ].Type < resources [j ].Type
3940 })
4041
41- // Address on stop indexes whether a resource still exists when in the stopped transition.
42- addressOnStop := map [string ]codersdk.WorkspaceResource {}
43- for _ , resource := range resources {
44- if resource .Transition != codersdk .WorkspaceTransitionStop {
45- continue
46- }
47- addressOnStop [resource .Type + "." + resource .Name ] = resource
48- }
49- // Displayed stores whether a resource has already been shown.
50- // Resources can be stored with numerous states, which we
51- // process prior to display.
52- displayed := map [string ]struct {}{}
53-
5442 tableWriter := table .NewWriter ()
5543 if options .Title != "" {
5644 tableWriter .SetTitle (options .Title )
5745 }
5846 tableWriter .SetStyle (table .StyleLight )
5947 tableWriter .Style ().Options .SeparateColumns = false
60- row := table.Row {"Resource" , "Status" }
48+ row := table.Row {"Resource" }
49+ if ! options .HideAgentState {
50+ row = append (row , "Status" )
51+ }
6152 if ! options .HideAccess {
6253 row = append (row , "Access" )
6354 }
@@ -76,60 +67,52 @@ func WorkspaceResources(writer io.Writer, resources []codersdk.WorkspaceResource
7667 continue
7768 }
7869 resourceAddress := resource .Type + "." + resource .Name
79- if _ , shown := displayed [resourceAddress ]; shown {
80- // The same resource can have multiple transitions.
81- continue
82- }
83- displayed [resourceAddress ] = struct {}{}
8470
8571 // Sort agents by name for consistent output.
8672 sort .Slice (resource .Agents , func (i , j int ) bool {
8773 return resource .Agents [i ].Name < resource .Agents [j ].Name
8874 })
89- _ , existsOnStop := addressOnStop [resourceAddress ]
90- resourceState := "ephemeral"
91- if existsOnStop {
92- resourceState = "persistent"
93- }
75+
9476 // Display a line for the resource.
9577 tableWriter .AppendRow (table.Row {
9678 Styles .Bold .Render (resourceAddress ),
97- Styles . Placeholder . Render ( resourceState ) ,
79+ "" ,
9880 "" ,
9981 })
10082 // Display all agents associated with the resource.
10183 for index , agent := range resource .Agents {
102- sshCommand := "coder ssh " + options .WorkspaceName
103- if totalAgents > 1 {
104- sshCommand += "." + agent .Name
105- }
106- sshCommand = Styles .Code .Render (sshCommand )
107- var agentStatus string
108- if ! options .HideAgentState {
109- switch agent .Status {
110- case codersdk .WorkspaceAgentConnecting :
111- since := database .Now ().Sub (agent .CreatedAt )
112- agentStatus = Styles .Warn .Render ("⦾ connecting" ) + " " +
113- Styles .Placeholder .Render ("[" + strconv .Itoa (int (since .Seconds ()))+ "s]" )
114- case codersdk .WorkspaceAgentDisconnected :
115- since := database .Now ().Sub (* agent .DisconnectedAt )
116- agentStatus = Styles .Error .Render ("⦾ disconnected" ) + " " +
117- Styles .Placeholder .Render ("[" + strconv .Itoa (int (since .Seconds ()))+ "s]" )
118- case codersdk .WorkspaceAgentConnected :
119- agentStatus = Styles .Keyword .Render ("⦿ connected" )
120- }
121- }
122-
12384 pipe := "├"
12485 if index == len (resource .Agents )- 1 {
12586 pipe = "└"
12687 }
12788 row := table.Row {
12889 // These tree from a resource!
12990 fmt .Sprintf ("%s─ %s (%s, %s)" , pipe , agent .Name , agent .OperatingSystem , agent .Architecture ),
130- agentStatus ,
91+ }
92+ if ! options .HideAgentState {
93+ var agentStatus string
94+ if ! options .HideAgentState {
95+ switch agent .Status {
96+ case codersdk .WorkspaceAgentConnecting :
97+ since := database .Now ().Sub (agent .CreatedAt )
98+ agentStatus = Styles .Warn .Render ("⦾ connecting" ) + " " +
99+ Styles .Placeholder .Render ("[" + strconv .Itoa (int (since .Seconds ()))+ "s]" )
100+ case codersdk .WorkspaceAgentDisconnected :
101+ since := database .Now ().Sub (* agent .DisconnectedAt )
102+ agentStatus = Styles .Error .Render ("⦾ disconnected" ) + " " +
103+ Styles .Placeholder .Render ("[" + strconv .Itoa (int (since .Seconds ()))+ "s]" )
104+ case codersdk .WorkspaceAgentConnected :
105+ agentStatus = Styles .Keyword .Render ("⦿ connected" )
106+ }
107+ }
108+ row = append (row , agentStatus )
131109 }
132110 if ! options .HideAccess {
111+ sshCommand := "coder ssh " + options .WorkspaceName
112+ if totalAgents > 1 {
113+ sshCommand += "." + agent .Name
114+ }
115+ sshCommand = Styles .Code .Render (sshCommand )
133116 row = append (row , sshCommand )
134117 }
135118 tableWriter .AppendRow (row )
0 commit comments