getWorkspaceAgent finds the specified agent in the workspace
(workspace codersdk.Workspace, agentName string)
| 242 | |
| 243 | // getWorkspaceAgent finds the specified agent in the workspace |
| 244 | func getWorkspaceAgent(workspace codersdk.Workspace, agentName string) (codersdk.WorkspaceAgent, error) { |
| 245 | resources := workspace.LatestBuild.Resources |
| 246 | |
| 247 | var agents []codersdk.WorkspaceAgent |
| 248 | var availableNames []string |
| 249 | |
| 250 | for _, resource := range resources { |
| 251 | for _, agent := range resource.Agents { |
| 252 | availableNames = append(availableNames, agent.Name) |
| 253 | agents = append(agents, agent) |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if len(agents) == 0 { |
| 258 | return codersdk.WorkspaceAgent{}, xerrors.Errorf("workspace %q has no agents", workspace.Name) |
| 259 | } |
| 260 | |
| 261 | if agentName != "" { |
| 262 | for _, agent := range agents { |
| 263 | if agent.Name == agentName || agent.ID.String() == agentName { |
| 264 | return agent, nil |
| 265 | } |
| 266 | } |
| 267 | return codersdk.WorkspaceAgent{}, xerrors.Errorf("agent not found by name %q, available agents: %v", agentName, availableNames) |
| 268 | } |
| 269 | |
| 270 | if len(agents) == 1 { |
| 271 | return agents[0], nil |
| 272 | } |
| 273 | |
| 274 | return codersdk.WorkspaceAgent{}, xerrors.Errorf("multiple agents found, please specify the agent name, available agents: %v", availableNames) |
| 275 | } |
| 276 | |
| 277 | // executeCommandWithTimeout executes a command with timeout support |
| 278 | func executeCommandWithTimeout(ctx context.Context, session *gossh.Session, command string) ([]byte, error) { |
no test coverage detected