1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

refactored to remove ShellEnvModifier code from shell-session to local-shell-session

Signed-off-by: Jim Ehrismann <jehrismann@mirantis.com>
This commit is contained in:
Jim Ehrismann 2022-02-09 11:52:32 -05:00
parent 22a32adc49
commit fd66fb0170
4 changed files with 24 additions and 20 deletions

View File

@ -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,

View File

@ -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);

View File

@ -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() {

View File

@ -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<ShellEnvId, Record<string, string>>();
private static shellEnvs = new Map<string, Record<string, string>>();
private static processes = new Map<string, pty.IPty>();
/**
@ -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;
}