Skip to content

Commit 71a0e75

Browse files
authored
feat: open an agent-defined directory by default (#57)
1 parent 2a28af4 commit 71a0e75

File tree

4 files changed

+49
-27
lines changed

4 files changed

+49
-27
lines changed

src/commands.ts

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import axios from "axios"
22
import { getUser, getWorkspaces, updateWorkspaceVersion } from "coder/site/src/api/api"
3-
import { Workspace } from "coder/site/src/api/typesGenerated"
3+
import { Workspace, WorkspaceAgent } from "coder/site/src/api/typesGenerated"
44
import * as vscode from "vscode"
55
import { Remote } from "./remote"
66
import { Storage } from "./storage"
@@ -104,6 +104,7 @@ export class Commands {
104104
public async open(...args: string[]): Promise<void> {
105105
let workspaceOwner: string
106106
let workspaceName: string
107+
let folderPath: string | undefined
107108

108109
if (args.length === 0) {
109110
const quickPick = vscode.window.createQuickPick()
@@ -156,9 +157,20 @@ export class Commands {
156157
}
157158
workspaceOwner = workspace.owner_name
158159
workspaceName = workspace.name
160+
161+
// TODO: multiple agent support
162+
const agents = workspace.latest_build.resources.reduce((acc, resource) => {
163+
return acc.concat(resource.agents || [])
164+
}, [] as WorkspaceAgent[])
165+
166+
if (agents.length === 1) {
167+
folderPath = agents[0].expanded_directory
168+
}
159169
} else {
160170
workspaceOwner = args[0]
161171
workspaceName = args[1]
172+
// workspaceAgent is reserved for args[2], but multiple agents aren't supported yet.
173+
folderPath = args[3]
162174
}
163175

164176
// A workspace can have multiple agents, but that's handled
@@ -171,39 +183,46 @@ export class Commands {
171183
newWindow = false
172184
}
173185

174-
const output: {
175-
workspaces: { folderUri: vscode.Uri; remoteAuthority: string }[]
176-
} = await vscode.commands.executeCommand("_workbench.getRecentlyOpened")
177-
const opened = output.workspaces.filter(
178-
// Filter out `/` since that's added below.
179-
(opened) => opened.folderUri?.authority === remoteAuthority,
180-
)
181-
if (opened.length > 0) {
182-
let selected: (typeof opened)[0]
186+
// If a folder isn't specified, we can try to open a recently opened folder.
187+
if (!folderPath) {
188+
const output: {
189+
workspaces: { folderUri: vscode.Uri; remoteAuthority: string }[]
190+
} = await vscode.commands.executeCommand("_workbench.getRecentlyOpened")
191+
const opened = output.workspaces.filter(
192+
// Filter out `/` since that's added below.
193+
(opened) => opened.folderUri?.authority === remoteAuthority,
194+
)
195+
if (opened.length > 0) {
196+
let selected: (typeof opened)[0]
183197

184-
if (opened.length > 1) {
185-
const items: vscode.QuickPickItem[] = opened.map((folder): vscode.QuickPickItem => {
186-
return {
187-
label: folder.folderUri.path,
198+
if (opened.length > 1) {
199+
const items: vscode.QuickPickItem[] = opened.map((folder): vscode.QuickPickItem => {
200+
return {
201+
label: folder.folderUri.path,
202+
}
203+
})
204+
const item = await vscode.window.showQuickPick(items, {
205+
title: "Select a recently opened folder",
206+
})
207+
if (!item) {
208+
return
188209
}
189-
})
190-
const item = await vscode.window.showQuickPick(items, {
191-
title: "Select a recently opened folder",
192-
})
193-
if (!item) {
194-
return
210+
selected = opened[items.indexOf(item)]
211+
} else {
212+
selected = opened[0]
195213
}
196-
selected = opened[items.indexOf(item)]
197-
} else {
198-
selected = opened[0]
214+
215+
folderPath = selected.folderUri.path
199216
}
217+
}
200218

219+
if (folderPath) {
201220
await vscode.commands.executeCommand(
202221
"vscode.openFolder",
203222
vscode.Uri.from({
204223
scheme: "vscode-remote",
205224
authority: remoteAuthority,
206-
path: selected.folderUri.path,
225+
path: folderPath,
207226
}),
208227
// Open this in a new window!
209228
newWindow,

src/extension.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
3030
const owner = params.get("owner")
3131
const workspace = params.get("workspace")
3232
const agent = params.get("agent")
33+
const folder = params.get("folder")
3334
if (!owner) {
3435
throw new Error("owner must be specified as a query parameter")
3536
}
@@ -45,7 +46,7 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
4546
if (token) {
4647
await storage.setSessionToken(token)
4748
}
48-
vscode.commands.executeCommand("coder.open", owner, workspace, agent)
49+
vscode.commands.executeCommand("coder.open", owner, workspace, agent, folder)
4950
}
5051
},
5152
})

src/remote.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import axios from "axios"
22
import {
33
getBuildInfo,
4+
getTemplate,
45
getWorkspace,
56
getWorkspaceBuildLogs,
67
getWorkspaceByOwnerAndName,
@@ -134,9 +135,10 @@ export class Remote {
134135
buildComplete = r
135136
}),
136137
)
138+
const template = await getTemplate(this.storage.workspace.template_id)
137139
this.storage.workspace = {
138140
...this.storage.workspace,
139-
latest_build: await startWorkspace(this.storage.workspace.id),
141+
latest_build: await startWorkspace(this.storage.workspace.id, template.active_version_id),
140142
}
141143
}
142144

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1418,7 +1418,7 @@ co@3.1.0:
14181418

14191419
"coder@https://github.com/coder/coder":
14201420
version "0.0.0"
1421-
resolved "https://github.com/coder/coder#8a5760a2fea074fda65d199620aaa9ed447e7c8d"
1421+
resolved "https://github.com/coder/coder#7a1731b6205d9c68f6308ee362ff2d62124b6950"
14221422

14231423
collapse-white-space@^1.0.2:
14241424
version "1.0.6"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy