mirror of
https://github.com/beilunyang/moemail.git
synced 2026-05-06 20:02:52 +08:00
feat(cli): add read command
This commit is contained in:
45
packages/cli/src/commands/read.ts
Normal file
45
packages/cli/src/commands/read.ts
Normal 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);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user