Skip to content

Commit ead6f43

Browse files
committed
Handle secretsManager comments part 2
1 parent 25f4d61 commit ead6f43

File tree

15 files changed

+283
-228
lines changed

15 files changed

+283
-228
lines changed

src/commands.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export class Commands {
7979
*/
8080
public async login(args?: {
8181
url?: string;
82-
label?: string;
82+
safeHostname?: string;
8383
autoLogin?: boolean;
8484
}): Promise<void> {
8585
if (this.deploymentManager.isAuthenticated()) {
@@ -98,13 +98,13 @@ export class Commands {
9898
}
9999

100100
// It is possible that we are trying to log into an old-style host, in which
101-
// case we want to write with the provided blank label instead of generating
102-
// a host label.
103-
const label = args?.label ?? toSafeHost(url);
104-
this.logger.info("Using deployment label", label);
101+
// case we want to write with the provided blank hostname instead of
102+
// generating one.
103+
const safeHostname = args?.safeHostname ?? toSafeHost(url);
104+
this.logger.info("Using hostname", safeHostname);
105105

106106
const result = await this.loginCoordinator.promptForLogin({
107-
label,
107+
safeHostname,
108108
url,
109109
autoLogin: args?.autoLogin,
110110
});
@@ -115,7 +115,7 @@ export class Commands {
115115

116116
await this.deploymentManager.changeDeployment({
117117
url,
118-
label,
118+
safeHostname,
119119
token: result.token,
120120
user: result.user,
121121
});
@@ -307,13 +307,13 @@ export class Commands {
307307

308308
// If workspace_name is provided, run coder ssh before the command
309309
const baseUrl = this.requireExtensionBaseUrl();
310-
const label = toSafeHost(baseUrl);
310+
const safeHost = toSafeHost(baseUrl);
311311
const binary = await this.cliManager.fetchBinary(
312312
this.extensionClient,
313-
label,
313+
safeHost,
314314
);
315315

316-
const configDir = this.pathResolver.getGlobalConfigDir(label);
316+
const configDir = this.pathResolver.getGlobalConfigDir(safeHost);
317317
const globalFlags = getGlobalFlags(
318318
vscode.workspace.getConfiguration(),
319319
configDir,

src/core/cliManager.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,19 @@ export class CliManager {
3333

3434
/**
3535
* Download and return the path to a working binary for the deployment with
36-
* the provided label using the provided client. If the label is empty, use
37-
* the old deployment-unaware path instead.
36+
* the provided hostname using the provided client. If the hostname is empty,
37+
* use the old deployment-unaware path instead.
3838
*
3939
* If there is already a working binary and it matches the server version,
4040
* return that, skipping the download. If it does not match but downloads are
4141
* disabled, return whatever we have and log a warning. Otherwise throw if
4242
* unable to download a working binary, whether because of network issues or
4343
* downloads being disabled.
4444
*/
45-
public async fetchBinary(restClient: Api, label: string): Promise<string> {
45+
public async fetchBinary(
46+
restClient: Api,
47+
safeHostname: string,
48+
): Promise<string> {
4649
const cfg = vscode.workspace.getConfiguration("coder");
4750
// Settings can be undefined when set to their defaults (true in this case),
4851
// so explicitly check against false.
@@ -64,7 +67,7 @@ export class CliManager {
6467
// is valid and matches the server, or if it does not match the server but
6568
// downloads are disabled, we can return early.
6669
const binPath = path.join(
67-
this.pathResolver.getBinaryCachePath(label),
70+
this.pathResolver.getBinaryCachePath(safeHostname),
6871
cliUtils.name(),
6972
);
7073
this.output.info("Using binary path", binPath);
@@ -693,48 +696,48 @@ export class CliManager {
693696
}
694697

695698
/**
696-
* Configure the CLI for the deployment with the provided label.
699+
* Configure the CLI for the deployment with the provided hostname.
697700
*
698701
* Falsey URLs and null tokens are a no-op; we avoid unconfiguring the CLI to
699702
* avoid breaking existing connections.
700703
*/
701704
public async configure(
702-
label: string,
705+
safeHostname: string,
703706
url: string | undefined,
704707
token: string | null,
705708
) {
706709
await Promise.all([
707-
this.updateUrlForCli(label, url),
708-
this.updateTokenForCli(label, token),
710+
this.updateUrlForCli(safeHostname, url),
711+
this.updateTokenForCli(safeHostname, token),
709712
]);
710713
}
711714

712715
/**
713-
* Update the URL for the deployment with the provided label on disk which can
714-
* be used by the CLI via --url-file. If the URL is falsey, do nothing.
716+
* Update the URL for the deployment with the provided hostname on disk which
717+
* can be used by the CLI via --url-file. If the URL is falsey, do nothing.
715718
*
716-
* If the label is empty, read the old deployment-unaware config instead.
719+
* If the hostname is empty, read the old deployment-unaware config instead.
717720
*/
718721
private async updateUrlForCli(
719-
label: string,
722+
safeHostname: string,
720723
url: string | undefined,
721724
): Promise<void> {
722725
if (url) {
723-
const urlPath = this.pathResolver.getUrlPath(label);
726+
const urlPath = this.pathResolver.getUrlPath(safeHostname);
724727
await this.atomicWriteFile(urlPath, url);
725728
}
726729
}
727730

728731
/**
729-
* Update the session token for a deployment with the provided label on disk
730-
* which can be used by the CLI via --session-token-file. If the token is
731-
* null, do nothing.
732+
* Update the session token for a deployment with the provided hostname on
733+
* disk which can be used by the CLI via --session-token-file. If the token
734+
* is null, do nothing.
732735
*
733-
* If the label is empty, read the old deployment-unaware config instead.
736+
* If the hostname is empty, read the old deployment-unaware config instead.
734737
*/
735-
private async updateTokenForCli(label: string, token: string | null) {
738+
private async updateTokenForCli(safeHostname: string, token: string | null) {
736739
if (token !== null) {
737-
const tokenPath = this.pathResolver.getSessionTokenPath(label);
740+
const tokenPath = this.pathResolver.getSessionTokenPath(safeHostname);
738741
await this.atomicWriteFile(tokenPath, token);
739742
}
740743
}

src/core/container.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export class ServiceContainer implements vscode.Disposable {
3535
this.secretsManager = new SecretsManager(
3636
context.secrets,
3737
context.globalState,
38+
this.logger,
3839
);
3940
this.cliManager = new CliManager(
4041
this.vscodeProposed,

src/core/pathResolver.ts

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as path from "path";
1+
import * as path from "node:path";
22
import * as vscode from "vscode";
33

44
export class PathResolver {
@@ -8,26 +8,28 @@ export class PathResolver {
88
) {}
99

1010
/**
11-
* Return the directory for the deployment with the provided label to where
12-
* the global Coder configs are stored.
11+
* Return the directory for the deployment with the provided hostname to
12+
* where the global Coder configs are stored.
1313
*
14-
* If the label is empty, read the old deployment-unaware config instead.
14+
* If the hostname is empty, read the old deployment-unaware config instead.
1515
*
1616
* The caller must ensure this directory exists before use.
1717
*/
18-
public getGlobalConfigDir(label: string): string {
19-
return label ? path.join(this.basePath, label) : this.basePath;
18+
public getGlobalConfigDir(safeHostname: string): string {
19+
return safeHostname
20+
? path.join(this.basePath, safeHostname)
21+
: this.basePath;
2022
}
2123

2224
/**
23-
* Return the directory for a deployment with the provided label to where its
24-
* binary is cached.
25+
* Return the directory for a deployment with the provided hostname to where
26+
* its binary is cached.
2527
*
26-
* If the label is empty, read the old deployment-unaware config instead.
28+
* If the hostname is empty, read the old deployment-unaware config instead.
2729
*
2830
* The caller must ensure this directory exists before use.
2931
*/
30-
public getBinaryCachePath(label: string): string {
32+
public getBinaryCachePath(safeHostname: string): string {
3133
const settingPath = vscode.workspace
3234
.getConfiguration()
3335
.get<string>("coder.binaryDestination")
@@ -36,7 +38,7 @@ export class PathResolver {
3638
settingPath || process.env.CODER_BINARY_DESTINATION?.trim();
3739
return binaryPath
3840
? path.normalize(binaryPath)
39-
: path.join(this.getGlobalConfigDir(label), "bin");
41+
: path.join(this.getGlobalConfigDir(safeHostname), "bin");
4042
}
4143

4244
/**
@@ -69,39 +71,39 @@ export class PathResolver {
6971
}
7072

7173
/**
72-
* Return the directory for the deployment with the provided label to where
73-
* its session token is stored.
74+
* Return the directory for the deployment with the provided hostname to
75+
* where its session token is stored.
7476
*
75-
* If the label is empty, read the old deployment-unaware config instead.
77+
* If the hostname is empty, read the old deployment-unaware config instead.
7678
*
7779
* The caller must ensure this directory exists before use.
7880
*/
79-
public getSessionTokenPath(label: string): string {
80-
return path.join(this.getGlobalConfigDir(label), "session");
81+
public getSessionTokenPath(safeHostname: string): string {
82+
return path.join(this.getGlobalConfigDir(safeHostname), "session");
8183
}
8284

8385
/**
84-
* Return the directory for the deployment with the provided label to where
85-
* its session token was stored by older code.
86+
* Return the directory for the deployment with the provided hostname to
87+
* where its session token was stored by older code.
8688
*
87-
* If the label is empty, read the old deployment-unaware config instead.
89+
* If the hostname is empty, read the old deployment-unaware config instead.
8890
*
8991
* The caller must ensure this directory exists before use.
9092
*/
91-
public getLegacySessionTokenPath(label: string): string {
92-
return path.join(this.getGlobalConfigDir(label), "session_token");
93+
public getLegacySessionTokenPath(safeHostname: string): string {
94+
return path.join(this.getGlobalConfigDir(safeHostname), "session_token");
9395
}
9496

9597
/**
96-
* Return the directory for the deployment with the provided label to where
97-
* its url is stored.
98+
* Return the directory for the deployment with the provided hostname to
99+
* where its url is stored.
98100
*
99-
* If the label is empty, read the old deployment-unaware config instead.
101+
* If the hostname is empty, read the old deployment-unaware config instead.
100102
*
101103
* The caller must ensure this directory exists before use.
102104
*/
103-
public getUrlPath(label: string): string {
104-
return path.join(this.getGlobalConfigDir(label), "url");
105+
public getUrlPath(safeHostname: string): string {
106+
return path.join(this.getGlobalConfigDir(safeHostname), "url");
105107
}
106108

107109
/**

0 commit comments

Comments
 (0)