feat(cli): add read command

This commit is contained in:
ty
2026-03-22 14:47:39 +08:00
committed by BeilunYang
parent 8f4f605094
commit 1c7cf2e5fd
2 changed files with 47 additions and 0 deletions

View File

@@ -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 <id>", "email ID")
.requiredOption("--message-id <id>", "message ID")
.option("--format <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);
}
});
}

View File

@@ -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();