diff --git a/src/main/helm/exec-helm/exec-helm.injectable.ts b/src/main/helm/exec-helm/exec-helm.injectable.ts new file mode 100644 index 0000000000..9005c95baf --- /dev/null +++ b/src/main/helm/exec-helm/exec-helm.injectable.ts @@ -0,0 +1,20 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { getInjectable } from "@ogre-tools/injectable"; +import execFileInjectable from "../../../common/fs/exec-file.injectable"; +import helmBinaryPathInjectable from "../helm-binary-path.injectable"; + +const execHelmInjectable = getInjectable({ + id: "exec-helm", + + instantiate: (di) => { + const execFile = di.inject(execFileInjectable); + const helmBinaryPath = di.inject(helmBinaryPathInjectable); + + return (...args: string[]) => execFile(helmBinaryPath, args); + }, +}); + +export default execHelmInjectable; diff --git a/src/main/helm/get-helm-env/get-helm-env.injectable.ts b/src/main/helm/get-helm-env/get-helm-env.injectable.ts new file mode 100644 index 0000000000..5b023d805d --- /dev/null +++ b/src/main/helm/get-helm-env/get-helm-env.injectable.ts @@ -0,0 +1,38 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { getInjectable } from "@ogre-tools/injectable"; +import execHelmInjectable from "../exec-helm/exec-helm.injectable"; + +export type HelmEnv = Record & { + HELM_REPOSITORY_CACHE?: string; + HELM_REPOSITORY_CONFIG?: string; +}; + +const getHelmEnvInjectable = getInjectable({ + id: "get-helm-env", + + instantiate: (di) => { + const execHelm = di.inject(execHelmInjectable); + + return async () => { + const output = await execHelm("env"); + + const lines = output.split(/\r?\n/); // split by new line feed + const env: HelmEnv = {}; + + lines.forEach((line: string) => { + const [key, value] = line.split("="); + + if (key && value) { + env[key] = value.replace(/"/g, ""); // strip quotas + } + }); + + return env; + }; + }, +}); + +export default getHelmEnvInjectable; diff --git a/src/main/helm/helm-binary-path.injectable.ts b/src/main/helm/helm-binary-path.injectable.ts new file mode 100644 index 0000000000..7793d70977 --- /dev/null +++ b/src/main/helm/helm-binary-path.injectable.ts @@ -0,0 +1,35 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { getInjectable } from "@ogre-tools/injectable"; +import { getBinaryName } from "../../common/vars"; +import getAbsolutePathInjectable from "../../common/path/get-absolute-path.injectable"; +import lensResourcesDirInjectable from "../../common/vars/lens-resources-dir.injectable"; +import normalizedPlatformInjectable from "../../common/vars/normalized-platform.injectable"; +import isProductionInjectable from "../../common/vars/is-production.injectable"; +import normalizedPlatformArchitectureInjectable from "../../common/vars/normalized-platform-architecture.injectable"; + +const helmBinaryPathInjectable = getInjectable({ + id: "helm-binary-path", + + instantiate: (di) => { + const getAbsolutePath = di.inject(getAbsolutePathInjectable); + const lensResourcesDir = di.inject(lensResourcesDirInjectable); + const normalizedPlatform = di.inject(normalizedPlatformInjectable); + const normalizedPlatformArchitecture = di.inject(normalizedPlatformArchitectureInjectable); + const isProduction = di.inject(isProductionInjectable); + + const resourcesDir = isProduction + ? lensResourcesDir + : getAbsolutePath(lensResourcesDir, "binaries", "client", normalizedPlatform); + + const baseBinariesDir = getAbsolutePath(resourcesDir, normalizedPlatformArchitecture); + + const helmBinaryName = getBinaryName("helm", { forPlatform: normalizedPlatform }); + + return getAbsolutePath(baseBinariesDir, helmBinaryName); + }, +}); + +export default helmBinaryPathInjectable;