()
| 45 | const externalSessionTokenPlaceholder = "$SESSION_TOKEN" |
| 46 | |
| 47 | func (r *RootCmd) openVSCode() *serpent.Command { |
| 48 | var ( |
| 49 | generateToken bool |
| 50 | testOpenError bool |
| 51 | ) |
| 52 | |
| 53 | cmd := &serpent.Command{ |
| 54 | Annotations: workspaceCommand, |
| 55 | Use: "vscode <workspace> [<directory in workspace>]", |
| 56 | Short: fmt.Sprintf("Open a workspace in %s", vscodeDesktopName), |
| 57 | Middleware: serpent.Chain( |
| 58 | serpent.RequireRangeArgs(1, 2), |
| 59 | ), |
| 60 | Handler: func(inv *serpent.Invocation) error { |
| 61 | client, err := r.InitClient(inv) |
| 62 | if err != nil { |
| 63 | return err |
| 64 | } |
| 65 | ctx, cancel := context.WithCancel(inv.Context()) |
| 66 | defer cancel() |
| 67 | appearanceConfig := initAppearance(ctx, client) |
| 68 | |
| 69 | // Check if we're inside a workspace, and especially inside _this_ |
| 70 | // workspace so we can perform path resolution/expansion. Generally, |
| 71 | // we know that if we're inside a workspace, `open` can't be used. |
| 72 | insideAWorkspace := inv.Environ.Get("CODER") == "true" |
| 73 | inWorkspaceName := inv.Environ.Get("CODER_WORKSPACE_NAME") + "." + inv.Environ.Get("CODER_WORKSPACE_AGENT_NAME") |
| 74 | |
| 75 | // We need a started workspace to figure out e.g. expanded directory. |
| 76 | // Pehraps the vscode-coder extension could handle this by accepting |
| 77 | // default_directory=true, then probing the agent. Then we wouldn't |
| 78 | // need to wait for the agent to start. |
| 79 | workspaceQuery := inv.Args[0] |
| 80 | autostart := true |
| 81 | workspace, workspaceAgent, otherWorkspaceAgents, err := GetWorkspaceAndAgent(ctx, inv, client, autostart, workspaceQuery) |
| 82 | if err != nil { |
| 83 | return xerrors.Errorf("get workspace and agent: %w", err) |
| 84 | } |
| 85 | |
| 86 | workspaceName := workspace.Name + "." + workspaceAgent.Name |
| 87 | insideThisWorkspace := insideAWorkspace && inWorkspaceName == workspaceName |
| 88 | |
| 89 | // To properly work with devcontainers, VS Code has to connect to |
| 90 | // parent workspace agent. It will then proceed to enter the |
| 91 | // container given the correct parameters. There is inherently no |
| 92 | // dependency on the devcontainer agent in this scenario, but |
| 93 | // relying on it simplifies the logic and ensures the devcontainer |
| 94 | // is ready. To eliminate the dependency we would need to know that |
| 95 | // a sub-agent that hasn't been created yet may be a devcontainer, |
| 96 | // and thus will be created at a later time as well as expose the |
| 97 | // container folder on the API response. |
| 98 | var parentWorkspaceAgent codersdk.WorkspaceAgent |
| 99 | var devcontainer codersdk.WorkspaceAgentDevcontainer |
| 100 | if workspaceAgent.ParentID.Valid { |
| 101 | // This is likely a devcontainer agent, so we need to find the |
| 102 | // parent workspace agent as well as the devcontainer. |
| 103 | for _, otherAgent := range otherWorkspaceAgents { |
| 104 | if otherAgent.ID == workspaceAgent.ParentID.UUID { |
no test coverage detected