From 6dc8c67e0fa3a5c79122792adffb89a86c23c856 Mon Sep 17 00:00:00 2001 From: marknguyen1302 Date: Mon, 12 Aug 2024 16:14:25 +0700 Subject: [PATCH 1/2] feat: support to change config path --- .../commanders/cortex-command.commander.ts | 18 +++++++++-- .../file-manager/file-manager.service.ts | 30 ++++++++++++------- .../src/usecases/cortex/cortex.usecases.ts | 1 + 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts b/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts index 98746894c..cc6ddaf84 100644 --- a/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts +++ b/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts @@ -32,6 +32,7 @@ type ServeOptions = { dataFolder?: string; version?: boolean; name?: string; + configFolderPath?: string; enginePort?: string; }; @@ -67,6 +68,12 @@ export class CortexCommand extends CommandRunner { } async run(passedParams: string[], options?: ServeOptions): Promise { + if (options?.configFolderPath) { + fileManagerService.setConfigFolderPath(options.configFolderPath); + } + if (options?.name) { + fileManagerService.setConfigProfile(options.name); + } if (options?.name) { const isProfileConfigExists = fileManagerService.profileConfigExists( options.name, @@ -192,13 +199,21 @@ export class CortexCommand extends CommandRunner { } @Option({ - flags: '-df, --dataFolder ', + flags: '-df, --dataFolder ', description: 'Set the data folder directory', }) parseDataFolder(value: string) { return value; } + @Option({ + flags: '-cp, --configFolderPath ', + description: 'Set the config folder directory', + }) + parseConfigFolder(value: string) { + return value; + } + @Option({ flags: '-v, --version', description: 'Show version', @@ -212,7 +227,6 @@ export class CortexCommand extends CommandRunner { description: 'Name of the process', }) parseName(value: string) { - fileManagerService.setConfigProfile(value); return value; } diff --git a/cortex-js/src/infrastructure/services/file-manager/file-manager.service.ts b/cortex-js/src/infrastructure/services/file-manager/file-manager.service.ts index 5043028cd..815efdd08 100644 --- a/cortex-js/src/infrastructure/services/file-manager/file-manager.service.ts +++ b/cortex-js/src/infrastructure/services/file-manager/file-manager.service.ts @@ -36,22 +36,26 @@ export class FileManagerService { private cortexEnginesFolderName = 'engines'; private cortexTelemetryFolderName = 'telemetry'; private configProfile = process.env.CORTEX_PROFILE || 'default'; + private configPath = process.env.CORTEX_CONFIG_PATH || os.homedir(); /** * Get cortex configs * @returns the config object */ async getConfig(dataFolderPath?: string): Promise { - const homeDir = os.homedir(); const configPath = join( - homeDir, + this.configPath, this.getConfigFileName(this.configProfile), ); const config = this.defaultConfig(); const dataFolderPathUsed = dataFolderPath || config.dataFolderPath; if (!existsSync(configPath) || !existsSync(dataFolderPathUsed)) { - await this.createFolderIfNotExist(dataFolderPathUsed); - await this.writeConfigFile(config); + if (!existsSync(dataFolderPathUsed)) { + await this.createFolderIfNotExist(dataFolderPathUsed); + } + if (!existsSync(configPath)) { + await this.writeConfigFile(config); + } return config; } @@ -72,9 +76,8 @@ export class FileManagerService { } async writeConfigFile(config: Config & object): Promise { - const homeDir = os.homedir(); const configPath = join( - homeDir, + this.configPath, this.getConfigFileName(this.configProfile), ); @@ -348,9 +351,8 @@ export class FileManagerService { * @returns the server configurations */ getServerConfig(): { host: string; port: number } { - const homeDir = os.homedir(); const configPath = join( - homeDir, + this.configPath, this.getConfigFileName(this.configProfile), ); let config = this.defaultConfig(); @@ -370,12 +372,12 @@ export class FileManagerService { public setConfigProfile(profile: string) { this.configProfile = profile; } + public getConfigProfile() { return this.configProfile; } public profileConfigExists(profile: string): boolean { - const homeDir = os.homedir(); - const configPath = join(homeDir, this.getConfigFileName(profile)); + const configPath = join(this.configPath, this.getConfigFileName(profile)); try { const content = readFileSync(configPath, 'utf8'); const config = yaml.load(content) as Config & object; @@ -391,6 +393,14 @@ export class FileManagerService { } return `.${configProfile}rc`; } + + public setConfigFolderPath(configFolderPath: string) { + this.configPath = configFolderPath; + } + + public getConfigPath(): string { + return this.configPath; + } } export const fileManagerService = new FileManagerService(); diff --git a/cortex-js/src/usecases/cortex/cortex.usecases.ts b/cortex-js/src/usecases/cortex/cortex.usecases.ts index 74fbe942e..ac904da53 100644 --- a/cortex-js/src/usecases/cortex/cortex.usecases.ts +++ b/cortex-js/src/usecases/cortex/cortex.usecases.ts @@ -177,6 +177,7 @@ export class CortexUsecases implements BeforeApplicationShutdown { CORTEX_JS_HOST: host, CORTEX_JS_PORT: port.toString(), CORTEX_PROFILE: fileManagerService.getConfigProfile(), + CORTEX_CONFIG_PATH: fileManagerService.getConfigPath(), ...process.env, }, }); From 7d91409f083f3092cd33ee5351df09119412c776 Mon Sep 17 00:00:00 2001 From: marknguyen1302 Date: Tue, 13 Aug 2024 09:19:45 +0700 Subject: [PATCH 2/2] chore: change option name --- .../infrastructure/commanders/cortex-command.commander.ts | 8 ++++---- .../services/file-manager/file-manager.service.ts | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts b/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts index cc6ddaf84..f03d9e242 100644 --- a/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts +++ b/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts @@ -32,7 +32,7 @@ type ServeOptions = { dataFolder?: string; version?: boolean; name?: string; - configFolderPath?: string; + configPath?: string; enginePort?: string; }; @@ -68,8 +68,8 @@ export class CortexCommand extends CommandRunner { } async run(passedParams: string[], options?: ServeOptions): Promise { - if (options?.configFolderPath) { - fileManagerService.setConfigFolderPath(options.configFolderPath); + if (options?.configPath) { + fileManagerService.setConfigPath(options.configPath); } if (options?.name) { fileManagerService.setConfigProfile(options.name); @@ -207,7 +207,7 @@ export class CortexCommand extends CommandRunner { } @Option({ - flags: '-cp, --configFolderPath ', + flags: '-cp, --configPath ', description: 'Set the config folder directory', }) parseConfigFolder(value: string) { diff --git a/cortex-js/src/infrastructure/services/file-manager/file-manager.service.ts b/cortex-js/src/infrastructure/services/file-manager/file-manager.service.ts index 815efdd08..c452133eb 100644 --- a/cortex-js/src/infrastructure/services/file-manager/file-manager.service.ts +++ b/cortex-js/src/infrastructure/services/file-manager/file-manager.service.ts @@ -394,8 +394,8 @@ export class FileManagerService { return `.${configProfile}rc`; } - public setConfigFolderPath(configFolderPath: string) { - this.configPath = configFolderPath; + public setConfigPath(configPath: string) { + this.configPath = configPath; } public getConfigPath(): string {