From bcfa57d5b00d98f7bd4e99e127309bef2ea71ec3 Mon Sep 17 00:00:00 2001 From: Janne Savolainen Date: Thu, 9 Jun 2022 14:05:56 +0300 Subject: [PATCH] Remove duplication how to acquire binary path for helm Signed-off-by: Janne Savolainen --- .../base-bundled-binaries-dir.injectable.ts | 24 ++++++++++++------- .../vars/bundled-resources-dir.injectable.ts | 9 ++++--- src/main/getDiForUnitTesting.ts | 4 ++-- src/main/helm/helm-binary-path.injectable.ts | 16 +++---------- .../create-kube-auth-proxy.injectable.ts | 4 ++-- .../kubectl/bundled-binary-path.injectable.ts | 4 ++-- src/main/kubectl/create-kubectl.injectable.ts | 4 ++-- 7 files changed, 32 insertions(+), 33 deletions(-) diff --git a/src/common/vars/base-bundled-binaries-dir.injectable.ts b/src/common/vars/base-bundled-binaries-dir.injectable.ts index 5f42f7f45a..f0d78482c8 100644 --- a/src/common/vars/base-bundled-binaries-dir.injectable.ts +++ b/src/common/vars/base-bundled-binaries-dir.injectable.ts @@ -3,16 +3,22 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getInjectable } from "@ogre-tools/injectable"; -import path from "path"; -import normalizedPlatformArchitecture from "./normalized-platform-architecture.injectable"; import bundledResourcesDirectoryInjectable from "./bundled-resources-dir.injectable"; +import getAbsolutePathInjectable from "../path/get-absolute-path.injectable"; +import normalizedPlatformArchitectureInjectable from "./normalized-platform-architecture.injectable"; -const baseBundeledBinariesDirectoryInjectable = getInjectable({ - id: "base-bundeled-binaries-directory", - instantiate: (di) => path.join( - di.inject(bundledResourcesDirectoryInjectable), - di.inject(normalizedPlatformArchitecture), - ), +const baseBundledBinariesDirectoryInjectable = getInjectable({ + id: "base-bundled-binaries-directory", + instantiate: (di) => { + const bundledResourcesDirectory = di.inject(bundledResourcesDirectoryInjectable); + const normalizedPlatformArchitecture = di.inject(normalizedPlatformArchitectureInjectable); + const getAbsolutePath = di.inject(getAbsolutePathInjectable); + + return getAbsolutePath( + bundledResourcesDirectory, + normalizedPlatformArchitecture, + ); + }, }); -export default baseBundeledBinariesDirectoryInjectable; +export default baseBundledBinariesDirectoryInjectable; diff --git a/src/common/vars/bundled-resources-dir.injectable.ts b/src/common/vars/bundled-resources-dir.injectable.ts index a73ff98e78..e20a638384 100644 --- a/src/common/vars/bundled-resources-dir.injectable.ts +++ b/src/common/vars/bundled-resources-dir.injectable.ts @@ -3,19 +3,22 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getInjectable } from "@ogre-tools/injectable"; -import path from "path"; import isProductionInjectable from "./is-production.injectable"; import normalizedPlatformInjectable from "./normalized-platform.injectable"; +import getAbsolutePathInjectable from "../path/get-absolute-path.injectable"; +import lensResourcesDirInjectable from "./lens-resources-dir.injectable"; const bundledResourcesDirectoryInjectable = getInjectable({ id: "bundled-resources-directory", instantiate: (di) => { const isProduction = di.inject(isProductionInjectable); const normalizedPlatform = di.inject(normalizedPlatformInjectable); + const getAbsolutePath = di.inject(getAbsolutePathInjectable); + const lensResourcesDir = di.inject(lensResourcesDirInjectable); return isProduction - ? process.resourcesPath - : path.join(process.cwd(), "binaries", "client", normalizedPlatform); + ? lensResourcesDir + : getAbsolutePath(lensResourcesDir, "binaries", "client", normalizedPlatform); }, }); diff --git a/src/main/getDiForUnitTesting.ts b/src/main/getDiForUnitTesting.ts index 4cc86a20bc..43f61c22cc 100644 --- a/src/main/getDiForUnitTesting.ts +++ b/src/main/getDiForUnitTesting.ts @@ -76,7 +76,7 @@ import quitAndInstallUpdateInjectable from "./electron-app/features/quit-and-ins import electronUpdaterIsActiveInjectable from "./electron-app/features/electron-updater-is-active.injectable"; import publishIsConfiguredInjectable from "./application-update/publish-is-configured.injectable"; import checkForPlatformUpdatesInjectable from "./application-update/check-for-platform-updates/check-for-platform-updates.injectable"; -import baseBundeledBinariesDirectoryInjectable from "../common/vars/base-bundled-binaries-dir.injectable"; +import baseBundledBinariesDirectoryInjectable from "../common/vars/base-bundled-binaries-dir.injectable"; import setUpdateOnQuitInjectable from "./electron-app/features/set-update-on-quit.injectable"; import downloadPlatformUpdateInjectable from "./application-update/download-platform-update/download-platform-update.injectable"; import startCatalogSyncInjectable from "./catalog-sync-to-renderer/start-catalog-sync.injectable"; @@ -172,7 +172,7 @@ export function getDiForUnitTesting(opts: { doGeneralOverrides?: boolean } = {}) di.override(broadcastMessageInjectable, () => (channel) => { throw new Error(`Tried to broadcast message to channel "${channel}" over IPC without explicit override.`); }); - di.override(baseBundeledBinariesDirectoryInjectable, () => "some-bin-directory"); + di.override(baseBundledBinariesDirectoryInjectable, () => "some-bin-directory"); di.override(spawnInjectable, () => () => { return { stderr: { on: jest.fn(), removeAllListeners: jest.fn() }, diff --git a/src/main/helm/helm-binary-path.injectable.ts b/src/main/helm/helm-binary-path.injectable.ts index 7793d70977..d5983572c7 100644 --- a/src/main/helm/helm-binary-path.injectable.ts +++ b/src/main/helm/helm-binary-path.injectable.ts @@ -5,30 +5,20 @@ 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"; +import baseBundledBinariesDirectoryInjectable from "../../common/vars/base-bundled-binaries-dir.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 baseBundledBinariesDirectory = di.inject(baseBundledBinariesDirectoryInjectable); const helmBinaryName = getBinaryName("helm", { forPlatform: normalizedPlatform }); - return getAbsolutePath(baseBinariesDir, helmBinaryName); + return getAbsolutePath(baseBundledBinariesDirectory, helmBinaryName); }, }); diff --git a/src/main/kube-auth-proxy/create-kube-auth-proxy.injectable.ts b/src/main/kube-auth-proxy/create-kube-auth-proxy.injectable.ts index 07bb87bd63..8546adb4fb 100644 --- a/src/main/kube-auth-proxy/create-kube-auth-proxy.injectable.ts +++ b/src/main/kube-auth-proxy/create-kube-auth-proxy.injectable.ts @@ -12,7 +12,7 @@ import { getBinaryName } from "../../common/vars"; import spawnInjectable from "../child-process/spawn.injectable"; import { getKubeAuthProxyCertificate } from "./get-kube-auth-proxy-certificate"; import loggerInjectable from "../../common/logger.injectable"; -import baseBundeledBinariesDirectoryInjectable from "../../common/vars/base-bundled-binaries-dir.injectable"; +import baseBundledBinariesDirectoryInjectable from "../../common/vars/base-bundled-binaries-dir.injectable"; export type CreateKubeAuthProxy = (cluster: Cluster, environmentVariables: NodeJS.ProcessEnv) => KubeAuthProxy; @@ -25,7 +25,7 @@ const createKubeAuthProxyInjectable = getInjectable({ return (cluster: Cluster, environmentVariables: NodeJS.ProcessEnv) => { const clusterUrl = new URL(cluster.apiUrl); const dependencies: KubeAuthProxyDependencies = { - proxyBinPath: path.join(di.inject(baseBundeledBinariesDirectoryInjectable), binaryName), + proxyBinPath: path.join(di.inject(baseBundledBinariesDirectoryInjectable), binaryName), proxyCert: getKubeAuthProxyCertificate(clusterUrl.hostname, selfsigned.generate), spawn: di.inject(spawnInjectable), logger: di.inject(loggerInjectable), diff --git a/src/main/kubectl/bundled-binary-path.injectable.ts b/src/main/kubectl/bundled-binary-path.injectable.ts index 99cb7f2e7e..df5ee63c04 100644 --- a/src/main/kubectl/bundled-binary-path.injectable.ts +++ b/src/main/kubectl/bundled-binary-path.injectable.ts @@ -4,13 +4,13 @@ */ import { getInjectable } from "@ogre-tools/injectable"; import path from "path"; -import baseBundeledBinariesDirectoryInjectable from "../../common/vars/base-bundled-binaries-dir.injectable"; +import baseBundledBinariesDirectoryInjectable from "../../common/vars/base-bundled-binaries-dir.injectable"; import kubectlBinaryNameInjectable from "./binary-name.injectable"; const bundledKubectlBinaryPathInjectable = getInjectable({ id: "bundled-kubectl-binary-path", instantiate: (di) => path.join( - di.inject(baseBundeledBinariesDirectoryInjectable), + di.inject(baseBundledBinariesDirectoryInjectable), di.inject(kubectlBinaryNameInjectable), ), }); diff --git a/src/main/kubectl/create-kubectl.injectable.ts b/src/main/kubectl/create-kubectl.injectable.ts index d670e8e665..1e13b7d02c 100644 --- a/src/main/kubectl/create-kubectl.injectable.ts +++ b/src/main/kubectl/create-kubectl.injectable.ts @@ -11,7 +11,7 @@ import kubectlDownloadingNormalizedArchInjectable from "./normalized-arch.inject import normalizedPlatformInjectable from "../../common/vars/normalized-platform.injectable"; import kubectlBinaryNameInjectable from "./binary-name.injectable"; import bundledKubectlBinaryPathInjectable from "./bundled-binary-path.injectable"; -import baseBundeledBinariesDirectoryInjectable from "../../common/vars/base-bundled-binaries-dir.injectable"; +import baseBundledBinariesDirectoryInjectable from "../../common/vars/base-bundled-binaries-dir.injectable"; const createKubectlInjectable = getInjectable({ id: "create-kubectl", @@ -24,7 +24,7 @@ const createKubectlInjectable = getInjectable({ normalizedDownloadPlatform: di.inject(normalizedPlatformInjectable), kubectlBinaryName: di.inject(kubectlBinaryNameInjectable), bundledKubectlBinaryPath: di.inject(bundledKubectlBinaryPathInjectable), - baseBundeledBinariesDirectory: di.inject(baseBundeledBinariesDirectoryInjectable), + baseBundeledBinariesDirectory: di.inject(baseBundledBinariesDirectoryInjectable), }; return (clusterVersion: string) => new Kubectl(dependencies, clusterVersion);