(ctx context.Context, client *codersdk.Client, log slog.Logger, agentID uuid.UUID)
| 535 | } |
| 536 | |
| 537 | func AgentInfo(ctx context.Context, client *codersdk.Client, log slog.Logger, agentID uuid.UUID) Agent { |
| 538 | var ( |
| 539 | a Agent |
| 540 | eg errgroup.Group |
| 541 | ) |
| 542 | |
| 543 | if agentID == uuid.Nil { |
| 544 | log.Error(ctx, "no agent id specified") |
| 545 | return a |
| 546 | } |
| 547 | |
| 548 | eg.Go(func() error { |
| 549 | agt, err := client.WorkspaceAgent(ctx, agentID) |
| 550 | if err != nil { |
| 551 | return xerrors.Errorf("fetch workspace agent: %w", err) |
| 552 | } |
| 553 | sanitizeEnv(agt.EnvironmentVariables) |
| 554 | a.Agent = &agt |
| 555 | return nil |
| 556 | }) |
| 557 | |
| 558 | eg.Go(func() error { |
| 559 | agentLogCh, closer, err := client.WorkspaceAgentLogsAfter(ctx, agentID, 0, false) |
| 560 | if err != nil { |
| 561 | return xerrors.Errorf("fetch agent startup logs: %w", err) |
| 562 | } |
| 563 | defer closer.Close() |
| 564 | var logs []codersdk.WorkspaceAgentLog |
| 565 | for logChunk := range agentLogCh { |
| 566 | logs = append(logs, logChunk...) |
| 567 | } |
| 568 | a.StartupLogs = logs |
| 569 | return nil |
| 570 | }) |
| 571 | |
| 572 | // to simplify control flow, fetching information directly from |
| 573 | // the agent is handled in a separate function |
| 574 | closer := connectedAgentInfo(ctx, client, log, agentID, &eg, &a) |
| 575 | defer closer() |
| 576 | |
| 577 | if err := eg.Wait(); err != nil { |
| 578 | log.Error(ctx, "fetch agent information", slog.Error(err)) |
| 579 | } |
| 580 | |
| 581 | return a |
| 582 | } |
| 583 | |
| 584 | func connectedAgentInfo(ctx context.Context, client *codersdk.Client, log slog.Logger, agentID uuid.UUID, eg *errgroup.Group, a *Agent) (closer func()) { |
| 585 | conn, err := workspacesdk.New(client). |
no test coverage detected