From 71ed05e9ca15f85c3c2082bf6331395b105324d2 Mon Sep 17 00:00:00 2001 From: marknguyen1302 Date: Tue, 13 Aug 2024 14:26:28 +0700 Subject: [PATCH] feat: support to change config path (#1009) --- .../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..f03d9e242 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; + configPath?: string; enginePort?: string; }; @@ -67,6 +68,12 @@ export class CortexCommand extends CommandRunner { } async run(passedParams: string[], options?: ServeOptions): Promise { + if (options?.configPath) { + fileManagerService.setConfigPath(options.configPath); + } + 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, --configPath ', + 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..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 @@ -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 setConfigPath(configPath: string) { + this.configPath = configPath; + } + + 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, }, });