Skip to content

Commit e4b831b

Browse files
committed
fix(cli): resolve --api-key option being ignored in mon local-install
The --api-key option was defined both globally on the program and locally on the local-install subcommand. Commander.js routes the option to the global definition, leaving opts.apiKey undefined in the subcommand. Fix by checking program.opts().apiKey as fallback for the subcommand's opts.apiKey.
1 parent b9e4aeb commit e4b831b

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

cli/bin/postgres-ai.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,11 @@ mon
11741174
.option("--tag <tag>", "Docker image tag to use (e.g., 0.14.0, 0.14.0-dev.33)")
11751175
.option("-y, --yes", "accept all defaults and skip interactive prompts", false)
11761176
.action(async (opts: { demo: boolean; apiKey?: string; dbUrl?: string; tag?: string; yes: boolean }) => {
1177+
// Get apiKey from global program options (--api-key is defined globally)
1178+
// This is needed because Commander.js routes --api-key to the global option, not the subcommand's option
1179+
const globalOpts = program.opts<CliOptions>();
1180+
const apiKey = opts.apiKey || globalOpts.apiKey;
1181+
11771182
console.log("\n=================================");
11781183
console.log(" PostgresAI monitoring local install");
11791184
console.log("=================================\n");
@@ -1226,7 +1231,7 @@ mon
12261231
opts.dbUrl = undefined;
12271232
}
12281233

1229-
if (opts.demo && opts.apiKey) {
1234+
if (opts.demo && apiKey) {
12301235
console.error("✗ Cannot use --api-key with --demo mode");
12311236
console.error("✗ Demo mode is for testing only and does not support API key integration");
12321237
console.error("\nUse demo mode without API key: postgres-ai mon local-install --demo");
@@ -1248,11 +1253,11 @@ mon
12481253
console.log("Step 1: Postgres AI API Configuration (Optional)");
12491254
console.log("An API key enables automatic upload of PostgreSQL reports to Postgres AI\n");
12501255

1251-
if (opts.apiKey) {
1256+
if (apiKey) {
12521257
console.log("Using API key provided via --api-key parameter");
1253-
config.writeConfig({ apiKey: opts.apiKey });
1258+
config.writeConfig({ apiKey: apiKey });
12541259
// Keep reporter compatibility (docker-compose mounts .pgwatch-config)
1255-
fs.writeFileSync(path.resolve(projectDir, ".pgwatch-config"), `api_key=${opts.apiKey}\n`, {
1260+
fs.writeFileSync(path.resolve(projectDir, ".pgwatch-config"), `api_key=${apiKey}\n`, {
12561261
encoding: "utf8",
12571262
mode: 0o600
12581263
});

0 commit comments

Comments
 (0)