Skip to content

Commit f3acedf

Browse files
committed
Handle review comments
1 parent 32eec2e commit f3acedf

4 files changed

Lines changed: 41 additions & 41 deletions

File tree

src/core/cliCredentialManager.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { promisify } from "node:util";
55
import * as semver from "semver";
66

77
import { isKeyringEnabled } from "../cliConfig";
8-
import { type FeatureSet, featureSetForVersion } from "../featureSet";
8+
import { featureSetForVersion } from "../featureSet";
99
import { getHeaderArgs } from "../headers";
1010
import { tempFilePath, toSafeHost } from "../util";
1111

@@ -19,6 +19,8 @@ import type { PathResolver } from "./pathResolver";
1919

2020
const execFileAsync = promisify(execFile);
2121

22+
type KeyringFeature = "keyringAuth" | "keyringTokenRead";
23+
2224
const EXEC_TIMEOUT_MS = 60_000;
2325
const EXEC_LOG_INTERVAL_MS = 5_000;
2426

@@ -147,7 +149,7 @@ export class CliCredentialManager {
147149
private async resolveKeyringBinary(
148150
url: string,
149151
configs: Pick<WorkspaceConfiguration, "get">,
150-
feature: keyof Pick<FeatureSet, "keyringAuth" | "keyringTokenRead">,
152+
feature: KeyringFeature,
151153
): Promise<string | undefined> {
152154
if (!isKeyringEnabled(configs)) {
153155
return undefined;

src/core/container.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,21 @@ export class ServiceContainer implements vscode.Disposable {
3838
this.logger,
3939
);
4040
// Circular ref: cliCredentialManager ↔ cliManager. The resolver
41-
// closure captures `ref` which starts undefined, so it must only
42-
// be called after construction completes.
43-
const cliManagerRef: { current: CliManager | undefined } = {
44-
current: undefined,
45-
};
41+
// closure captures `this` by reference, so `this.cliManager` is
42+
// available when the closure is called (after construction).
4643
this.cliCredentialManager = new CliCredentialManager(
4744
this.logger,
4845
async (url) => {
49-
if (!cliManagerRef.current) {
46+
if (!this.cliManager) {
5047
throw new Error(
5148
"BinaryResolver called before CliManager was initialised",
5249
);
5350
}
5451
try {
55-
return await cliManagerRef.current.locateBinary(url);
52+
return await this.cliManager.locateBinary(url);
5653
} catch {
5754
const client = CoderApi.create(url, "", this.logger);
58-
return cliManagerRef.current.fetchBinary(client);
55+
return this.cliManager.fetchBinary(client);
5956
}
6057
},
6158
this.pathResolver,
@@ -65,7 +62,6 @@ export class ServiceContainer implements vscode.Disposable {
6562
this.pathResolver,
6663
this.cliCredentialManager,
6764
);
68-
cliManagerRef.current = this.cliManager;
6965
this.contextManager = new ContextManager(context);
7066
this.loginCoordinator = new LoginCoordinator(
7167
this.secretsManager,

src/featureSet.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@ export interface FeatureSet {
99
keyringTokenRead: boolean;
1010
}
1111

12+
/**
13+
* True when the CLI version is at least `minVersion`, or is a dev build.
14+
* Returns false for null (unknown) versions.
15+
*/
16+
function versionAtLeast(
17+
version: semver.SemVer | null,
18+
minVersion: string,
19+
): boolean {
20+
if (!version) {
21+
return false;
22+
}
23+
return version.compare(minVersion) >= 0 || version.prerelease[0] === "devel";
24+
}
25+
1226
/**
1327
* Builds and returns a FeatureSet object for a given coder version.
1428
*/
@@ -23,29 +37,15 @@ export function featureSetForVersion(
2337
version?.prerelease.length === 0
2438
),
2539

26-
// CLI versions before 2.3.3 don't support the --log-dir flag!
27-
// If this check didn't exist, VS Code connections would fail on
28-
// older versions because of an unknown CLI argument.
29-
proxyLogDirectory:
30-
(version?.compare("2.3.3") ?? 0) > 0 ||
31-
version?.prerelease[0] === "devel",
32-
wildcardSSH:
33-
(version ? version.compare("2.19.0") : -1) >= 0 ||
34-
version?.prerelease[0] === "devel",
35-
36-
// The --reason flag was added to `coder start` in 2.25.0
37-
buildReason:
38-
(version?.compare("2.25.0") ?? 0) >= 0 ||
39-
version?.prerelease[0] === "devel",
40-
41-
// Keyring-backed token storage was added in CLI 2.29.0
42-
keyringAuth:
43-
(version?.compare("2.29.0") ?? 0) >= 0 ||
44-
version?.prerelease[0] === "devel",
45-
46-
// `coder login token` for reading tokens from the keyring was added in 2.31.0
47-
keyringTokenRead:
48-
(version?.compare("2.31.0") ?? 0) >= 0 ||
49-
version?.prerelease[0] === "devel",
40+
// --log-dir flag for proxy logs; vscodessh fails if unsupported
41+
proxyLogDirectory: versionAtLeast(version, "2.4.0"),
42+
// Wildcard SSH host matching
43+
wildcardSSH: versionAtLeast(version, "2.19.0"),
44+
// --reason flag for `coder start`
45+
buildReason: versionAtLeast(version, "2.25.0"),
46+
// Keyring-backed token storage via `coder login`
47+
keyringAuth: versionAtLeast(version, "2.29.0"),
48+
// `coder login token` for reading tokens from the keyring
49+
keyringTokenRead: versionAtLeast(version, "2.31.0"),
5050
};
5151
}

test/unit/featureSet.test.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import { featureSetForVersion } from "@/featureSet";
55

66
describe("check version support", () => {
77
it("has logs", () => {
8-
["v1.3.3+e491217", "v2.3.3+e491217"].forEach((v: string) => {
9-
expect(
10-
featureSetForVersion(semver.parse(v)).proxyLogDirectory,
11-
).toBeFalsy();
12-
});
13-
["v2.3.4+e491217", "v5.3.4+e491217", "v5.0.4+e491217"].forEach(
8+
["v1.3.3+e491217", "v2.3.3+e491217", "v2.3.9+e491217"].forEach(
9+
(v: string) => {
10+
expect(
11+
featureSetForVersion(semver.parse(v)).proxyLogDirectory,
12+
).toBeFalsy();
13+
},
14+
);
15+
["v2.4.0+e491217", "v5.3.4+e491217", "v5.0.4+e491217"].forEach(
1416
(v: string) => {
1517
expect(
1618
featureSetForVersion(semver.parse(v)).proxyLogDirectory,

0 commit comments

Comments
 (0)