2022-03-22 13:17:50 -06:00
package cli
import (
2026-01-16 15:45:33 +00:00
"fmt"
2025-02-10 13:25:35 +00:00
"sort"
"sync"
2026-01-16 15:45:33 +00:00
"time"
2025-02-10 13:25:35 +00:00
2026-01-08 15:24:11 +04:00
"github.com/google/uuid"
2022-04-11 18:54:30 -05:00
"golang.org/x/xerrors"
2025-07-08 19:16:00 +03:00
"github.com/coder/coder/v2/agent/agentcontainers"
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/codersdk"
2024-03-15 11:24:38 -05:00
"github.com/coder/serpent"
2022-03-22 13:17:50 -06:00
)
2024-03-17 09:45:26 -05:00
func ( r * RootCmd ) show () * serpent . Command {
2025-07-08 19:16:00 +03:00
var details bool
2024-03-17 09:45:26 -05:00
return & serpent . Command {
2023-03-23 17:42:20 -05:00
Use : "show <workspace>" ,
Short : "Display details of a workspace's resources and agents" ,
2025-07-08 19:16:00 +03:00
Options : serpent . OptionSet {
{
Flag : "details" ,
Description : "Show full error messages and additional details." ,
Default : "false" ,
Value : serpent . BoolOf ( & details ),
},
},
2024-03-15 11:24:38 -05:00
Middleware : serpent . Chain (
serpent . RequireNArgs ( 1 ),
2023-03-23 17:42:20 -05:00
),
2024-03-15 11:24:38 -05:00
Handler : func ( inv * serpent . Invocation ) error {
2025-09-22 17:14:07 +04:00
client , err := r . InitClient ( inv )
if err != nil {
return err
}
2023-03-23 17:42:20 -05:00
buildInfo , err := client . BuildInfo ( inv . Context ())
2022-08-31 16:33:50 +01:00
if err != nil {
return xerrors . Errorf ( "get server version: %w" , err )
}
2026-04-27 12:58:26 +01:00
workspace , err := client . ResolveWorkspace ( inv . Context (), inv . Args [ 0 ])
2022-04-11 18:54:30 -05:00
if err != nil {
return xerrors . Errorf ( "get workspace: %w" , err )
}
2025-02-10 13:25:35 +00:00
options := cliui . WorkspaceResourcesOptions {
2022-04-11 18:54:30 -05:00
WorkspaceName : workspace . Name ,
2022-08-31 16:33:50 +01:00
ServerVersion : buildInfo . Version ,
2025-07-08 19:16:00 +03:00
ShowDetails : details ,
2026-01-16 15:45:33 +00:00
Title : fmt . Sprintf ( "%s/%s (%s since %s) %s:%s" , workspace . OwnerName , workspace . Name , workspace . LatestBuild . Status , time . Since ( workspace . LatestBuild . CreatedAt ). Round ( time . Second ). String (), workspace . TemplateName , workspace . LatestBuild . TemplateVersionName ),
2025-02-10 13:25:35 +00:00
}
if workspace . LatestBuild . Status == codersdk . WorkspaceStatusRunning {
// Get listening ports for each agent.
2025-02-11 09:20:55 +00:00
ports , devcontainers := fetchRuntimeResources ( inv , client , workspace . LatestBuild . Resources ... )
options . ListeningPorts = ports
options . Devcontainers = devcontainers
2025-02-10 13:25:35 +00:00
}
return cliui . WorkspaceResources ( inv . Stdout , workspace . LatestBuild . Resources , options )
2022-03-22 13:17:50 -06:00
},
}
}
2025-02-10 13:25:35 +00:00
2025-02-11 09:20:55 +00:00
func fetchRuntimeResources ( inv * serpent . Invocation , client * codersdk . Client , resources ... codersdk . WorkspaceResource ) ( map [ uuid . UUID ] codersdk . WorkspaceAgentListeningPortsResponse , map [ uuid . UUID ] codersdk . WorkspaceAgentListContainersResponse ) {
2025-02-10 13:25:35 +00:00
ports := make ( map [ uuid . UUID ] codersdk . WorkspaceAgentListeningPortsResponse )
2025-02-11 09:20:55 +00:00
devcontainers := make ( map [ uuid . UUID ] codersdk . WorkspaceAgentListContainersResponse )
2025-02-10 13:25:35 +00:00
var wg sync . WaitGroup
var mu sync . Mutex
for _ , res := range resources {
for _ , agent := range res . Agents {
wg . Add ( 1 )
go func () {
defer wg . Done ()
lp , err := client . WorkspaceAgentListeningPorts ( inv . Context (), agent . ID )
if err != nil {
cliui . Warnf ( inv . Stderr , "Failed to get listening ports for agent %s: %v" , agent . Name , err )
}
sort . Slice ( lp . Ports , func ( i , j int ) bool {
return lp . Ports [ i ]. Port < lp . Ports [ j ]. Port
})
mu . Lock ()
ports [ agent . ID ] = lp
mu . Unlock ()
}()
2025-07-08 19:16:00 +03:00
if agent . ParentID . Valid {
continue
}
2025-02-11 09:20:55 +00:00
wg . Add ( 1 )
go func () {
defer wg . Done ()
dc , err := client . WorkspaceAgentListContainers ( inv . Context (), agent . ID , map [ string ] string {
// Labels set by VSCode Remote Containers and @devcontainers/cli.
2025-07-08 19:16:00 +03:00
agentcontainers . DevcontainerConfigFileLabel : "" ,
agentcontainers . DevcontainerLocalFolderLabel : "" ,
2025-02-11 09:20:55 +00:00
})
if err != nil {
cliui . Warnf ( inv . Stderr , "Failed to get devcontainers for agent %s: %v" , agent . Name , err )
}
mu . Lock ()
devcontainers [ agent . ID ] = dc
mu . Unlock ()
}()
2025-02-10 13:25:35 +00:00
}
}
wg . Wait ()
2025-02-11 09:20:55 +00:00
return ports , devcontainers
2025-02-10 13:25:35 +00:00
}