Skip to content

Commit 652fb90

Browse files
committed
Ask before starting workspace
Closes #309.
1 parent 13fbd90 commit 652fb90

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

src/remote.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,29 @@ export interface RemoteDetails extends vscode.Disposable {
2727

2828
export class Remote {
2929
public constructor(
30+
// We use the proposed API to get access to useCustom in dialogs.
3031
private readonly vscodeProposed: typeof vscode,
3132
private readonly storage: Storage,
3233
private readonly commands: Commands,
3334
private readonly mode: vscode.ExtensionMode,
3435
) {}
3536

36-
private async waitForRunning(restClient: Api, workspace: Workspace): Promise<Workspace> {
37+
private async confirmStart(workspaceName: string): Promise<boolean> {
38+
const action = await this.vscodeProposed.window.showInformationMessage(
39+
`Unable to connect to the workspace ${workspaceName} because it is not running. Start the workspace?`,
40+
{
41+
useCustom: true,
42+
modal: true,
43+
},
44+
"Start",
45+
)
46+
return action === "Start"
47+
}
48+
49+
/**
50+
* Try to get the workspace running. Return undefined if the user canceled.
51+
*/
52+
private async maybeWaitForRunning(restClient: Api, workspace: Workspace): Promise<Workspace | undefined> {
3753
// Maybe already running?
3854
if (workspace.latest_build.status === "running") {
3955
return workspace
@@ -83,18 +99,24 @@ export class Remote {
8399
workspace = await waitForBuild(restClient, writeEmitter, workspace)
84100
break
85101
case "stopped":
102+
if (!(await this.confirmStart(workspaceName))) {
103+
return undefined
104+
}
86105
this.storage.writeToCoderOutputChannel(`Starting ${workspaceName}...`)
87106
workspace = await startWorkspace(restClient, workspace)
88107
break
89108
case "failed":
90109
// On a first attempt, we will try starting a failed workspace
91110
// (for example canceling a start seems to cause this state).
92111
if (attempts === 1) {
112+
if (!(await this.confirmStart(workspaceName))) {
113+
return undefined
114+
}
93115
this.storage.writeToCoderOutputChannel(`Starting ${workspaceName}...`)
94116
workspace = await startWorkspace(restClient, workspace)
95117
break
96118
}
97-
// Otherwise fall through and error.
119+
// Otherwise fall through and error.
98120
case "canceled":
99121
case "canceling":
100122
case "deleted":
@@ -255,8 +277,13 @@ export class Remote {
255277
const action = await WorkspaceAction.init(this.vscodeProposed, workspaceRestClient, this.storage)
256278

257279
// If the workspace is not in a running state, try to get it running.
258-
workspace = await this.waitForRunning(workspaceRestClient, workspace)
259-
this.commands.workspace = workspace
280+
const updatedWorkspace = await this.maybeWaitForRunning(workspaceRestClient, workspace)
281+
if (!updatedWorkspace) {
282+
// User declined to start the workspace.
283+
await this.closeRemote()
284+
return
285+
}
286+
this.commands.workspace = workspace = updatedWorkspace
260287

261288
// Pick an agent.
262289
this.storage.writeToCoderOutputChannel(`Finding agent for ${workspaceName}...`)

0 commit comments

Comments
 (0)