Skip to content

Commit a2c71ac

Browse files
committed
Merge branch 'neo/fix-top-level-login-20260703' into 'main'
fix(cli): support top-level login command Closes #263 See merge request postgres-ai/postgresai!343
2 parents 09fa3b9 + b6dce45 commit a2c71ac

2 files changed

Lines changed: 48 additions & 9 deletions

File tree

cli/bin/postgres-ai.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3931,13 +3931,9 @@ targets
39313931
// Authentication and API key management
39323932
const auth = program.command("auth").description("authentication and API key management");
39333933

3934-
auth
3935-
.command("login", { isDefault: true })
3936-
.description("authenticate via browser (OAuth) or store API key directly")
3937-
.option("--set-key <key>", "store API key directly without OAuth flow")
3938-
.option("--port <port>", "local callback server port (default: random)", parseInt)
3939-
.option("--debug", "enable debug output")
3940-
.action(async (opts: { setKey?: string; port?: number; debug?: boolean }) => {
3934+
type AuthLoginOptions = { setKey?: string; port?: number; debug?: boolean };
3935+
3936+
async function runAuthLogin(opts: AuthLoginOptions) {
39413937
// If --set-key is provided, store it directly without OAuth
39423938
if (opts.setKey) {
39433939
const trimmedKey = opts.setKey.trim();
@@ -4191,7 +4187,21 @@ auth
41914187
console.error(`Authentication error: ${message}`);
41924188
process.exit(1);
41934189
}
4194-
});
4190+
}
4191+
4192+
function configureLoginCommand(command: Command): Command {
4193+
return command
4194+
.description("authenticate via browser (OAuth) or store API key directly")
4195+
.option("--set-key <key>", "store API key directly without OAuth flow")
4196+
.option("--port <port>", "local callback server port (default: random)", parseInt)
4197+
.option("--debug", "enable debug output");
4198+
}
4199+
4200+
configureLoginCommand(auth.command("login", { isDefault: true }))
4201+
.action(runAuthLogin);
4202+
4203+
configureLoginCommand(program.command("login"))
4204+
.action(runAuthLogin);
41954205

41964206
auth
41974207
.command("show-key")

cli/test/auth.test.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { describe, test, expect } from "bun:test";
2-
import { resolve } from "path";
2+
import { mkdtempSync, readFileSync, rmSync } from "fs";
3+
import { tmpdir } from "os";
4+
import { join, resolve } from "path";
35

46
import * as util from "../lib/util";
57
import * as pkce from "../lib/pkce";
@@ -212,6 +214,33 @@ describe("Auth callback server", () => {
212214
});
213215

214216
describe("CLI auth commands", () => {
217+
test("cli: login --help shows all options", () => {
218+
const r = runCli(["login", "--help"]);
219+
expect(r.status).toBe(0);
220+
expect(r.stdout).toMatch(/--set-key/);
221+
expect(r.stdout).toMatch(/--debug/);
222+
});
223+
224+
test("cli: login --set-key aliases auth login", () => {
225+
const home = mkdtempSync(join(tmpdir(), "postgresai-login-"));
226+
227+
try {
228+
const r = runCli(["login", "--set-key", "test-token"], {
229+
HOME: home,
230+
XDG_CONFIG_HOME: join(home, ".config"),
231+
});
232+
expect(r.status).toBe(0);
233+
expect(r.stdout).toMatch(/API key saved/);
234+
235+
const saved = JSON.parse(
236+
readFileSync(join(home, ".config", "postgresai", "config.json"), "utf8")
237+
);
238+
expect(saved.apiKey).toBe("test-token");
239+
} finally {
240+
rmSync(home, { recursive: true, force: true });
241+
}
242+
});
243+
215244
test("cli: auth login --help shows all options", () => {
216245
const r = runCli(["auth", "login", "--help"]);
217246
expect(r.status).toBe(0);

0 commit comments

Comments
 (0)