feat(cli): add list command for mailboxes and messages

This commit is contained in:
ty
2026-03-22 14:47:19 +08:00
committed by BeilunYang
parent 12b5f4afeb
commit b891ee4655
2 changed files with 69 additions and 0 deletions

View File

@@ -0,0 +1,67 @@
import { Command } from "commander";
import { api } from "../api.js";
import { log, printJson, printText, msToIso } from "../output.js";
export function registerListCommand(program: Command) {
program
.command("list")
.description("List mailboxes or messages")
.option("--email-id <id>", "list messages in this mailbox")
.option("--cursor <cursor>", "pagination cursor")
.action(async (opts) => {
const json = program.opts().json;
try {
if (opts.emailId) {
const data = (await api.listMessages(opts.emailId, opts.cursor)) as any;
if (json) {
printJson({
messages: data.messages.map((m: any) => ({
id: m.id,
from: m.from_address,
subject: m.subject,
receivedAt: m.received_at ? msToIso(m.received_at) : null,
})),
nextCursor: data.nextCursor,
total: data.total,
});
} else {
if (!data.messages.length) {
printText("No messages.");
} else {
for (const m of data.messages) {
printText(`[${m.id}] From: ${m.from_address}${m.subject}`);
}
printText(`Total: ${data.total}`);
}
}
} else {
// Note: GET /api/emails returns raw Drizzle ORM objects.
// expiresAt is a Date serialized to ISO string (not epoch ms).
const data = (await api.listEmails(opts.cursor)) as any;
if (json) {
printJson({
emails: data.emails.map((e: any) => ({
id: e.id,
address: e.address,
expiresAt: e.expiresAt || null,
})),
nextCursor: data.nextCursor,
total: data.total,
});
} else {
if (!data.emails.length) {
printText("No mailboxes.");
} else {
for (const e of data.emails) {
printText(`[${e.id}] ${e.address}`);
}
printText(`Total: ${data.total}`);
}
}
}
} catch (e: any) {
log(`Error: ${e.message}`);
process.exit(1);
}
});
}

View File

@@ -2,6 +2,7 @@
import { Command } from "commander";
import { registerConfigCommand } from "./commands/config.js";
import { registerCreateCommand } from "./commands/create.js";
import { registerListCommand } from "./commands/list.js";
const program = new Command();
@@ -13,5 +14,6 @@ program
registerConfigCommand(program);
registerCreateCommand(program);
registerListCommand(program);
program.parse();