From 1c7cf2e5fd4ed09e516ed4bc4d8f44974499a5bc Mon Sep 17 00:00:00 2001 From: ty <786220806@qq.com> Date: Sun, 22 Mar 2026 14:47:39 +0800 Subject: [PATCH] feat(cli): add read command --- packages/cli/src/commands/read.ts | 45 +++++++++++++++++++++++++++++++ packages/cli/src/index.ts | 2 ++ 2 files changed, 47 insertions(+) create mode 100644 packages/cli/src/commands/read.ts diff --git a/packages/cli/src/commands/read.ts b/packages/cli/src/commands/read.ts new file mode 100644 index 0000000..5e7a085 --- /dev/null +++ b/packages/cli/src/commands/read.ts @@ -0,0 +1,45 @@ +import { Command } from "commander"; +import { api } from "../api.js"; +import { log, printJson, printText, msToIso } from "../output.js"; + +export function registerReadCommand(program: Command) { + program + .command("read") + .description("Read an email message") + .requiredOption("--email-id ", "email ID") + .requiredOption("--message-id ", "message ID") + .option("--format ", "text | html", "text") + .action(async (opts) => { + const json = program.opts().json; + try { + const data = (await api.getMessage(opts.emailId, opts.messageId)) as any; + const msg = data.message; + + if (json) { + printJson({ + id: msg.id, + from: msg.from_address, + to: msg.to_address, + subject: msg.subject, + content: msg.content, + html: msg.html, + receivedAt: msg.received_at ? msToIso(msg.received_at) : null, + type: msg.type, + }); + } else { + printText(`From: ${msg.from_address}`); + printText(`To: ${msg.to_address}`); + printText(`Subject: ${msg.subject}`); + printText(`---`); + if (opts.format === "html") { + printText(msg.html || "(no HTML content)"); + } else { + printText(msg.content || "(no text content)"); + } + } + } catch (e: any) { + log(`Error: ${e.message}`); + process.exit(1); + } + }); +} diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index bc73837..be796ee 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -4,6 +4,7 @@ import { registerConfigCommand } from "./commands/config.js"; import { registerCreateCommand } from "./commands/create.js"; import { registerListCommand } from "./commands/list.js"; import { registerWaitCommand } from "./commands/wait.js"; +import { registerReadCommand } from "./commands/read.js"; const program = new Command(); @@ -17,5 +18,6 @@ registerConfigCommand(program); registerCreateCommand(program); registerListCommand(program); registerWaitCommand(program); +registerReadCommand(program); program.parse();