| 1102 | } |
| 1103 | |
| 1104 | func 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 |