Skip to content

Commit 0e4a760

Browse files
committed
Remove storage.ts file entirely
1 parent e3ba71a commit 0e4a760

File tree

8 files changed

+74
-552
lines changed

8 files changed

+74
-552
lines changed

src/commands.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import * as vscode from "vscode";
99
import { createWorkspaceIdentifier, extractAgents } from "./api/api-helper";
1010
import { CoderApi } from "./api/coderApi";
1111
import { needToken } from "./api/utils";
12+
import { BinaryManager } from "./core/binaryManager";
1213
import { CliConfigManager } from "./core/cliConfig";
1314
import { MementoManager } from "./core/mementoManager";
1415
import { PathResolver } from "./core/pathResolver";
1516
import { SecretsManager } from "./core/secretsManager";
1617
import { CertificateError } from "./error";
1718
import { getGlobalFlags } from "./globalFlags";
18-
import { Storage } from "./storage";
19+
import { Logger } from "./logging/logger";
1920
import { escapeCommandArg, toRemoteAuthority, toSafeHost } from "./util";
2021
import {
2122
AgentTreeItem,
@@ -39,10 +40,11 @@ export class Commands {
3940
public constructor(
4041
private readonly vscodeProposed: typeof vscode,
4142
private readonly restClient: Api,
42-
private readonly storage: Storage,
43+
private readonly logger: Logger,
4344
private readonly pathResolver: PathResolver,
4445
private readonly mementoManager: MementoManager,
4546
private readonly secretsManager: SecretsManager,
47+
private readonly binaryManager: BinaryManager,
4648
) {
4749
this.cliConfigManager = new CliConfigManager(pathResolver);
4850
}
@@ -249,7 +251,7 @@ export class Commands {
249251
token: string,
250252
isAutologin: boolean,
251253
): Promise<{ user: User; token: string } | null> {
252-
const client = CoderApi.create(url, token, this.storage.output, () =>
254+
const client = CoderApi.create(url, token, this.logger, () =>
253255
vscode.workspace.getConfiguration(),
254256
);
255257
if (!needToken(vscode.workspace.getConfiguration())) {
@@ -261,10 +263,7 @@ export class Commands {
261263
} catch (err) {
262264
const message = getErrorMessage(err, "no response from the server");
263265
if (isAutologin) {
264-
this.storage.output.warn(
265-
"Failed to log in to Coder server:",
266-
message,
267-
);
266+
this.logger.warn("Failed to log in to Coder server:", message);
268267
} else {
269268
this.vscodeProposed.window.showErrorMessage(
270269
"Failed to log in to Coder server",
@@ -516,7 +515,7 @@ export class Commands {
516515
if (!url) {
517516
throw new Error("No coder url found for sidebar");
518517
}
519-
const binary = await this.storage.fetchBinary(
518+
const binary = await this.binaryManager.fetchBinary(
520519
this.restClient,
521520
toSafeHost(url),
522521
);
@@ -764,7 +763,7 @@ export class Commands {
764763
// If we have no agents, the workspace may not be running, in which case
765764
// we need to fetch the agents through the resources API, as the
766765
// workspaces query does not include agents when off.
767-
this.storage.output.info("Fetching agents from template version");
766+
this.logger.info("Fetching agents from template version");
768767
const resources = await this.restClient.getTemplateVersionResources(
769768
workspace.latest_build.template_version_id,
770769
);

src/core/pathResolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export class PathResolver {
103103
}
104104

105105
/**
106-
* The uri of a directory in which the extension can create log files.
106+
* The URI of a directory in which the extension can create log files.
107107
*
108108
* The directory might not exist on disk and creation is up to the extension.
109109
* However, the parent directory is guaranteed to be existent.

src/extension.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import { errToStr } from "./api/api-helper";
77
import { CoderApi } from "./api/coderApi";
88
import { needToken } from "./api/utils";
99
import { Commands } from "./commands";
10+
import { BinaryManager } from "./core/binaryManager";
1011
import { CliConfigManager } from "./core/cliConfig";
1112
import { MementoManager } from "./core/mementoManager";
1213
import { PathResolver } from "./core/pathResolver";
1314
import { SecretsManager } from "./core/secretsManager";
1415
import { CertificateError, getErrorDetail } from "./error";
1516
import { Remote } from "./remote";
16-
import { Storage } from "./storage";
1717
import { toSafeHost } from "./util";
1818
import { WorkspaceQuery, WorkspaceProvider } from "./workspacesProvider";
1919

@@ -62,7 +62,6 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
6262
const secretsManager = new SecretsManager(ctx.secrets);
6363

6464
const output = vscode.window.createOutputChannel("Coder", { log: true });
65-
const storage = new Storage(output, pathResolver);
6665

6766
// Try to clear this flag ASAP
6867
const isFirstConnect = await mementoManager.getAndClearFirstConnect();
@@ -74,20 +73,20 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
7473
const client = CoderApi.create(
7574
url || "",
7675
await secretsManager.getSessionToken(),
77-
storage.output,
76+
output,
7877
() => vscode.workspace.getConfiguration(),
7978
);
8079

8180
const myWorkspacesProvider = new WorkspaceProvider(
8281
WorkspaceQuery.Mine,
8382
client,
84-
storage,
83+
output,
8584
5,
8685
);
8786
const allWorkspacesProvider = new WorkspaceProvider(
8887
WorkspaceQuery.All,
8988
client,
90-
storage,
89+
output,
9190
);
9291

9392
// createTreeView, unlike registerTreeDataProvider, gives us the tree view API
@@ -257,15 +256,18 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
257256
},
258257
});
259258

259+
const binaryManager = new BinaryManager(output, pathResolver);
260+
260261
// Register globally available commands. Many of these have visibility
261262
// controlled by contexts, see `when` in the package.json.
262263
const commands = new Commands(
263264
vscodeProposed,
264265
client,
265-
storage,
266+
output,
266267
pathResolver,
267268
mementoManager,
268269
secretsManager,
270+
binaryManager,
269271
);
270272
vscode.commands.registerCommand("coder.login", commands.login.bind(commands));
271273
vscode.commands.registerCommand(
@@ -322,10 +324,11 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
322324
if (remoteSSHExtension && vscodeProposed.env.remoteAuthority) {
323325
const remote = new Remote(
324326
vscodeProposed,
325-
storage,
327+
output,
326328
commands,
327329
ctx.extensionMode,
328330
pathResolver,
331+
binaryManager,
329332
);
330333
try {
331334
const details = await remote.setup(
@@ -340,7 +343,7 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
340343
}
341344
} catch (ex) {
342345
if (ex instanceof CertificateError) {
343-
storage.output.warn(ex.x509Err || ex.message);
346+
output.warn(ex.x509Err || ex.message);
344347
await ex.showModal("Failed to open workspace");
345348
} else if (isAxiosError(ex)) {
346349
const msg = getErrorMessage(ex, "None");
@@ -349,7 +352,7 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
349352
const method = ex.config?.method?.toUpperCase() || "request";
350353
const status = ex.response?.status || "None";
351354
const message = `API ${method} to '${urlString}' failed.\nStatus code: ${status}\nMessage: ${msg}\nDetail: ${detail}`;
352-
storage.output.warn(message);
355+
output.warn(message);
353356
await vscodeProposed.window.showErrorMessage(
354357
"Failed to open workspace",
355358
{
@@ -360,7 +363,7 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
360363
);
361364
} else {
362365
const message = errToStr(ex, "No error message was provided");
363-
storage.output.warn(message);
366+
output.warn(message);
364367
await vscodeProposed.window.showErrorMessage(
365368
"Failed to open workspace",
366369
{
@@ -379,12 +382,12 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
379382
// See if the plugin client is authenticated.
380383
const baseUrl = client.getAxiosInstance().defaults.baseURL;
381384
if (baseUrl) {
382-
storage.output.info(`Logged in to ${baseUrl}; checking credentials`);
385+
output.info(`Logged in to ${baseUrl}; checking credentials`);
383386
client
384387
.getAuthenticatedUser()
385388
.then(async (user) => {
386389
if (user && user.roles) {
387-
storage.output.info("Credentials are valid");
390+
output.info("Credentials are valid");
388391
vscode.commands.executeCommand(
389392
"setContext",
390393
"coder.authenticated",
@@ -402,13 +405,13 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
402405
myWorkspacesProvider.fetchAndRefresh();
403406
allWorkspacesProvider.fetchAndRefresh();
404407
} else {
405-
storage.output.warn("No error, but got unexpected response", user);
408+
output.warn("No error, but got unexpected response", user);
406409
}
407410
})
408411
.catch((error) => {
409412
// This should be a failure to make the request, like the header command
410413
// errored.
411-
storage.output.warn("Failed to check user authentication", error);
414+
output.warn("Failed to check user authentication", error);
412415
vscode.window.showErrorMessage(
413416
`Failed to check user authentication: ${error.message}`,
414417
);
@@ -417,7 +420,7 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
417420
vscode.commands.executeCommand("setContext", "coder.loaded", true);
418421
});
419422
} else {
420-
storage.output.info("Not currently logged in");
423+
output.info("Not currently logged in");
421424
vscode.commands.executeCommand("setContext", "coder.loaded", true);
422425

423426
// Handle autologin, if not already logged in.

src/inbox.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
} from "coder/site/src/api/typesGenerated";
55
import * as vscode from "vscode";
66
import { CoderApi } from "./api/coderApi";
7-
import { type Storage } from "./storage";
7+
import { Logger } from "./logging/logger";
88
import { OneWayWebSocket } from "./websocket/oneWayWebSocket";
99

1010
// These are the template IDs of our notifications.
@@ -14,12 +14,12 @@ const TEMPLATE_WORKSPACE_OUT_OF_MEMORY = "a9d027b4-ac49-4fb1-9f6d-45af15f64e7a";
1414
const TEMPLATE_WORKSPACE_OUT_OF_DISK = "f047f6a3-5713-40f7-85aa-0394cce9fa3a";
1515

1616
export class Inbox implements vscode.Disposable {
17-
readonly #storage: Storage;
17+
readonly #logger: Logger;
1818
#disposed = false;
1919
#socket: OneWayWebSocket<GetInboxNotificationResponse>;
2020

21-
constructor(workspace: Workspace, client: CoderApi, storage: Storage) {
22-
this.#storage = storage;
21+
constructor(workspace: Workspace, client: CoderApi, logger: Logger) {
22+
this.#logger = logger;
2323

2424
const watchTemplates = [
2525
TEMPLATE_WORKSPACE_OUT_OF_DISK,
@@ -31,7 +31,7 @@ export class Inbox implements vscode.Disposable {
3131
this.#socket = client.watchInboxNotifications(watchTemplates, watchTargets);
3232

3333
this.#socket.addEventListener("open", () => {
34-
this.#storage.output.info("Listening to Coder Inbox");
34+
this.#logger.info("Listening to Coder Inbox");
3535
});
3636

3737
this.#socket.addEventListener("error", () => {
@@ -41,10 +41,7 @@ export class Inbox implements vscode.Disposable {
4141

4242
this.#socket.addEventListener("message", (data) => {
4343
if (data.parseError) {
44-
this.#storage.output.error(
45-
"Failed to parse inbox message",
46-
data.parseError,
47-
);
44+
this.#logger.error("Failed to parse inbox message", data.parseError);
4845
} else {
4946
vscode.window.showInformationMessage(
5047
data.parsedMessage.notification.title,
@@ -55,7 +52,7 @@ export class Inbox implements vscode.Disposable {
5552

5653
dispose() {
5754
if (!this.#disposed) {
58-
this.#storage.output.info("No longer listening to Coder Inbox");
55+
this.#logger.info("No longer listening to Coder Inbox");
5956
this.#socket.close();
6057
this.#disposed = true;
6158
}

0 commit comments

Comments
 (0)