@@ -9,6 +9,17 @@ import { Storage } from "./storage"
99import { OpenableTreeItem } from "./workspacesProvider"
1010
1111export 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+
1223 public constructor (
1324 private readonly vscodeProposed : typeof vscode ,
1425 private readonly restClient : Api ,
@@ -162,15 +173,14 @@ export class Commands {
162173 }
163174
164175 /**
165- * View the logs for the currently logged-in deployment .
176+ * View the logs for the currently connected workspace .
166177 */
167178 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>" )
171181 return
172182 }
173- const uri = vscode . Uri . file ( this . storage . workspaceLogPath )
183+ const uri = vscode . Uri . file ( this . workspaceLogPath )
174184 const doc = await vscode . workspace . openTextDocument ( uri )
175185 await vscode . window . showTextDocument ( doc )
176186 }
@@ -212,14 +222,18 @@ export class Commands {
212222 /**
213223 * Open a link to the workspace in the Coder dashboard.
214224 *
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).
216229 */
217230 public async navigateToWorkspace ( workspace : OpenableTreeItem ) {
218231 if ( workspace ) {
219232 const uri = this . storage . getUrl ( ) + `/@${ workspace . workspaceOwner } /${ workspace . workspaceName } `
220233 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 } `
223237 await vscode . commands . executeCommand ( "vscode.open" , uri )
224238 } else {
225239 vscode . window . showInformationMessage ( "No workspace found." )
@@ -229,15 +243,18 @@ export class Commands {
229243 /**
230244 * Open a link to the workspace settings in the Coder dashboard.
231245 *
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).
233250 */
234251 public async navigateToWorkspaceSettings ( workspace : OpenableTreeItem ) {
235252 if ( workspace ) {
236253 const uri = this . storage . getUrl ( ) + `/@${ workspace . workspaceOwner } /${ workspace . workspaceName } /settings`
237254 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`
241258 await vscode . commands . executeCommand ( "vscode.open" , uri )
242259 } else {
243260 vscode . window . showInformationMessage ( "No workspace found." )
@@ -248,7 +265,8 @@ export class Commands {
248265 * Open a workspace or agent that is showing in the sidebar.
249266 *
250267 * 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.
252270 */
253271 public async openFromSidebar ( treeItem : OpenableTreeItem ) {
254272 if ( treeItem ) {
@@ -263,9 +281,9 @@ export class Commands {
263281 }
264282
265283 /**
266- * Open a workspace from the currently logged-in deployment.
284+ * Open a workspace belonging to the currently logged-in deployment.
267285 *
268- * This must only be called if the REST client is logged in .
286+ * This must only be called if logged into a deployment .
269287 */
270288 public async open ( ...args : unknown [ ] ) : Promise < void > {
271289 let workspaceOwner : string
@@ -393,20 +411,20 @@ export class Commands {
393411 * this is a no-op.
394412 */
395413 public async updateWorkspace ( ) : Promise < void > {
396- if ( ! this . storage . workspace || ! this . storage . restClient ) {
414+ if ( ! this . workspace || ! this . workspaceRestClient ) {
397415 return
398416 }
399417 const action = await this . vscodeProposed . window . showInformationMessage (
400418 "Update Workspace" ,
401419 {
402420 useCustom : true ,
403421 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.` ,
405423 } ,
406424 "Update" ,
407425 )
408426 if ( action === "Update" ) {
409- await this . storage . restClient . updateWorkspaceVersion ( this . storage . workspace )
427+ await this . restClient . updateWorkspaceVersion ( this . workspace )
410428 }
411429 }
412430}
0 commit comments