From c582f12730050e2cf7e82857edce680fd849501c Mon Sep 17 00:00:00 2001 From: Ivo Yankov Date: Wed, 18 Dec 2024 17:32:22 +0200 Subject: [PATCH] wip: refactoring dependencies Signed-off-by: Ivo Yankov --- package-lock.json | 191 ++---------------- package.json | 1 + src/commands/base.ts | 6 +- src/core/container_init.ts | 43 ++++ .../dependency_managers/dependency_manager.ts | 20 +- .../helm_dependency_manager.ts | 20 +- src/core/helm.ts | 8 +- src/core/logging.ts | 2 + src/core/package_downloader.ts | 8 +- src/core/shell_runner.ts | 8 +- src/core/zippy.ts | 8 +- src/index.ts | 18 +- test/e2e/integration/commands/init.test.ts | 14 +- .../integration/core/chart_manager.test.ts | 3 +- .../helm_dependency_manager.test.ts | 20 +- test/test_util.ts | 20 +- test/unit/commands/base.test.ts | 10 +- test/unit/commands/cluster.test.ts | 3 +- test/unit/commands/network.test.ts | 3 +- .../dependency_manager.test.ts | 8 +- test/unit/core/helm.test.ts | 2 +- test/unit/core/shell_runner.test.ts | 2 +- 22 files changed, 156 insertions(+), 262 deletions(-) create mode 100644 src/core/container_init.ts diff --git a/package-lock.json b/package-lock.json index ca6d4930b..261531d9d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -39,6 +39,7 @@ "stream-buffers": "^3.0.3", "tar": "^7.4.3", "tsx": "^4.19.2", + "tsyringe-neo": "^5.1.0", "uuid": "^11.0.3", "validator": "^13.12.0", "winston": "^3.17.0", @@ -2880,29 +2881,6 @@ "typescript": ">=4.8.4 <5.8.0" } }, - "node_modules/@typescript-eslint/eslint-plugin/node_modules/@typescript-eslint/utils": { - "version": "8.18.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/utils/-/utils-8.18.0.tgz", - "integrity": "sha512-p6GLdY383i7h5b0Qrfbix3Vc3+J2k6QWw6UMUeY5JGfm3C5LbZ4QIZzJNoNOfgyRe0uuYKjvVOsO/jD4SJO+xg==", - "dev": true, - "dependencies": { - "@eslint-community/eslint-utils": "^4.4.0", - "@typescript-eslint/scope-manager": "8.18.0", - "@typescript-eslint/types": "8.18.0", - "@typescript-eslint/typescript-estree": "8.18.0" - }, - "engines": { - "node": "^18.18.0 || ^20.9.0 || >=21.1.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/typescript-eslint" - }, - "peerDependencies": { - "eslint": "^8.57.0 || ^9.0.0", - "typescript": ">=4.8.4 <5.8.0" - } - }, "node_modules/@typescript-eslint/parser": { "version": "8.18.1", "resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-8.18.1.tgz", @@ -2967,29 +2945,6 @@ "typescript": ">=4.8.4 <5.8.0" } }, - "node_modules/@typescript-eslint/type-utils/node_modules/@typescript-eslint/utils": { - "version": "8.18.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/utils/-/utils-8.18.0.tgz", - "integrity": "sha512-p6GLdY383i7h5b0Qrfbix3Vc3+J2k6QWw6UMUeY5JGfm3C5LbZ4QIZzJNoNOfgyRe0uuYKjvVOsO/jD4SJO+xg==", - "dev": true, - "dependencies": { - "@eslint-community/eslint-utils": "^4.4.0", - "@typescript-eslint/scope-manager": "8.18.0", - "@typescript-eslint/types": "8.18.0", - "@typescript-eslint/typescript-estree": "8.18.0" - }, - "engines": { - "node": "^18.18.0 || ^20.9.0 || >=21.1.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/typescript-eslint" - }, - "peerDependencies": { - "eslint": "^8.57.0 || ^9.0.0", - "typescript": ">=4.8.4 <5.8.0" - } - }, "node_modules/@typescript-eslint/types": { "version": "8.18.1", "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-8.18.1.tgz", @@ -3076,115 +3031,6 @@ "typescript": ">=4.8.4 <5.8.0" } }, - "node_modules/@typescript-eslint/utils/node_modules/@typescript-eslint/scope-manager": { - "version": "8.18.1", - "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-8.18.1.tgz", - "integrity": "sha512-HxfHo2b090M5s2+/9Z3gkBhI6xBH8OJCFjH9MhQ+nnoZqxU3wNxkLT+VWXWSFWc3UF3Z+CfPAyqdCTdoXtDPCQ==", - "dev": true, - "dependencies": { - "@typescript-eslint/types": "8.18.1", - "@typescript-eslint/visitor-keys": "8.18.1" - }, - "engines": { - "node": "^18.18.0 || ^20.9.0 || >=21.1.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/typescript-eslint" - } - }, - "node_modules/@typescript-eslint/utils/node_modules/@typescript-eslint/types": { - "version": "8.18.1", - "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-8.18.1.tgz", - "integrity": "sha512-7uoAUsCj66qdNQNpH2G8MyTFlgerum8ubf21s3TSM3XmKXuIn+H2Sifh/ES2nPOPiYSRJWAk0fDkW0APBWcpfw==", - "dev": true, - "engines": { - "node": "^18.18.0 || ^20.9.0 || >=21.1.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/typescript-eslint" - } - }, - "node_modules/@typescript-eslint/utils/node_modules/@typescript-eslint/typescript-estree": { - "version": "8.18.1", - "resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-8.18.1.tgz", - "integrity": "sha512-z8U21WI5txzl2XYOW7i9hJhxoKKNG1kcU4RzyNvKrdZDmbjkmLBo8bgeiOJmA06kizLI76/CCBAAGlTlEeUfyg==", - "dev": true, - "dependencies": { - "@typescript-eslint/types": "8.18.1", - "@typescript-eslint/visitor-keys": "8.18.1", - "debug": "^4.3.4", - "fast-glob": "^3.3.2", - "is-glob": "^4.0.3", - "minimatch": "^9.0.4", - "semver": "^7.6.0", - "ts-api-utils": "^1.3.0" - }, - "engines": { - "node": "^18.18.0 || ^20.9.0 || >=21.1.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/typescript-eslint" - }, - "peerDependencies": { - "typescript": ">=4.8.4 <5.8.0" - } - }, - "node_modules/@typescript-eslint/utils/node_modules/@typescript-eslint/visitor-keys": { - "version": "8.18.1", - "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-8.18.1.tgz", - "integrity": "sha512-Vj0WLm5/ZsD013YeUKn+K0y8p1M0jPpxOkKdbD1wB0ns53a5piVY02zjf072TblEweAbcYiFiPoSMF3kp+VhhQ==", - "dev": true, - "dependencies": { - "@typescript-eslint/types": "8.18.1", - "eslint-visitor-keys": "^4.2.0" - }, - "engines": { - "node": "^18.18.0 || ^20.9.0 || >=21.1.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/typescript-eslint" - } - }, - "node_modules/@typescript-eslint/utils/node_modules/brace-expansion": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz", - "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==", - "dev": true, - "dependencies": { - "balanced-match": "^1.0.0" - } - }, - "node_modules/@typescript-eslint/utils/node_modules/eslint-visitor-keys": { - "version": "4.2.0", - "resolved": "https://registry.npmjs.org/eslint-visitor-keys/-/eslint-visitor-keys-4.2.0.tgz", - "integrity": "sha512-UyLnSehNt62FFhSwjZlHmeokpRK59rcz29j+F1/aDgbkbRTk7wIc9XzdoasMUbRNKDM0qQt/+BJ4BrpFeABemw==", - "dev": true, - "engines": { - "node": "^18.18.0 || ^20.9.0 || >=21.1.0" - }, - "funding": { - "url": "https://opencollective.com/eslint" - } - }, - "node_modules/@typescript-eslint/utils/node_modules/minimatch": { - "version": "9.0.5", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-9.0.5.tgz", - "integrity": "sha512-G6T0ZX48xgozx7587koeX9Ys2NYy6Gmv//P89sEte9V9whIapMNF4idKxnW2QtCcLiTWlb/wfCabAtAFWhhBow==", - "dev": true, - "dependencies": { - "brace-expansion": "^2.0.1" - }, - "engines": { - "node": ">=16 || 14 >=14.17" - }, - "funding": { - "url": "https://github.com/sponsors/isaacs" - } - }, "node_modules/@typescript-eslint/visitor-keys": { "version": "8.18.1", "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-8.18.1.tgz", @@ -12266,6 +12112,18 @@ "node": ">= 6.0.0" } }, + "node_modules/tsyringe-neo": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/tsyringe-neo/-/tsyringe-neo-5.1.0.tgz", + "integrity": "sha512-x6QAckY85YLGE980TIR5Xle3s76nhBNYu0kr6UlLAWiPqt4S6PgbbvNV4cjZlItv3ka8adcGYG2Wy/I2yxcqdg==", + "license": "MIT", + "dependencies": { + "tslib": "^2.6.3" + }, + "engines": { + "node": ">= 18.0.0" + } + }, "node_modules/tsyringe/node_modules/tslib": { "version": "1.14.1", "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz", @@ -12482,29 +12340,6 @@ "typescript": ">=4.8.4 <5.8.0" } }, - "node_modules/typescript-eslint/node_modules/@typescript-eslint/utils": { - "version": "8.18.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/utils/-/utils-8.18.0.tgz", - "integrity": "sha512-p6GLdY383i7h5b0Qrfbix3Vc3+J2k6QWw6UMUeY5JGfm3C5LbZ4QIZzJNoNOfgyRe0uuYKjvVOsO/jD4SJO+xg==", - "dev": true, - "dependencies": { - "@eslint-community/eslint-utils": "^4.4.0", - "@typescript-eslint/scope-manager": "8.18.0", - "@typescript-eslint/types": "8.18.0", - "@typescript-eslint/typescript-estree": "8.18.0" - }, - "engines": { - "node": "^18.18.0 || ^20.9.0 || >=21.1.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/typescript-eslint" - }, - "peerDependencies": { - "eslint": "^8.57.0 || ^9.0.0", - "typescript": ">=4.8.4 <5.8.0" - } - }, "node_modules/uc.micro": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/uc.micro/-/uc.micro-2.1.0.tgz", diff --git a/package.json b/package.json index 7defc4aa2..2c31df2b2 100644 --- a/package.json +++ b/package.json @@ -71,6 +71,7 @@ "stream-buffers": "^3.0.3", "tar": "^7.4.3", "tsx": "^4.19.2", + "tsyringe-neo": "^5.1.0", "uuid": "^11.0.3", "validator": "^13.12.0", "winston": "^3.17.0", diff --git a/src/commands/base.ts b/src/commands/base.ts index 2b26a13ef..6543950cc 100644 --- a/src/commands/base.ts +++ b/src/commands/base.ts @@ -31,6 +31,7 @@ import {type CommandFlag} from '../types/flag_types.js'; import {type Lease} from '../core/lease/lease.js'; import {Listr} from 'listr2'; import path from 'path'; +import {container, injectable} from "tsyringe-neo"; export interface CommandHandlers { parent: BaseCommand; @@ -48,7 +49,7 @@ export abstract class BaseCommand extends ShellRunner { protected readonly remoteConfigManager: RemoteConfigManager; constructor(opts: Opts) { - if (!opts || !opts.logger) throw new Error('An instance of core/SoloLogger is required'); + // if (!opts || !opts.logger) throw new Error('An instance of core/SoloLogger is required'); if (!opts || !opts.helm) throw new Error('An instance of core/Helm is required'); if (!opts || !opts.k8) throw new Error('An instance of core/K8 is required'); if (!opts || !opts.chartManager) throw new Error('An instance of core/ChartManager is required'); @@ -57,8 +58,7 @@ export abstract class BaseCommand extends ShellRunner { if (!opts || !opts.localConfig) throw new Error('An instance of core/LocalConfig is required'); if (!opts || !opts.remoteConfigManager) throw new Error('An instance of core/config/RemoteConfigManager is required'); - - super(opts.logger); + super(); this.helm = opts.helm; this.k8 = opts.k8; diff --git a/src/core/container_init.ts b/src/core/container_init.ts new file mode 100644 index 000000000..c4c41c36c --- /dev/null +++ b/src/core/container_init.ts @@ -0,0 +1,43 @@ +import {container} from "tsyringe-neo"; +import {SoloLogger} from "./logging.js"; +import {PackageDownloader} from "./package_downloader.js"; +import {Zippy} from "./zippy.js"; +import {DependencyManager, HelmDependencyManager} from "./dependency_managers/index.js"; +import * as constants from "./constants.js"; +import {Helm} from "./helm.js"; +import {ChartManager} from "./chart_manager.js"; +import {ConfigManager} from "./config_manager.js"; +import {K8} from "./k8.js"; +import {AccountManager} from "./account_manager.js"; +import {PlatformInstaller} from "./platform_installer.js"; +import {KeyManager} from "./key_manager.js"; +import {ProfileManager} from "./profile_manager.js"; +import type {LeaseRenewalService} from "./lease/lease.js"; +import {IntervalLeaseRenewalService} from "./lease/interval_lease_renewal.js"; +import {LeaseManager} from "./lease/lease_manager.js"; +import {CertificateManager} from "./certificate_manager.js"; +import path from "path"; +import {LocalConfig} from "./config/local_config.js"; +import {RemoteConfigManager} from "./config/remote/remote_config_manager.js"; + +container.register(SoloLogger, {useValue: new SoloLogger('debug', false)}); +container.register(PackageDownloader, {useValue: new PackageDownloader()}); +container.register(Zippy, {useValue: new Zippy()}); +container.register(HelmDependencyManager, {useValue: new HelmDependencyManager()}); +container.register(DependencyManager, {useValue: new DependencyManager()}); +container.register(Helm, {useValue: new Helm()}); + + +// const chartManager = new ChartManager(helm, logger); +// const configManager = new ConfigManager(logger); +// const k8 = new K8(configManager, logger); +// const accountManager = new AccountManager(logger, k8); +// const platformInstaller = new PlatformInstaller(logger, k8, configManager); +// const keyManager = new KeyManager(logger); +// const profileManager = new ProfileManager(logger, configManager); +// const leaseRenewalService: LeaseRenewalService = new IntervalLeaseRenewalService(); +// const leaseManager = new LeaseManager(k8, configManager, logger, leaseRenewalService); +// const certificateManager = new CertificateManager(k8, logger, configManager); +// const localConfigPath = path.join(constants.SOLO_CACHE_DIR, constants.DEFAULT_LOCAL_CONFIG_FILE); +// const localConfig = new LocalConfig(localConfigPath, logger, configManager); +// const remoteConfigManager = new RemoteConfigManager(k8, logger, localConfig, configManager); diff --git a/src/core/dependency_managers/dependency_manager.ts b/src/core/dependency_managers/dependency_manager.ts index 4e3981956..48cad96f4 100644 --- a/src/core/dependency_managers/dependency_manager.ts +++ b/src/core/dependency_managers/dependency_manager.ts @@ -15,20 +15,20 @@ * */ import os from 'os'; -import {SoloError, MissingArgumentError} from '../errors.js'; +import {SoloError} from '../errors.js'; import {ShellRunner} from '../shell_runner.js'; -import {type SoloLogger} from '../logging.js'; -import {type HelmDependencyManager} from './helm_dependency_manager.js'; +import {HelmDependencyManager} from './helm_dependency_manager.js'; import {type ListrTask} from 'listr2'; +import {autoInjectable} from "tsyringe-neo"; +import * as constants from "../constants.js"; +@autoInjectable() export class DependencyManager extends ShellRunner { - constructor( - logger: SoloLogger, - private readonly depManagerMap: Map, - ) { - if (!logger) throw new MissingArgumentError('an instance of core/SoloLogger is required', logger); - super(logger); - if (!depManagerMap) throw new MissingArgumentError('A map of dependency managers are required'); + private readonly depManagerMap: Map; + + constructor(helmDepManager?: HelmDependencyManager) { + super(); + this.depManagerMap = new Map().set(constants.HELM, helmDepManager); } /** diff --git a/src/core/dependency_managers/helm_dependency_manager.ts b/src/core/dependency_managers/helm_dependency_manager.ts index 120dd6904..92f76fcd3 100644 --- a/src/core/dependency_managers/helm_dependency_manager.ts +++ b/src/core/dependency_managers/helm_dependency_manager.ts @@ -21,14 +21,15 @@ import * as util from 'util'; import {IllegalArgumentError, MissingArgumentError} from '../errors.js'; import * as helpers from '../helpers.js'; import * as constants from '../constants.js'; -import {type PackageDownloader} from '../package_downloader.js'; -import {type Zippy} from '../zippy.js'; +import {PackageDownloader} from '../package_downloader.js'; +import {Zippy} from '../zippy.js'; import {Templates} from '../templates.js'; import * as version from '../../../version.js'; import {ShellRunner} from '../shell_runner.js'; import * as semver from 'semver'; import {OS_WIN32, OS_WINDOWS} from '../constants.js'; import {type SoloLogger} from '../logging.js'; +import {autoInjectable, container, singleton} from "tsyringe-neo"; // constants required by HelmDependencyManager const HELM_RELEASE_BASE_URL = 'https://get.helm.sh'; @@ -42,6 +43,7 @@ const HELM_ARTIFACT_EXT: Map = new Map() /** * Helm dependency manager installs or uninstalls helm client at SOLO_HOME_DIR/bin directory */ +@singleton() export class HelmDependencyManager extends ShellRunner { private readonly osPlatform: string; private readonly osArch: string; @@ -49,25 +51,21 @@ export class HelmDependencyManager extends ShellRunner { private readonly artifactName: string; private readonly helmURL: string; private readonly checksumURL: string; + private readonly downloader; + private readonly zippy; constructor( - private readonly downloader: PackageDownloader, - private readonly zippy: Zippy, - logger: SoloLogger, private readonly installationDir = path.join(constants.SOLO_HOME_DIR, 'bin'), osPlatform = os.platform(), osArch = os.arch(), private readonly helmVersion = version.HELM_VERSION, ) { - super(logger); + super(); - if (!downloader) throw new MissingArgumentError('An instance of core/PackageDownloader is required'); - if (!zippy) throw new MissingArgumentError('An instance of core/Zippy is required'); - if (!logger) throw new IllegalArgumentError('an instance of core/SoloLogger is required', logger); if (!installationDir) throw new MissingArgumentError('installation directory is required'); - this.downloader = downloader; - this.zippy = zippy; + this.downloader = container.resolve(PackageDownloader); + this.zippy = container.resolve(Zippy); this.installationDir = installationDir; // Node.js uses 'win32' for windows in package.json os field, but helm uses 'windows' if (osPlatform === OS_WIN32) { diff --git a/src/core/helm.ts b/src/core/helm.ts index b2cec27f8..33bf885af 100644 --- a/src/core/helm.ts +++ b/src/core/helm.ts @@ -18,18 +18,16 @@ import os from 'os'; import * as constants from './constants.js'; import {ShellRunner} from './shell_runner.js'; import {Templates} from './templates.js'; -import {IllegalArgumentError} from './errors.js'; -import type {SoloLogger} from './logging.js'; +import {singleton} from "tsyringe-neo"; +@singleton() export class Helm extends ShellRunner { private readonly helmPath: string; constructor( - logger: SoloLogger, private readonly osPlatform: NodeJS.Platform = os.platform(), ) { - if (!logger) throw new IllegalArgumentError('an instance of core/SoloLogger is required', logger); - super(logger); + super(); this.helmPath = Templates.installationPath(constants.HELM, this.osPlatform); } diff --git a/src/core/logging.ts b/src/core/logging.ts index 46c71f4d3..ac6f25366 100644 --- a/src/core/logging.ts +++ b/src/core/logging.ts @@ -20,6 +20,7 @@ import * as util from 'util'; import chalk from 'chalk'; import path from 'path'; import * as constants from './constants.js'; +import {injectable, singleton} from "tsyringe-neo"; const customFormat = winston.format.combine( winston.format.label({label: 'SOLO', message: false}), @@ -47,6 +48,7 @@ const customFormat = winston.format.combine( winston.format(data => (data.private ? false : data))(), ); +@singleton() export class SoloLogger { private winstonLogger: winston.Logger; private traceId?: string; diff --git a/src/core/package_downloader.ts b/src/core/package_downloader.ts index 5abd01b13..496b23a66 100644 --- a/src/core/package_downloader.ts +++ b/src/core/package_downloader.ts @@ -30,13 +30,13 @@ import * as https from 'https'; import * as http from 'http'; import {Templates} from './templates.js'; import * as constants from './constants.js'; -import {type SoloLogger} from './logging.js'; +import {SoloLogger} from './logging.js'; import {StatusCodes} from 'http-status-codes'; +import {autoInjectable} from "tsyringe-neo"; +@autoInjectable() export class PackageDownloader { - constructor(public readonly logger: SoloLogger) { - if (!logger) throw new IllegalArgumentError('an instance of core/SoloLogger is required', logger); - } + constructor(public readonly logger?: SoloLogger) {} isValidURL(url: string) { try { diff --git a/src/core/shell_runner.ts b/src/core/shell_runner.ts index dc19f18c0..64844d82a 100644 --- a/src/core/shell_runner.ts +++ b/src/core/shell_runner.ts @@ -16,12 +16,12 @@ */ import {spawn} from 'child_process'; import chalk from 'chalk'; -import {type SoloLogger} from './logging.js'; +import {SoloLogger} from './logging.js'; +import {autoInjectable} from "tsyringe-neo"; +@autoInjectable() export class ShellRunner { - constructor(public logger: SoloLogger) { - if (!logger) throw new Error('An instance of core/SoloLogger is required'); - } + constructor(public logger?: SoloLogger) {} /** Returns a promise that invokes the shell command */ run(cmd: string, verbose = false) { diff --git a/src/core/zippy.ts b/src/core/zippy.ts index 152258df0..303e6926a 100644 --- a/src/core/zippy.ts +++ b/src/core/zippy.ts @@ -20,12 +20,12 @@ import AdmZip from 'adm-zip'; import * as tar from 'tar'; import chalk from 'chalk'; import path from 'path'; -import type {SoloLogger} from './logging.js'; +import {SoloLogger} from './logging.js'; +import {autoInjectable} from "tsyringe-neo"; +@autoInjectable() export class Zippy { - constructor(private readonly logger: SoloLogger) { - if (!logger) throw new Error('An instance of core/SoloLogger is required'); - } + constructor(private readonly logger?: SoloLogger) {} /** * Zip a file or directory diff --git a/src/index.ts b/src/index.ts index 386fa8639..618d9e0d8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,6 +19,8 @@ import yargs from 'yargs'; import {hideBin} from 'yargs/helpers'; import 'dotenv/config'; import path from 'path'; +import "reflect-metadata"; +import './core/container_init.js' import {ListrLogger} from 'listr2'; import {Flags as flags} from './commands/flags.js'; @@ -38,16 +40,17 @@ import {LeaseManager} from './core/lease/lease_manager.js'; import {CertificateManager} from './core/certificate_manager.js'; import {LocalConfig} from './core/config/local_config.js'; import {RemoteConfigManager} from './core/config/remote/remote_config_manager.js'; -import * as logging from './core/logging.js'; import * as helpers from './core/helpers.js'; import {K8} from './core/k8.js'; import {CustomProcessOutput} from './core/process_output.js'; import {type Opts} from './types/command_types.js'; import {IntervalLeaseRenewalService} from './core/lease/interval_lease_renewal.js'; import {type LeaseRenewalService} from './core/lease/lease.js'; +import {container} from "tsyringe-neo"; +import {SoloLogger} from "./core/logging.js"; export function main(argv: any) { - const logger = logging.NewLogger('debug'); + const logger = container.resolve(SoloLogger) constants.LISTR_DEFAULT_RENDERER_OPTION.logger = new ListrLogger({processOutput: new CustomProcessOutput(logger)}); if (argv.length >= 3 && ['-version', '--version', '-v', '--v'].includes(argv[2])) { logger.showUser(chalk.cyan('\n******************************* Solo *********************************************')); @@ -58,13 +61,12 @@ export function main(argv: any) { try { // prepare dependency manger registry - const downloader = new PackageDownloader(logger); - const zippy = new Zippy(logger); - const helmDepManager = new HelmDependencyManager(downloader, zippy, logger); - const depManagerMap = new Map().set(constants.HELM, helmDepManager); - const depManager = new DependencyManager(logger, depManagerMap); + const downloader = container.resolve(PackageDownloader) + const zippy = container.resolve(Zippy) + const helmDepManager = container.resolve(HelmDependencyManager) + const depManager = container.resolve(DependencyManager) + const helm = container.resolve(Helm) - const helm = new Helm(logger); const chartManager = new ChartManager(helm, logger); const configManager = new ConfigManager(logger); const k8 = new K8(configManager, logger); diff --git a/test/e2e/integration/commands/init.test.ts b/test/e2e/integration/commands/init.test.ts index 64272c0f3..5ce3506ca 100644 --- a/test/e2e/integration/commands/init.test.ts +++ b/test/e2e/integration/commands/init.test.ts @@ -36,17 +36,19 @@ import {IntervalLeaseRenewalService} from '../../../../src/core/lease/interval_l import path from 'path'; import {BASE_TEST_DIR} from '../../../test_util.js'; import {Duration} from '../../../../src/core/time/duration.js'; +import {container} from "tsyringe-neo"; const testLogger = logging.NewLogger('debug', true); describe('InitCommand', () => { // prepare dependency manger registry - const downloader = new PackageDownloader(testLogger); - const zippy = new Zippy(testLogger); - const helmDepManager = new HelmDependencyManager(downloader, zippy, testLogger); - const depManagerMap = new Map().set(constants.HELM, helmDepManager); - const depManager = new DependencyManager(testLogger, depManagerMap); + // const downloader = new PackageDownloader(testLogger); + // const zippy = new Zippy(testLogger); + // const helmDepManager = new HelmDependencyManager(downloader, zippy, testLogger); + + // const depManager = new DependencyManager(helmDepManager); + const depManager = container.resolve(DependencyManager); + const helm = container.resolve(Helm); - const helm = new Helm(testLogger); const chartManager = new ChartManager(helm, testLogger); const configManager = new ConfigManager(testLogger); let k8: K8; diff --git a/test/e2e/integration/core/chart_manager.test.ts b/test/e2e/integration/core/chart_manager.test.ts index 79d9d5e83..f74e3ea94 100644 --- a/test/e2e/integration/core/chart_manager.test.ts +++ b/test/e2e/integration/core/chart_manager.test.ts @@ -21,9 +21,10 @@ import * as constants from '../../../../src/core/constants.js'; import {Helm} from '../../../../src/core/helm.js'; import {ChartManager} from '../../../../src/core/chart_manager.js'; import {testLogger} from '../../../test_util.js'; +import {container} from "tsyringe-neo"; describe('ChartManager', () => { - const helm = new Helm(testLogger); + const helm = container.resolve(Helm); const chartManager = new ChartManager(helm, testLogger); it('should be able to list installed charts', async () => { diff --git a/test/e2e/integration/core/dependency_managers/helm_dependency_manager.test.ts b/test/e2e/integration/core/dependency_managers/helm_dependency_manager.test.ts index 0a360e275..188aa784c 100644 --- a/test/e2e/integration/core/dependency_managers/helm_dependency_manager.test.ts +++ b/test/e2e/integration/core/dependency_managers/helm_dependency_manager.test.ts @@ -40,17 +40,20 @@ describe('HelmDependencyManager', () => { }); it('should return helm version', () => { - const helmDependencyManager = new HelmDependencyManager(downloader, zippy, testLogger, tmpDir); + // const helmDependencyManager = new HelmDependencyManager(downloader, zippy, testLogger, tmpDir); + const helmDependencyManager = new HelmDependencyManager(tmpDir); expect(helmDependencyManager.getHelmVersion()).to.equal(version.HELM_VERSION); }); it('should be able to check when helm not installed', () => { - const helmDependencyManager = new HelmDependencyManager(downloader, zippy, testLogger, tmpDir); + // const helmDependencyManager = new HelmDependencyManager(downloader, zippy, testLogger, tmpDir); + const helmDependencyManager = new HelmDependencyManager(tmpDir); expect(helmDependencyManager.isInstalled()).not.to.be.ok; }); it('should be able to check when helm is installed', () => { - const helmDependencyManager = new HelmDependencyManager(downloader, zippy, testLogger, tmpDir); + // const helmDependencyManager = new HelmDependencyManager(downloader, zippy, testLogger, tmpDir); + const helmDependencyManager = new HelmDependencyManager(tmpDir); fs.writeFileSync(helmDependencyManager.getHelmPath(), ''); expect(helmDependencyManager.isInstalled()).to.be.ok; }); @@ -62,10 +65,15 @@ describe('HelmDependencyManager', () => { // { osRelease: 'linux', osArch: 'amd64' }, // { osRelease: 'windows', osArch: 'amd64' } ]).it('should be able to install helm base on os and architecture', async input => { + // const helmDependencyManager = new HelmDependencyManager( + // downloader, + // zippy, + // testLogger, + // tmpDir, + // input.osPlatform, + // input.osArch, + // ); const helmDependencyManager = new HelmDependencyManager( - downloader, - zippy, - testLogger, tmpDir, input.osPlatform, input.osArch, diff --git a/test/test_util.ts b/test/test_util.ts index eda7b91c3..9ee7c1555 100644 --- a/test/test_util.ts +++ b/test/test_util.ts @@ -60,6 +60,7 @@ import {Zippy} from '../src/core/zippy.js'; import {HEDERA_PLATFORM_VERSION} from '../version.js'; import {IntervalLeaseRenewalService} from '../src/core/lease/interval_lease_renewal.js'; import {Duration} from '../src/core/time/duration.js'; +import {container} from "tsyringe-neo"; export const testLogger = logging.NewLogger('debug', true); export const TEST_CLUSTER = 'solo-e2e'; @@ -139,13 +140,20 @@ export function bootstrapTestVariables( const cacheDir: string = argv[flags.cacheDir.name] || getTestCacheDir(testName); const configManager = new ConfigManager(testLogger); configManager.update(argv); - const downloader = new PackageDownloader(testLogger); - const zippy = new Zippy(testLogger); - const helmDepManager = new HelmDependencyManager(downloader, zippy, testLogger); - const depManagerMap = new Map().set(constants.HELM, helmDepManager); - const depManager = new DependencyManager(testLogger, depManagerMap); + + // const downloader = new PackageDownloader(testLogger); + // const zippy = new Zippy(testLogger); + + const downloader = container.resolve(PackageDownloader); + + // const helmDepManager = new HelmDependencyManager(downloader, zippy, testLogger); + // const helmDepManager = new HelmDependencyManager(); + // const depManagerMap = new Map().set(constants.HELM, helmDepManager); + // const depManager = new DependencyManager(testLogger, depManagerMap); + const depManager = container.resolve(DependencyManager); + const helm = container.resolve(Helm); + const keyManager = new KeyManager(testLogger); - const helm = new Helm(testLogger); const chartManager = new ChartManager(helm, testLogger); const k8 = k8Arg || new K8(configManager, testLogger); const accountManager = new AccountManager(testLogger, k8); diff --git a/test/unit/commands/base.test.ts b/test/unit/commands/base.test.ts index 2df6be98e..19aa8161a 100644 --- a/test/unit/commands/base.test.ts +++ b/test/unit/commands/base.test.ts @@ -32,20 +32,16 @@ import {Flags as flags} from '../../../src/commands/flags.js'; import sinon from 'sinon'; import path from 'path'; import {BASE_TEST_DIR} from '../../test_util.js'; +import {container} from "tsyringe-neo"; const testLogger = logging.NewLogger('debug', true); describe('BaseCommand', () => { - const helm = new Helm(testLogger); + const helm = container.resolve(Helm); const chartManager = new ChartManager(helm, testLogger); const configManager = new ConfigManager(testLogger); - // prepare dependency manger registry - const downloader = new PackageDownloader(testLogger); - const zippy = new Zippy(testLogger); - const helmDepManager = new HelmDependencyManager(downloader, zippy, testLogger); - const depManagerMap = new Map().set(constants.HELM, helmDepManager); - const depManager = new DependencyManager(testLogger, depManagerMap); + const depManager = container.resolve(DependencyManager); const localConfig = new LocalConfig(path.join(BASE_TEST_DIR, 'local-config.yaml'), testLogger, configManager); const remoteConfigManager = new RemoteConfigManager({} as any, testLogger, localConfig, configManager); diff --git a/test/unit/commands/cluster.test.ts b/test/unit/commands/cluster.test.ts index 5eb726bbc..0e2e52cef 100644 --- a/test/unit/commands/cluster.test.ts +++ b/test/unit/commands/cluster.test.ts @@ -29,6 +29,7 @@ import {ChartManager} from '../../../src/core/chart_manager.js'; import {Helm} from '../../../src/core/helm.js'; import {ROOT_DIR} from '../../../src/core/constants.js'; import path from 'path'; +import {container} from "tsyringe-neo"; const getBaseCommandOpts = () => ({ logger: sinon.stub(), @@ -61,7 +62,7 @@ describe('ClusterCommand unit tests', () => { beforeEach(() => { opts = getBaseCommandOpts(); opts.logger = new SoloLogger(); - opts.helm = new Helm(opts.logger); + opts.helm = container.resolve(Helm); opts.helm.dependency = sinon.stub(); opts.chartManager = new ChartManager(opts.helm, opts.logger); opts.chartManager.isChartInstalled = sinon.stub().returns(false); diff --git a/test/unit/commands/network.test.ts b/test/unit/commands/network.test.ts index 71b2549d6..ae584a817 100644 --- a/test/unit/commands/network.test.ts +++ b/test/unit/commands/network.test.ts @@ -40,6 +40,7 @@ import {ProfileManager} from '../../../src/core/profile_manager.js'; import {KeyManager} from '../../../src/core/key_manager.js'; import {ROOT_DIR} from '../../../src/core/constants.js'; import {ListrLease} from '../../../src/core/lease/listr_lease.js'; +import {container} from "tsyringe-neo"; const getBaseCommandOpts = () => ({ logger: sinon.stub(), @@ -75,7 +76,7 @@ describe('NetworkCommand unit tests', () => { beforeEach(() => { opts = getBaseCommandOpts(); opts.logger = testLogger; - opts.helm = new Helm(opts.logger); + opts.helm = container.resolve(Helm); opts.helm.dependency = sinon.stub(); opts.configManager = new ConfigManager(testLogger); diff --git a/test/unit/core/dependency_managers/dependency_manager.test.ts b/test/unit/core/dependency_managers/dependency_manager.test.ts index 5e4274f79..e3944b340 100644 --- a/test/unit/core/dependency_managers/dependency_manager.test.ts +++ b/test/unit/core/dependency_managers/dependency_manager.test.ts @@ -22,15 +22,13 @@ import {PackageDownloader} from '../../../../src/core/package_downloader.js'; import {Zippy} from '../../../../src/core/zippy.js'; import * as constants from '../../../../src/core/constants.js'; import * as logging from '../../../../src/core/logging.js'; +import {container} from "tsyringe-neo"; const testLogger = logging.NewLogger('debug', true); describe('DependencyManager', () => { // prepare dependency manger registry - const downloader = new PackageDownloader(testLogger); - const zippy = new Zippy(testLogger); - const helmDepManager = new HelmDependencyManager(downloader, zippy, testLogger); - const depManagerMap: Map = new Map().set(constants.HELM, helmDepManager); - const depManager = new DependencyManager(testLogger, depManagerMap); + + const depManager = container.resolve(DependencyManager); describe('checkDependency', () => { it('should fail during invalid dependency check', async () => { diff --git a/test/unit/core/helm.test.ts b/test/unit/core/helm.test.ts index 2abfb2db1..0c944d1e6 100644 --- a/test/unit/core/helm.test.ts +++ b/test/unit/core/helm.test.ts @@ -28,7 +28,7 @@ import {ShellRunner} from '../../../src/core/shell_runner.js'; describe('Helm platform specific tests', () => { each(['linux', 'windows', 'darwin']).describe('Helm on %s platform', osPlatform => { const logger = logging.NewLogger('debug', true); - const helm = new Helm(logger, osPlatform); + const helm = new Helm(osPlatform); let shellStub: sinon.SinonStub<[cmd: string, verbose?: boolean], Promise>; diff --git a/test/unit/core/shell_runner.test.ts b/test/unit/core/shell_runner.test.ts index 886fef943..4abd8d4f1 100644 --- a/test/unit/core/shell_runner.test.ts +++ b/test/unit/core/shell_runner.test.ts @@ -35,7 +35,7 @@ describe('ShellRunner', () => { beforeEach(() => { logger = NewLogger('debug'); - shellRunner = new ShellRunner(logger); + shellRunner = new ShellRunner(); // Spy on methods loggerStub = sinon.stub(SoloLogger.prototype, 'debug');