@@ -5,8 +5,8 @@ import * as vscode from "vscode"
5
5
import { makeCoderSdk } from "./api"
6
6
import { extractAgents } from "./api-helper"
7
7
import { CertificateError } from "./error"
8
- import { Remote } from "./remote"
9
8
import { Storage } from "./storage"
9
+ import { AuthorityPrefix , toSafeHost } from "./util"
10
10
import { OpenableTreeItem } from "./workspacesProvider"
11
11
12
12
export class Commands {
@@ -153,10 +153,13 @@ export class Commands {
153
153
this . restClient . setHost ( url )
154
154
this . restClient . setSessionToken ( token )
155
155
156
- // Store these to be used in later sessions and in the cli .
156
+ // Store these to be used in later sessions.
157
157
await this . storage . setURL ( url )
158
158
await this . storage . setSessionToken ( token )
159
159
160
+ // Store on disk to be used by the cli.
161
+ await this . storage . configureCli ( toSafeHost ( url ) , url , token )
162
+
160
163
await vscode . commands . executeCommand ( "setContext" , "coder.authenticated" , true )
161
164
if ( user . roles . find ( ( role ) => role . name === "owner" ) ) {
162
165
await vscode . commands . executeCommand ( "setContext" , "coder.isOwner" , true )
@@ -197,6 +200,12 @@ export class Commands {
197
200
* Log out from the currently logged-in deployment.
198
201
*/
199
202
public async logout ( ) : Promise < void > {
203
+ const url = this . storage . getUrl ( )
204
+ if ( ! url ) {
205
+ // Sanity check; command should not be available if no url.
206
+ throw new Error ( "You are not logged in" )
207
+ }
208
+
200
209
// Clear from the REST client. An empty url will indicate to other parts of
201
210
// the code that we are logged out.
202
211
this . restClient . setHost ( "" )
@@ -206,6 +215,9 @@ export class Commands {
206
215
await this . storage . setURL ( undefined )
207
216
await this . storage . setSessionToken ( undefined )
208
217
218
+ // Clear from disk.
219
+ await this . storage . configureCli ( toSafeHost ( url ) , undefined , undefined )
220
+
209
221
await vscode . commands . executeCommand ( "setContext" , "coder.authenticated" , false )
210
222
vscode . window . showInformationMessage ( "You've been logged out of Coder!" , "Login" ) . then ( ( action ) => {
211
223
if ( action === "Login" ) {
@@ -272,13 +284,19 @@ export class Commands {
272
284
/**
273
285
* Open a workspace or agent that is showing in the sidebar.
274
286
*
275
- * This essentially just builds the host name and passes it to the VS Code
276
- * Remote SSH extension, so it is not necessary to be logged in, although then
277
- * the sidebar would not have any workspaces in it anyway.
287
+ * This builds the host name and passes it to the VS Code Remote SSH
288
+ * extension.
289
+
290
+ * Throw if not logged into a deployment.
278
291
*/
279
292
public async openFromSidebar ( treeItem : OpenableTreeItem ) {
280
293
if ( treeItem ) {
294
+ const baseUrl = this . restClient . getAxiosInstance ( ) . defaults . baseURL
295
+ if ( ! baseUrl ) {
296
+ throw new Error ( "You are not logged in" )
297
+ }
281
298
await openWorkspace (
299
+ baseUrl ,
282
300
treeItem . workspaceOwner ,
283
301
treeItem . workspaceName ,
284
302
treeItem . workspaceAgent ,
@@ -291,7 +309,7 @@ export class Commands {
291
309
/**
292
310
* Open a workspace belonging to the currently logged-in deployment.
293
311
*
294
- * This must only be called if logged into a deployment.
312
+ * Throw if not logged into a deployment.
295
313
*/
296
314
public async open ( ...args : unknown [ ] ) : Promise < void > {
297
315
let workspaceOwner : string
@@ -300,6 +318,11 @@ export class Commands {
300
318
let folderPath : string | undefined
301
319
let openRecent : boolean | undefined
302
320
321
+ const baseUrl = this . restClient . getAxiosInstance ( ) . defaults . baseURL
322
+ if ( ! baseUrl ) {
323
+ throw new Error ( "You are not logged in" )
324
+ }
325
+
303
326
if ( args . length === 0 ) {
304
327
const quickPick = vscode . window . createQuickPick ( )
305
328
quickPick . value = "owner:me "
@@ -411,7 +434,7 @@ export class Commands {
411
434
openRecent = args [ 4 ] as boolean | undefined
412
435
}
413
436
414
- await openWorkspace ( workspaceOwner , workspaceName , workspaceAgent , folderPath , openRecent )
437
+ await openWorkspace ( baseUrl , workspaceOwner , workspaceName , workspaceAgent , folderPath , openRecent )
415
438
}
416
439
417
440
/**
@@ -439,9 +462,10 @@ export class Commands {
439
462
440
463
/**
441
464
* Given a workspace, build the host name, find a directory to open, and pass
442
- * both to the Remote SSH plugin.
465
+ * both to the Remote SSH plugin in the form of a remote authority URI .
443
466
*/
444
467
async function openWorkspace (
468
+ baseUrl : string ,
445
469
workspaceOwner : string ,
446
470
workspaceName : string ,
447
471
workspaceAgent : string | undefined ,
@@ -450,7 +474,7 @@ async function openWorkspace(
450
474
) {
451
475
// A workspace can have multiple agents, but that's handled
452
476
// when opening a workspace unless explicitly specified.
453
- let remoteAuthority = `ssh-remote+${ Remote . Prefix } ${ workspaceOwner } --${ workspaceName } `
477
+ let remoteAuthority = `ssh-remote+${ AuthorityPrefix } . ${ toSafeHost ( baseUrl ) } -- ${ workspaceOwner } --${ workspaceName } `
454
478
if ( workspaceAgent ) {
455
479
remoteAuthority += `--${ workspaceAgent } `
456
480
}
0 commit comments