mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat(cli): add display of open ports in coder show (#16464)
Relates to https://github.com/coder/coder/issues/16418 -- devcontainers will be shown in a similar manner. Without ports (status quo):  With ports: 
This commit is contained in:
+39
-2
@@ -1,8 +1,13 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"sync"
|
||||
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/coder/coder/v2/cli/cliui"
|
||||
"github.com/coder/coder/v2/codersdk"
|
||||
"github.com/coder/serpent"
|
||||
@@ -26,10 +31,42 @@ func (r *RootCmd) show() *serpent.Command {
|
||||
if err != nil {
|
||||
return xerrors.Errorf("get workspace: %w", err)
|
||||
}
|
||||
return cliui.WorkspaceResources(inv.Stdout, workspace.LatestBuild.Resources, cliui.WorkspaceResourcesOptions{
|
||||
|
||||
options := cliui.WorkspaceResourcesOptions{
|
||||
WorkspaceName: workspace.Name,
|
||||
ServerVersion: buildInfo.Version,
|
||||
})
|
||||
}
|
||||
if workspace.LatestBuild.Status == codersdk.WorkspaceStatusRunning {
|
||||
// Get listening ports for each agent.
|
||||
options.ListeningPorts = fetchListeningPorts(inv, client, workspace.LatestBuild.Resources...)
|
||||
}
|
||||
return cliui.WorkspaceResources(inv.Stdout, workspace.LatestBuild.Resources, options)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func fetchListeningPorts(inv *serpent.Invocation, client *codersdk.Client, resources ...codersdk.WorkspaceResource) map[uuid.UUID]codersdk.WorkspaceAgentListeningPortsResponse {
|
||||
ports := make(map[uuid.UUID]codersdk.WorkspaceAgentListeningPortsResponse)
|
||||
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()
|
||||
}()
|
||||
}
|
||||
}
|
||||
wg.Wait()
|
||||
return ports
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user