ShellEnv extension api (#4802)

* adding extension api for terminal environment variables

Signed-off-by: Jim Ehrismann <jehrismann@mirantis.com>

* modified shell env api to work as a transformer (WIP)

Signed-off-by: Jim Ehrismann <jehrismann@mirantis.com>

* address some review comments

Signed-off-by: Jim Ehrismann <jehrismann@mirantis.com>

* shell env modifiers now take a CatalogEntity in ShellEnvContext param

Signed-off-by: Jim Ehrismann <jehrismann@mirantis.com>

* tweaks and bug fix

Signed-off-by: Jim Ehrismann <jehrismann@mirantis.com>

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

Signed-off-by: Jim Ehrismann <jehrismann@mirantis.com>

* further refactoring and documentation

Signed-off-by: Jim Ehrismann <jehrismann@mirantis.com>

* added comment

Signed-off-by: Jim Ehrismann <jehrismann@mirantis.com>
This commit is contained in:
Jim Ehrismann authored and GitHub committed 2022-02-10 16:01:59 -05:00
1 parent 81e6dc5d8e
commit 9b9b8e0d05
7 files changed
+104 -2

No files matched your search

@@ -0,0 +1,12 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { CatalogEntity } from "../../../common/catalog";
export interface ShellEnvContext {
catalogEntity: CatalogEntity;
}
export type ShellEnvModifier = (ctx: ShellEnvContext, env: Record<string, string | undefined>) => Record<string, string | undefined>;
@@ -0,0 +1,39 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
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 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;
};
@@ -0,0 +1,19 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
import mainExtensionsInjectable from "../../../extensions/main-extensions.injectable";
import { terminalShellEnvModify } from "./terminal-shell-env-modifiers";
const terminalShellEnvModifyInjectable = getInjectable({
instantiate: (di) =>
terminalShellEnvModify({
extensions: di.inject(mainExtensionsInjectable),
}),
lifecycle: lifecycleEnum.singleton,
});
export default terminalShellEnvModifyInjectable;