From fd66fb017004d6d279f336c728230cba12af2a6d Mon Sep 17 00:00:00 2001 From: Jim Ehrismann Date: Wed, 9 Feb 2022 11:52:32 -0500 Subject: [PATCH] refactored to remove ShellEnvModifier code from shell-session to local-shell-session Signed-off-by: Jim Ehrismann --- .../local-shell-session.injectable.ts | 2 +- .../local-shell-session.ts | 18 ++++++++++++++- .../node-shell-session/node-shell-session.ts | 2 +- src/main/shell-session/shell-session.ts | 22 +++++-------------- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/main/shell-session/local-shell-session/local-shell-session.injectable.ts b/src/main/shell-session/local-shell-session/local-shell-session.injectable.ts index 9b1d897865..03a8f564e2 100644 --- a/src/main/shell-session/local-shell-session/local-shell-session.injectable.ts +++ b/src/main/shell-session/local-shell-session/local-shell-session.injectable.ts @@ -24,7 +24,7 @@ const localShellSessionInjectable = getInjectable({ const kubectl = createKubectl(cluster.version); const entity = catalogEntityRegistry.getById(cluster.id); - return new LocalShellSession(kubectl, localShellEnvModifiers.get(), entity, webSocket, cluster, tabId); + return new LocalShellSession(localShellEnvModifiers.get(), entity, kubectl, webSocket, cluster, tabId); }, lifecycle: lifecycleEnum.transient, diff --git a/src/main/shell-session/local-shell-session/local-shell-session.ts b/src/main/shell-session/local-shell-session/local-shell-session.ts index ff7a3d34d4..ee520e62aa 100644 --- a/src/main/shell-session/local-shell-session/local-shell-session.ts +++ b/src/main/shell-session/local-shell-session/local-shell-session.ts @@ -3,14 +3,23 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ +import type WebSocket from "ws"; import path from "path"; import { helmCli } from "../../helm/helm-cli"; import { UserStore } from "../../../common/user-store"; +import type { Cluster } from "../../../common/cluster/cluster"; import { ShellSession } from "../shell-session"; +import type { Kubectl } from "../../kubectl/kubectl"; +import type { ShellEnvModifier } from "../shell-env-modifier/shell-env-modifier-registration"; +import type { CatalogEntity } from "../../../common/catalog"; export class LocalShellSession extends ShellSession { ShellType = "shell"; + constructor(protected shellEnvModifiers: ShellEnvModifier[], protected entity: CatalogEntity, kubectl: Kubectl, websocket: WebSocket, cluster: Cluster, terminalId: string) { + super(kubectl, websocket, cluster, terminalId); + } + protected getPathEntries(): string[] { return [helmCli.getBinaryDir()]; } @@ -20,7 +29,14 @@ export class LocalShellSession extends ShellSession { } public async open() { - const env = await this.getCachedShellEnv(); + let env = await this.getCachedShellEnv(); + + if (this.entity) { + const ctx = { catalogEntity: this.entity }; + + env = JSON.parse(JSON.stringify(this.shellEnvModifiers.reduce((env, modifier) => modifier(ctx, env), env))); + } + const shell = env.PTYSHELL; const args = await this.getShellArgs(shell); diff --git a/src/main/shell-session/node-shell-session/node-shell-session.ts b/src/main/shell-session/node-shell-session/node-shell-session.ts index 0f85afa3e3..1fa05a4ae2 100644 --- a/src/main/shell-session/node-shell-session/node-shell-session.ts +++ b/src/main/shell-session/node-shell-session/node-shell-session.ts @@ -25,7 +25,7 @@ export class NodeShellSession extends ShellSession { protected readonly cwd: string | undefined = undefined; constructor(protected nodeName: string, kubectl: Kubectl, socket: WebSocket, cluster: Cluster, terminalId: string) { - super(kubectl, [], undefined, socket, cluster, terminalId); + super(kubectl, socket, cluster, terminalId); } public async open() { diff --git a/src/main/shell-session/shell-session.ts b/src/main/shell-session/shell-session.ts index f81d69cd42..25b7217219 100644 --- a/src/main/shell-session/shell-session.ts +++ b/src/main/shell-session/shell-session.ts @@ -19,8 +19,6 @@ import logger from "../logger"; import { TerminalChannels, TerminalMessage } from "../../renderer/api/terminal-api"; import { deserialize, serialize } from "v8"; import { stat } from "fs/promises"; -import type { ShellEnvModifier } from "./shell-env-modifier/shell-env-modifier-registration"; -import type { CatalogEntity } from "../../common/catalog"; export class ShellOpenError extends Error { constructor(message: string, public cause: Error) { @@ -106,12 +104,10 @@ export enum WebSocketCloseEvent { TlsHandshake = 1015, } -type ShellEnvId = { clusterId: string, ShellType: string }; - export abstract class ShellSession { abstract ShellType: string; - private static shellEnvs = new Map>(); + private static shellEnvs = new Map>(); private static processes = new Map(); /** @@ -159,7 +155,7 @@ export abstract class ShellSession { return { shellProcess, resume }; } - constructor(protected kubectl: Kubectl, protected shellEnvModifiers: ShellEnvModifier[], protected entity: CatalogEntity, protected websocket: WebSocket, protected cluster: Cluster, terminalId: string) { + constructor(protected kubectl: Kubectl, protected websocket: WebSocket, protected cluster: Cluster, terminalId: string) { this.kubeconfigPathP = this.cluster.getProxyKubeconfigPath(); this.kubectlBinDirP = this.kubectl.binDir(); this.terminalId = `${cluster.id}:${terminalId}`; @@ -298,17 +294,16 @@ export abstract class ShellSession { protected async getCachedShellEnv() { const { id: clusterId } = this.cluster; - const { ShellType } = this; - let env = ShellSession.shellEnvs.get({ clusterId, ShellType }); + let env = ShellSession.shellEnvs.get(clusterId); if (!env) { env = await this.getShellEnv(); - ShellSession.shellEnvs.set({ clusterId, ShellType }, env); + ShellSession.shellEnvs.set(clusterId, env); } else { // refresh env in the background this.getShellEnv().then((shellEnv: any) => { - ShellSession.shellEnvs.set({ clusterId, ShellType }, shellEnv); + ShellSession.shellEnvs.set(clusterId, shellEnv); }); } @@ -363,13 +358,6 @@ export abstract class ShellSession { .filter(Boolean) .join(); - if (this.entity) { - const ctx = { catalogEntity: this.entity }; - const modifiedEnv = this.shellEnvModifiers.reduce((env, modifier) => modifier(ctx, env), env); - - return modifiedEnv; - } - return env; }