Skip to content

Commit 6654938

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

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

src/remote.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,25 @@ 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(`Start ${workspaceName}?`, {
39+
useCustom: true,
40+
modal: true,
41+
}, "Start")
42+
return action === "Start"
43+
}
44+
45+
/**
46+
* Try to get the workspace running. Return undefined if the user canceled.
47+
*/
48+
private async maybeWaitForRunning(restClient: Api, workspace: Workspace): Promise<Workspace | undefined> {
3749
// Maybe already running?
3850
if (workspace.latest_build.status === "running") {
3951
return workspace
@@ -83,18 +95,24 @@ export class Remote {
8395
workspace = await waitForBuild(restClient, writeEmitter, workspace)
8496
break
8597
case "stopped":
98+
if (!(await this.confirmStart(workspaceName))) {
99+
return undefined
100+
}
86101
this.storage.writeToCoderOutputChannel(`Starting ${workspaceName}...`)
87102
workspace = await startWorkspace(restClient, workspace)
88103
break
89104
case "failed":
90105
// On a first attempt, we will try starting a failed workspace
91106
// (for example canceling a start seems to cause this state).
92107
if (attempts === 1) {
108+
if (!(await this.confirmStart(workspaceName))) {
109+
return undefined
110+
}
93111
this.storage.writeToCoderOutputChannel(`Starting ${workspaceName}...`)
94112
workspace = await startWorkspace(restClient, workspace)
95113
break
96114
}
97-
// Otherwise fall through and error.
115+
// Otherwise fall through and error.
98116
case "canceled":
99117
case "canceling":
100118
case "deleted":
@@ -255,8 +273,13 @@ export class Remote {
255273
const action = await WorkspaceAction.init(this.vscodeProposed, workspaceRestClient, this.storage)
256274

257275
// 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
276+
const updatedWorkspace = await this.maybeWaitForRunning(workspaceRestClient, workspace)
277+
if (!updatedWorkspace) {
278+
// User declined to start the workspace.
279+
await this.closeRemote()
280+
return
281+
}
282+
this.commands.workspace = workspace = updatedWorkspace
260283

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

0 commit comments

Comments
 (0)