@@ -9,6 +9,17 @@ import { Storage } from "./storage"
9
9
import { OpenableTreeItem } from "./workspacesProvider"
10
10
11
11
export class Commands {
12
+ // These will only be populated when actively connected to a workspace and are
13
+ // used in commands. Because commands can be executed by the user, it is not
14
+ // possible to pass in arguments, so we have to store the current workspace
15
+ // and its client somewhere, separately from the current globally logged-in
16
+ // client, since you can connect to workspaces not belonging to whatever you
17
+ // are logged into (for convenience; otherwise the recents menu can be a pain
18
+ // if you use multiple deployments).
19
+ public workspace ?: Workspace
20
+ public workspaceLogPath ?: string
21
+ public workspaceRestClient ?: Api
22
+
12
23
public constructor (
13
24
private readonly vscodeProposed : typeof vscode ,
14
25
private readonly restClient : Api ,
@@ -162,15 +173,14 @@ export class Commands {
162
173
}
163
174
164
175
/**
165
- * View the logs for the currently logged-in deployment .
176
+ * View the logs for the currently connected workspace .
166
177
*/
167
178
public async viewLogs ( ) : Promise < void > {
168
- // TODO: This will need to be refactored for multi-deployment support.
169
- if ( ! this . storage . workspaceLogPath ) {
170
- vscode . window . showInformationMessage ( "No logs available." , this . storage . workspaceLogPath || "<unset>" )
179
+ if ( ! this . workspaceLogPath ) {
180
+ vscode . window . showInformationMessage ( "No logs available." , this . workspaceLogPath || "<unset>" )
171
181
return
172
182
}
173
- const uri = vscode . Uri . file ( this . storage . workspaceLogPath )
183
+ const uri = vscode . Uri . file ( this . workspaceLogPath )
174
184
const doc = await vscode . workspace . openTextDocument ( uri )
175
185
await vscode . window . showTextDocument ( doc )
176
186
}
@@ -212,14 +222,18 @@ export class Commands {
212
222
/**
213
223
* Open a link to the workspace in the Coder dashboard.
214
224
*
215
- * Must only be called if currently logged in.
225
+ * If passing in a workspace, it must belong to the currently logged-in
226
+ * deployment.
227
+ *
228
+ * Otherwise, the currently connected workspace is used (if any).
216
229
*/
217
230
public async navigateToWorkspace ( workspace : OpenableTreeItem ) {
218
231
if ( workspace ) {
219
232
const uri = this . storage . getUrl ( ) + `/@${ workspace . workspaceOwner } /${ workspace . workspaceName } `
220
233
await vscode . commands . executeCommand ( "vscode.open" , uri )
221
- } else if ( this . storage . workspace ) {
222
- const uri = this . storage . getUrl ( ) + `/@${ this . storage . workspace . owner_name } /${ this . storage . workspace . name } `
234
+ } else if ( this . workspace && this . workspaceRestClient ) {
235
+ const baseUrl = this . workspaceRestClient . getAxiosInstance ( ) . defaults . baseURL
236
+ const uri = `${ baseUrl } /@${ this . workspace . owner_name } /${ this . workspace . name } `
223
237
await vscode . commands . executeCommand ( "vscode.open" , uri )
224
238
} else {
225
239
vscode . window . showInformationMessage ( "No workspace found." )
@@ -229,15 +243,18 @@ export class Commands {
229
243
/**
230
244
* Open a link to the workspace settings in the Coder dashboard.
231
245
*
232
- * Must only be called if currently logged in.
246
+ * If passing in a workspace, it must belong to the currently logged-in
247
+ * deployment.
248
+ *
249
+ * Otherwise, the currently connected workspace is used (if any).
233
250
*/
234
251
public async navigateToWorkspaceSettings ( workspace : OpenableTreeItem ) {
235
252
if ( workspace ) {
236
253
const uri = this . storage . getUrl ( ) + `/@${ workspace . workspaceOwner } /${ workspace . workspaceName } /settings`
237
254
await vscode . commands . executeCommand ( "vscode.open" , uri )
238
- } else if ( this . storage . workspace ) {
239
- const uri =
240
- this . storage . getUrl ( ) + ` /@${ this . storage . workspace . owner_name } /${ this . storage . workspace . name } /settings`
255
+ } else if ( this . workspace && this . workspaceRestClient ) {
256
+ const baseUrl = this . workspaceRestClient . getAxiosInstance ( ) . defaults . baseURL
257
+ const uri = ` ${ baseUrl } /@${ this . workspace . owner_name } /${ this . workspace . name } /settings`
241
258
await vscode . commands . executeCommand ( "vscode.open" , uri )
242
259
} else {
243
260
vscode . window . showInformationMessage ( "No workspace found." )
@@ -248,7 +265,8 @@ export class Commands {
248
265
* Open a workspace or agent that is showing in the sidebar.
249
266
*
250
267
* This essentially just builds the host name and passes it to the VS Code
251
- * Remote SSH extension.
268
+ * Remote SSH extension, so it is not necessary to be logged in, although then
269
+ * the sidebar would not have any workspaces in it anyway.
252
270
*/
253
271
public async openFromSidebar ( treeItem : OpenableTreeItem ) {
254
272
if ( treeItem ) {
@@ -263,9 +281,9 @@ export class Commands {
263
281
}
264
282
265
283
/**
266
- * Open a workspace from the currently logged-in deployment.
284
+ * Open a workspace belonging to the currently logged-in deployment.
267
285
*
268
- * This must only be called if the REST client is logged in .
286
+ * This must only be called if logged into a deployment .
269
287
*/
270
288
public async open ( ...args : unknown [ ] ) : Promise < void > {
271
289
let workspaceOwner : string
@@ -393,20 +411,20 @@ export class Commands {
393
411
* this is a no-op.
394
412
*/
395
413
public async updateWorkspace ( ) : Promise < void > {
396
- if ( ! this . storage . workspace || ! this . storage . restClient ) {
414
+ if ( ! this . workspace || ! this . workspaceRestClient ) {
397
415
return
398
416
}
399
417
const action = await this . vscodeProposed . window . showInformationMessage (
400
418
"Update Workspace" ,
401
419
{
402
420
useCustom : true ,
403
421
modal : true ,
404
- detail : `${ this . storage . workspace . owner_name } /${ this . storage . workspace . name } will be updated then this window will reload to watch the build logs and reconnect.` ,
422
+ detail : `${ this . workspace . owner_name } /${ this . workspace . name } will be updated then this window will reload to watch the build logs and reconnect.` ,
405
423
} ,
406
424
"Update" ,
407
425
)
408
426
if ( action === "Update" ) {
409
- await this . storage . restClient . updateWorkspaceVersion ( this . storage . workspace )
427
+ await this . workspaceRestClient . updateWorkspaceVersion ( this . workspace )
410
428
}
411
429
}
412
430
}
0 commit comments