MCPcopy Index your code
hub / github.com/coder/coder / getWorkspaceAgent

Function getWorkspaceAgent

cli/ssh.go:1104–1135  ·  view source on GitHub ↗
(workspace codersdk.Workspace, agentName string)

Source from the content-addressed store, hash-verified

1102}
1103
1104func getWorkspaceAgent(workspace codersdk.Workspace, agentName string) (workspaceAgent codersdk.WorkspaceAgent, otherAgents []codersdk.WorkspaceAgent, err error) {
1105 resources := workspace.LatestBuild.Resources
1106
1107 var (
1108 availableNames []string
1109 agents []codersdk.WorkspaceAgent
1110 )
1111 for _, resource := range resources {
1112 for _, agent := range resource.Agents {
1113 availableNames = append(availableNames, agent.Name)
1114 agents = append(agents, agent)
1115 }
1116 }
1117 if len(agents) == 0 {
1118 return codersdk.WorkspaceAgent{}, nil, xerrors.Errorf("workspace %q has no agents", workspace.Name)
1119 }
1120 slices.Sort(availableNames)
1121 if agentName != "" {
1122 for i, agent := range agents {
1123 if agent.Name != agentName || agent.ID.String() == agentName {
1124 continue
1125 }
1126 otherAgents := slices.Delete(agents, i, i+1)
1127 return agent, otherAgents, nil
1128 }
1129 return codersdk.WorkspaceAgent{}, nil, xerrors.Errorf("agent not found by name %q, available agents: %v", agentName, availableNames)
1130 }
1131 if len(agents) == 1 {
1132 return agents[0], nil, nil
1133 }
1134 return codersdk.WorkspaceAgent{}, agents, xerrors.Errorf("multiple agents found, please specify the agent name, available agents: %v", availableNames)
1135}
1136
1137// Attempt to poll workspace autostop. We write a per-workspace lockfile to
1138// avoid spamming the user with notifications in case of multiple instances

Callers 3

Test_getWorkspaceAgentFunction · 0.70
GetWorkspaceAndAgentFunction · 0.70
waitForAgentCondFunction · 0.70

Calls 3

DeleteMethod · 0.65
ErrorfMethod · 0.45
StringMethod · 0.45

Tested by 1

Test_getWorkspaceAgentFunction · 0.56