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

further refactoring and documentation

Signed-off-by: Jim Ehrismann <jehrismann@mirantis.com>
This commit is contained in:
Jim Ehrismann 2022-02-09 17:26:03 -05:00
parent fd66fb0170
commit ee868910b2
5 changed files with 50 additions and 24 deletions

View File

@ -16,6 +16,19 @@ export class LensMainExtension extends LensExtension {
appMenus: MenuRegistration[] = [];
trayMenus: TrayMenuRegistration[] = [];
/**
* implement this to modify the shell environment that Lens terminals are opened with. The ShellEnvModifier type has the signature
*
* (ctx: ShellEnvContext, env: Record<string, string | undefined>) => Record<string, string | undefined>
*
* @param ctx the shell environment context, specifically the relevant catalog entity for the terminal. This can be used, for example, to get
* cluster-specific information that can be made available in the shell environment by the implementation of terminalShellEnvModifier
*
* @param env the current shell environment that the terminal will be opened with. The implementation should modify this as desired.
*
* @returns the modified shell environment that the terminal will be opened with. The implementation must return env as passed in, if it
* does not modify the shell environment
*/
terminalShellEnvModifier?: ShellEnvModifier;
async navigate(pageId?: string, params?: Record<string, any>, frameId?: number) {

View File

@ -7,8 +7,7 @@ import { LocalShellSession } from "./local-shell-session";
import type { Cluster } from "../../../common/cluster/cluster";
import type WebSocket from "ws";
import createKubectlInjectable from "../../kubectl/create-kubectl.injectable";
import terminalShellEnvModifiersInjectable from "../shell-env-modifier/terminal-shell-env-modifier.injectable";
import { catalogEntityRegistry } from "../../catalog";
import terminalShellEnvModifiersInjectable from "../shell-env-modifier/terminal-shell-env-modify.injectable";
interface InstantiationParameter {
webSocket: WebSocket;
@ -19,12 +18,11 @@ interface InstantiationParameter {
const localShellSessionInjectable = getInjectable({
instantiate: (di, { cluster, tabId, webSocket }: InstantiationParameter) => {
const createKubectl = di.inject(createKubectlInjectable);
const localShellEnvModifiers = di.inject(terminalShellEnvModifiersInjectable);
const localShellEnvModify = di.inject(terminalShellEnvModifiersInjectable);
const kubectl = createKubectl(cluster.version);
const entity = catalogEntityRegistry.getById(cluster.id);
return new LocalShellSession(localShellEnvModifiers.get(), entity, kubectl, webSocket, cluster, tabId);
return new LocalShellSession(localShellEnvModify, kubectl, webSocket, cluster, tabId);
},
lifecycle: lifecycleEnum.transient,

View File

@ -8,15 +8,14 @@ import path from "path";
import { helmCli } from "../../helm/helm-cli";
import { UserStore } from "../../../common/user-store";
import type { Cluster } from "../../../common/cluster/cluster";
import type { ClusterId } from "../../../common/cluster-types";
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) {
constructor(protected shellEnvModify: (clusterId: ClusterId, env: Record<string, string>) => Record<string, string>, kubectl: Kubectl, websocket: WebSocket, cluster: Cluster, terminalId: string) {
super(kubectl, websocket, cluster, terminalId);
}
@ -30,12 +29,8 @@ export class LocalShellSession extends ShellSession {
public async open() {
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)));
}
env = this.shellEnvModify(this.cluster.id, env);
const shell = env.PTYSHELL;
const args = await this.getShellArgs(shell);

View File

@ -4,16 +4,36 @@
*/
import { computed, IComputedValue } from "mobx";
import type { ClusterId } from "../../../common/cluster-types";
import type { LensMainExtension } from "../../../extensions/lens-main-extension";
import { catalogEntityRegistry } from "../../catalog";
interface Dependencies {
extensions: IComputedValue<LensMainExtension[]>;
}
export const terminalShellEnvModifiers = ({ extensions }: Dependencies) => {
return computed(() => (
extensions.get()
.map((extension) => extension.terminalShellEnvModifier)
.filter(Boolean)
));
};
export const terminalShellEnvModify = ({ extensions }: Dependencies) =>
(clusterId: ClusterId, env: Record<string, string>) => {
const terminalShellEnvModifiers = computed(() => (
extensions.get()
.map((extension) => extension.terminalShellEnvModifier)
.filter(Boolean)
))
.get();
if (terminalShellEnvModifiers.length === 0) {
return env;
}
const entity = catalogEntityRegistry.getById(clusterId);
if (entity) {
const ctx = { catalogEntity: entity };
// clone it so the passed value is not also modified
env = JSON.parse(JSON.stringify(env));
env = terminalShellEnvModifiers.reduce((env, modifier) => modifier(ctx, env), env);
}
return env;
};

View File

@ -5,15 +5,15 @@
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
import mainExtensionsInjectable from "../../../extensions/main-extensions.injectable";
import { terminalShellEnvModifiers } from "./terminal-shell-env-modifiers";
import { terminalShellEnvModify } from "./terminal-shell-env-modifiers";
const terminalShellEnvModifiersInjectable = getInjectable({
const terminalShellEnvModifyInjectable = getInjectable({
instantiate: (di) =>
terminalShellEnvModifiers({
terminalShellEnvModify({
extensions: di.inject(mainExtensionsInjectable),
}),
lifecycle: lifecycleEnum.singleton,
});
export default terminalShellEnvModifiersInjectable;
export default terminalShellEnvModifyInjectable;