Skip to content

Commit

Permalink
Use components/ExtensionContext
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Dover authored and Scott Dover committed Nov 3, 2023
1 parent 12f27eb commit ec16802
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 27 deletions.
14 changes: 13 additions & 1 deletion client/src/components/ExtensionContext.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright © 2023, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { ExtensionContext } from "vscode";
import { ExtensionContext, Uri } from "vscode";

let context: ExtensionContext;

Expand All @@ -26,3 +26,15 @@ export async function getContextValue(
): Promise<string | undefined> {
return context.workspaceState.get(key);
}

export async function setSecret(key: string, value: string): Promise<void> {
await context.secrets.store(key, value);
}

export async function getSecret(key: string): Promise<string> {
return await context.secrets.get(key);
}

export function getGlobalStorageUri(): Uri {
return context.globalStorageUri;
}
13 changes: 2 additions & 11 deletions client/src/connection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
ViyaProfile,
toAutoExecLines,
} from "../components/profile";
import { extensionContext } from "../node/extension";
import { ITCProtocol, getSession as getITCSession } from "./itc";
import { Config as RestConfig, getSession as getRestSession } from "./rest";
import {
Expand Down Expand Up @@ -55,17 +54,9 @@ export function getSession(): Session {
case ConnectionType.SSH:
return getSSHSession(validProfile.profile);
case ConnectionType.COM:
return getITCSession(
validProfile.profile,
ITCProtocol.COM,
extensionContext,
);
return getITCSession(validProfile.profile, ITCProtocol.COM);
case ConnectionType.IOM:
return getITCSession(
validProfile.profile,
ITCProtocol.IOMBridge,
extensionContext,
);
return getITCSession(validProfile.profile, ITCProtocol.IOMBridge);
default:
throw new Error(
l10n.t("Invalid connectionType. Check Profile settings."),
Expand Down
30 changes: 16 additions & 14 deletions client/src/connection/itc/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
// Copyright © 2023, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { ExtensionContext, Uri, l10n, window, workspace } from "vscode";
import { Uri, l10n, window, workspace } from "vscode";

import { ChildProcessWithoutNullStreams, spawn } from "child_process";
import { resolve } from "path";

import { BaseConfig, RunResult } from "..";
import {
getGlobalStorageUri,
getSecret,
setSecret,
} from "../../components/ExtensionContext";
import { Session } from "../session";
import { scriptContent } from "./script";

Expand Down Expand Up @@ -37,12 +42,10 @@ export class ITCSession extends Session {
private _runReject: ((reason?) => void) | undefined;
private _workDirectory: string;
private _password: string;
private _context: ExtensionContext;

constructor(extensionContext: ExtensionContext) {
constructor() {
super();
this._password = "";
this._context = extensionContext;
}

public set config(value: Config) {
Expand Down Expand Up @@ -123,12 +126,11 @@ export class ITCSession extends Session {
return setupPromise;
};

private storePassword = async () => {
await this._context.secrets.store(PASSWORD_KEY, this._password);
};
private storePassword = async () =>
await setSecret(PASSWORD_KEY, this._password);

private clearPassword = async () => {
await this._context.secrets.delete(PASSWORD_KEY);
await setSecret(PASSWORD_KEY, "");
this._password = "";
};

Expand All @@ -137,7 +139,7 @@ export class ITCSession extends Session {
return "";
}

const storedPassword = await this._context.secrets.get(PASSWORD_KEY);
const storedPassword = await getSecret(PASSWORD_KEY);
if (storedPassword) {
return storedPassword;
}
Expand Down Expand Up @@ -308,14 +310,15 @@ do {
* Fetches the ODS output results for the latest html results file.
*/
private fetchResults = async () => {
const globalStorageUri = getGlobalStorageUri();
try {
await workspace.fs.readDirectory(this._context.globalStorageUri);
await workspace.fs.readDirectory(globalStorageUri);
} catch (e) {
await workspace.fs.createDirectory(this._context.globalStorageUri);
await workspace.fs.createDirectory(globalStorageUri);
}

const outputFileUri = Uri.joinPath(
this._context.globalStorageUri,
globalStorageUri,
`${this._html5FileName}.htm`,
);
this._shellProcess.stdin.write(
Expand Down Expand Up @@ -368,7 +371,6 @@ $runner.FetchResultsFile($filePath, $outputFile)\n`,
export const getSession = (
c: Partial<Config>,
protocol: ITCProtocol,
extensionContext: ExtensionContext,
): Session => {
const defaults = {
host: "localhost",
Expand All @@ -378,7 +380,7 @@ export const getSession = (
};

if (!sessionInstance) {
sessionInstance = new ITCSession(extensionContext);
sessionInstance = new ITCSession();
}
sessionInstance.config = { ...defaults, ...c };
return sessionInstance;
Expand Down
5 changes: 4 additions & 1 deletion client/test/connection/itc/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { join } from "path";
import { SinonSandbox, SinonStub, createSandbox } from "sinon";
import { stubInterface } from "ts-sinon";

import { setContext } from "../../../src/components/ExtensionContext";
import { ITCProtocol, getSession } from "../../../src/connection/itc";
import { scriptContent } from "../../../src/connection/itc/script";
import { Session } from "../../../src/connection/session";
Expand Down Expand Up @@ -62,7 +63,9 @@ describe("ITC connection", () => {
secrets: secretStore,
};

session = getSession(config, ITCProtocol.COM, stubbedExtensionContext);
setContext(stubbedExtensionContext);

session = getSession(config, ITCProtocol.COM);
session.onLogFn = () => {
return;
};
Expand Down

0 comments on commit ec16802

Please sign in to comment.