Skip to content

Commit

Permalink
feat: support to change config path (#1009)
Browse files Browse the repository at this point in the history
  • Loading branch information
marknguyen1302 authored Aug 13, 2024
1 parent f38d9dc commit 71ed05e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type ServeOptions = {
dataFolder?: string;
version?: boolean;
name?: string;
configPath?: string;
enginePort?: string;
};

Expand Down Expand Up @@ -67,6 +68,12 @@ export class CortexCommand extends CommandRunner {
}

async run(passedParams: string[], options?: ServeOptions): Promise<void> {
if (options?.configPath) {
fileManagerService.setConfigPath(options.configPath);
}
if (options?.name) {
fileManagerService.setConfigProfile(options.name);
}
if (options?.name) {
const isProfileConfigExists = fileManagerService.profileConfigExists(
options.name,
Expand Down Expand Up @@ -192,13 +199,21 @@ export class CortexCommand extends CommandRunner {
}

@Option({
flags: '-df, --dataFolder <dataFolderPath>',
flags: '-df, --dataFolder <dataFolder>',
description: 'Set the data folder directory',
})
parseDataFolder(value: string) {
return value;
}

@Option({
flags: '-cp, --configPath <configPath>',
description: 'Set the config folder directory',
})
parseConfigFolder(value: string) {
return value;
}

@Option({
flags: '-v, --version',
description: 'Show version',
Expand All @@ -212,7 +227,6 @@ export class CortexCommand extends CommandRunner {
description: 'Name of the process',
})
parseName(value: string) {
fileManagerService.setConfigProfile(value);
return value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Config & object> {
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;
}

Expand All @@ -72,9 +76,8 @@ export class FileManagerService {
}

async writeConfigFile(config: Config & object): Promise<void> {
const homeDir = os.homedir();
const configPath = join(
homeDir,
this.configPath,
this.getConfigFileName(this.configProfile),
);

Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -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();
1 change: 1 addition & 0 deletions cortex-js/src/usecases/cortex/cortex.usecases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
});
Expand Down

0 comments on commit 71ed05e

Please sign in to comment.