Support deployment CLI flags

This commit is contained in:
hwhung
2026-04-27 12:27:27 +08:00
parent 878cd1eb3c
commit 554ab93df2
2 changed files with 87 additions and 32 deletions
+56 -2
View File
@@ -116,10 +116,64 @@ function configPathFromArgv(): string {
return path.resolve("config.json");
}
function argValue(name: string): string | undefined {
const longName = `--${name}`;
const index = process.argv.findIndex((arg) => arg === longName);
if (index >= 0) {
const value = process.argv[index + 1];
return value && !value.startsWith("--") ? value : undefined;
}
const inlineArg = process.argv.find((arg) => arg.startsWith(`${longName}=`));
return inlineArg ? inlineArg.slice(longName.length + 1) : undefined;
}
function argFlag(name: string): boolean {
return process.argv.includes(`--${name}`);
}
function applyArgOverrides(base: AppConfig): AppConfig {
const rawPort = argValue("port");
const port = rawPort === undefined ? base.server.port : Number(rawPort);
if (!Number.isInteger(port) || port <= 0) {
throw new Error(`Invalid CLI value "--port": ${rawPort}`);
}
return {
server: {
host: argValue("host") ?? base.server.host,
port,
},
proxy: {
apiKey: argValue("proxy-api-key") ?? base.proxy.apiKey,
},
providers: {
openai: {
...base.providers.openai,
...(argValue("openai-base-url") ? { baseURL: argValue("openai-base-url") } : {}),
...(argValue("openai-api-key") ? { apiKey: argValue("openai-api-key") } : {}),
},
anthropic: {
...base.providers.anthropic,
...(argValue("anthropic-base-url") ? { baseURL: argValue("anthropic-base-url") } : {}),
...(argValue("anthropic-api-key") ? { apiKey: argValue("anthropic-api-key") } : {}),
},
},
logging: {
level: argValue("log-level") ?? base.logging.level,
pretty: argFlag("pretty-logs")
? true
: argFlag("no-pretty-logs")
? false
: base.logging.pretty,
},
};
}
function loadConfig(): AppConfig {
const filePath = configPathFromArgv();
if (!existsSync(filePath)) {
return DEFAULT_CONFIG;
return applyArgOverrides(DEFAULT_CONFIG);
}
let parsed: unknown;
@@ -130,7 +184,7 @@ function loadConfig(): AppConfig {
throw new Error(`Could not read config file ${filePath}: ${message}`);
}
return mergeConfig(parsed);
return applyArgOverrides(mergeConfig(parsed));
}
export const config = loadConfig();