mirror of
https://github.com/pimalaya/himalaya.git
synced 2026-06-17 21:37:55 +08:00
adjust api, test commands with a greenmail instance
This commit is contained in:
@@ -4,8 +4,8 @@ use log::info;
|
||||
|
||||
use crate::{
|
||||
account::arg::name::AccountNameFlag, backend::Backend, cache::arg::disable::CacheDisableFlag,
|
||||
config::TomlConfig, envelope::arg::ids::EnvelopeIdsArgs, folder::arg::name::FolderNameArg,
|
||||
printer::Printer,
|
||||
config::TomlConfig, envelope::arg::ids::EnvelopeIdsArgs,
|
||||
folder::arg::name::FolderNameOptionalFlag, printer::Printer,
|
||||
};
|
||||
|
||||
/// Mark as deleted a message from a folder.
|
||||
@@ -17,7 +17,7 @@ use crate::{
|
||||
#[derive(Debug, Parser)]
|
||||
pub struct MessageDeleteCommand {
|
||||
#[command(flatten)]
|
||||
pub folder: FolderNameArg,
|
||||
pub folder: FolderNameOptionalFlag,
|
||||
|
||||
#[command(flatten)]
|
||||
pub envelopes: EnvelopeIdsArgs,
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
use anyhow::{anyhow, Result};
|
||||
use atty::Stream;
|
||||
use clap::Parser;
|
||||
use log::info;
|
||||
use std::io::{self, BufRead};
|
||||
|
||||
use crate::{
|
||||
account::arg::name::AccountNameFlag,
|
||||
@@ -10,7 +8,7 @@ use crate::{
|
||||
cache::arg::disable::CacheDisableFlag,
|
||||
config::TomlConfig,
|
||||
envelope::arg::ids::EnvelopeIdArg,
|
||||
folder::arg::name::FolderNameArg,
|
||||
folder::arg::name::FolderNameOptionalFlag,
|
||||
message::arg::{body::MessageRawBodyArg, header::HeaderRawArgs},
|
||||
printer::Printer,
|
||||
ui::editor,
|
||||
@@ -25,7 +23,7 @@ use crate::{
|
||||
#[derive(Debug, Parser)]
|
||||
pub struct MessageForwardCommand {
|
||||
#[command(flatten)]
|
||||
pub folder: FolderNameArg,
|
||||
pub folder: FolderNameOptionalFlag,
|
||||
|
||||
#[command(flatten)]
|
||||
pub envelope: EnvelopeIdArg,
|
||||
@@ -55,19 +53,6 @@ impl MessageForwardCommand {
|
||||
config.clone().into_account_configs(account, cache)?;
|
||||
let backend = Backend::new(toml_account_config, account_config.clone(), true).await?;
|
||||
|
||||
let is_tty = atty::is(Stream::Stdin);
|
||||
let is_json = printer.is_json();
|
||||
let body = if !self.body.is_empty() && (is_tty || is_json) {
|
||||
self.body.raw()
|
||||
} else {
|
||||
io::stdin()
|
||||
.lock()
|
||||
.lines()
|
||||
.filter_map(Result::ok)
|
||||
.collect::<Vec<String>>()
|
||||
.join("\r\n")
|
||||
};
|
||||
|
||||
let id = self.envelope.id;
|
||||
let tpl = backend
|
||||
.get_messages(folder, &[id])
|
||||
@@ -76,7 +61,7 @@ impl MessageForwardCommand {
|
||||
.ok_or(anyhow!("cannot find message"))?
|
||||
.to_forward_tpl_builder(&account_config)
|
||||
.with_headers(self.headers.raw)
|
||||
.with_body(body)
|
||||
.with_body(self.body.raw())
|
||||
.build()
|
||||
.await?;
|
||||
editor::edit_tpl_with_editor(&account_config, printer, &backend, tpl).await
|
||||
|
||||
@@ -4,7 +4,10 @@ use log::{debug, info};
|
||||
use mail_builder::MessageBuilder;
|
||||
use url::Url;
|
||||
|
||||
use crate::{backend::Backend, config::TomlConfig, printer::Printer, ui::editor};
|
||||
use crate::{
|
||||
account::arg::name::AccountNameFlag, backend::Backend, cache::arg::disable::CacheDisableFlag,
|
||||
config::TomlConfig, printer::Printer, ui::editor,
|
||||
};
|
||||
|
||||
/// Parse and edit a message from a mailto URL string.
|
||||
///
|
||||
@@ -17,19 +20,31 @@ pub struct MessageMailtoCommand {
|
||||
/// The mailto url.
|
||||
#[arg()]
|
||||
pub url: Url,
|
||||
|
||||
#[command(flatten)]
|
||||
pub cache: CacheDisableFlag,
|
||||
|
||||
#[command(flatten)]
|
||||
pub account: AccountNameFlag,
|
||||
}
|
||||
|
||||
impl MessageMailtoCommand {
|
||||
pub fn new(url: &str) -> Result<Self> {
|
||||
let url = Url::parse(url)?;
|
||||
Ok(Self { url })
|
||||
Ok(Self {
|
||||
url: Url::parse(url)?,
|
||||
cache: Default::default(),
|
||||
account: Default::default(),
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn execute(self, printer: &mut impl Printer, config: &TomlConfig) -> Result<()> {
|
||||
info!("executing message mailto command");
|
||||
|
||||
let account = self.account.name.as_ref().map(String::as_str);
|
||||
let cache = self.cache.disable;
|
||||
|
||||
let (toml_account_config, account_config) =
|
||||
config.clone().into_account_configs(None, false)?;
|
||||
config.clone().into_account_configs(account, cache)?;
|
||||
let backend = Backend::new(toml_account_config, account_config.clone(), true).await?;
|
||||
|
||||
let mut builder = MessageBuilder::new().to(self.url.path());
|
||||
|
||||
@@ -32,32 +32,34 @@ pub enum MessageSubcommand {
|
||||
#[command(arg_required_else_help = true)]
|
||||
Read(MessageReadCommand),
|
||||
|
||||
#[command(alias = "add", alias = "create", alias = "new", alias = "compose")]
|
||||
#[command(aliases = ["add", "create", "new", "compose"])]
|
||||
Write(MessageWriteCommand),
|
||||
|
||||
#[command()]
|
||||
Reply(MessageReplyCommand),
|
||||
|
||||
#[command(alias = "fwd")]
|
||||
#[command(aliases = ["fwd", "fd"])]
|
||||
Forward(MessageForwardCommand),
|
||||
|
||||
#[command()]
|
||||
Mailto(MessageMailtoCommand),
|
||||
|
||||
#[command(arg_required_else_help = true)]
|
||||
#[command(alias = "add", alias = "create")]
|
||||
Save(MessageSaveCommand),
|
||||
|
||||
#[command(arg_required_else_help = true)]
|
||||
Send(MessageSendCommand),
|
||||
|
||||
#[command(arg_required_else_help = true)]
|
||||
#[command(aliases = ["cpy", "cp"])]
|
||||
Copy(MessageCopyCommand),
|
||||
|
||||
#[command(arg_required_else_help = true)]
|
||||
#[command(alias = "mv")]
|
||||
Move(MessageMoveCommand),
|
||||
|
||||
#[command(arg_required_else_help = true)]
|
||||
#[command(aliases = ["remove", "rm"])]
|
||||
Delete(MessageDeleteCommand),
|
||||
}
|
||||
|
||||
|
||||
@@ -5,8 +5,8 @@ use mml::message::FilterParts;
|
||||
|
||||
use crate::{
|
||||
account::arg::name::AccountNameFlag, backend::Backend, cache::arg::disable::CacheDisableFlag,
|
||||
config::TomlConfig, envelope::arg::ids::EnvelopeIdsArgs, folder::arg::name::FolderNameArg,
|
||||
printer::Printer,
|
||||
config::TomlConfig, envelope::arg::ids::EnvelopeIdsArgs,
|
||||
folder::arg::name::FolderNameOptionalFlag, printer::Printer,
|
||||
};
|
||||
|
||||
/// Read a message.
|
||||
@@ -15,7 +15,7 @@ use crate::{
|
||||
#[derive(Debug, Parser)]
|
||||
pub struct MessageReadCommand {
|
||||
#[command(flatten)]
|
||||
pub folder: FolderNameArg,
|
||||
pub folder: FolderNameOptionalFlag,
|
||||
|
||||
#[command(flatten)]
|
||||
pub envelopes: EnvelopeIdsArgs,
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
use anyhow::{anyhow, Result};
|
||||
use atty::Stream;
|
||||
use clap::Parser;
|
||||
use email::flag::Flag;
|
||||
use log::info;
|
||||
use std::io::{self, BufRead};
|
||||
|
||||
use crate::{
|
||||
account::arg::name::AccountNameFlag,
|
||||
@@ -11,7 +9,7 @@ use crate::{
|
||||
cache::arg::disable::CacheDisableFlag,
|
||||
config::TomlConfig,
|
||||
envelope::arg::ids::EnvelopeIdArg,
|
||||
folder::arg::name::FolderNameArg,
|
||||
folder::arg::name::FolderNameOptionalFlag,
|
||||
message::arg::{body::MessageRawBodyArg, header::HeaderRawArgs, reply::MessageReplyAllArg},
|
||||
printer::Printer,
|
||||
ui::editor,
|
||||
@@ -26,7 +24,7 @@ use crate::{
|
||||
#[derive(Debug, Parser)]
|
||||
pub struct MessageReplyCommand {
|
||||
#[command(flatten)]
|
||||
pub folder: FolderNameArg,
|
||||
pub folder: FolderNameOptionalFlag,
|
||||
|
||||
#[command(flatten)]
|
||||
pub envelope: EnvelopeIdArg,
|
||||
@@ -59,19 +57,6 @@ impl MessageReplyCommand {
|
||||
config.clone().into_account_configs(account, cache)?;
|
||||
let backend = Backend::new(toml_account_config, account_config.clone(), true).await?;
|
||||
|
||||
let is_tty = atty::is(Stream::Stdin);
|
||||
let is_json = printer.is_json();
|
||||
let body = if !self.body.is_empty() && (is_tty || is_json) {
|
||||
self.body.raw()
|
||||
} else {
|
||||
io::stdin()
|
||||
.lock()
|
||||
.lines()
|
||||
.filter_map(Result::ok)
|
||||
.collect::<Vec<String>>()
|
||||
.join("\r\n")
|
||||
};
|
||||
|
||||
let id = self.envelope.id;
|
||||
let tpl = backend
|
||||
.get_messages(folder, &[id])
|
||||
@@ -80,7 +65,7 @@ impl MessageReplyCommand {
|
||||
.ok_or(anyhow!("cannot find message {id}"))?
|
||||
.to_reply_tpl_builder(&account_config)
|
||||
.with_headers(self.headers.raw)
|
||||
.with_body(body)
|
||||
.with_body(self.body.raw())
|
||||
.with_reply_all(self.reply.all)
|
||||
.build()
|
||||
.await?;
|
||||
|
||||
@@ -6,7 +6,7 @@ use std::io::{self, BufRead};
|
||||
|
||||
use crate::{
|
||||
account::arg::name::AccountNameFlag, backend::Backend, cache::arg::disable::CacheDisableFlag,
|
||||
config::TomlConfig, folder::arg::name::FolderNameArg, message::arg::body::MessageRawBodyArg,
|
||||
config::TomlConfig, folder::arg::name::FolderNameOptionalFlag, message::arg::MessageRawArg,
|
||||
printer::Printer,
|
||||
};
|
||||
|
||||
@@ -16,10 +16,10 @@ use crate::{
|
||||
#[derive(Debug, Parser)]
|
||||
pub struct MessageSaveCommand {
|
||||
#[command(flatten)]
|
||||
pub folder: FolderNameArg,
|
||||
pub folder: FolderNameOptionalFlag,
|
||||
|
||||
#[command(flatten)]
|
||||
pub body: MessageRawBodyArg,
|
||||
pub message: MessageRawArg,
|
||||
|
||||
#[command(flatten)]
|
||||
pub cache: CacheDisableFlag,
|
||||
@@ -43,7 +43,7 @@ impl MessageSaveCommand {
|
||||
let is_tty = atty::is(Stream::Stdin);
|
||||
let is_json = printer.is_json();
|
||||
let msg = if is_tty || is_json {
|
||||
self.body.raw()
|
||||
self.message.raw()
|
||||
} else {
|
||||
io::stdin()
|
||||
.lock()
|
||||
|
||||
@@ -7,7 +7,7 @@ use std::io::{self, BufRead};
|
||||
|
||||
use crate::{
|
||||
account::arg::name::AccountNameFlag, backend::Backend, cache::arg::disable::CacheDisableFlag,
|
||||
config::TomlConfig, message::arg::body::MessageRawBodyArg, printer::Printer,
|
||||
config::TomlConfig, message::arg::MessageRawArg, printer::Printer,
|
||||
};
|
||||
|
||||
/// Send a message.
|
||||
@@ -17,7 +17,7 @@ use crate::{
|
||||
#[derive(Debug, Parser)]
|
||||
pub struct MessageSendCommand {
|
||||
#[command(flatten)]
|
||||
pub body: MessageRawBodyArg,
|
||||
pub message: MessageRawArg,
|
||||
|
||||
#[command(flatten)]
|
||||
pub cache: CacheDisableFlag,
|
||||
@@ -41,7 +41,7 @@ impl MessageSendCommand {
|
||||
let is_tty = atty::is(Stream::Stdin);
|
||||
let is_json = printer.is_json();
|
||||
let msg = if is_tty || is_json {
|
||||
self.body.raw()
|
||||
self.message.raw()
|
||||
} else {
|
||||
io::stdin()
|
||||
.lock()
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
use anyhow::Result;
|
||||
use atty::Stream;
|
||||
use clap::Parser;
|
||||
use email::message::Message;
|
||||
use log::info;
|
||||
use std::io::{self, BufRead};
|
||||
|
||||
use crate::{
|
||||
account::arg::name::AccountNameFlag,
|
||||
@@ -47,22 +45,9 @@ impl MessageWriteCommand {
|
||||
config.clone().into_account_configs(account, cache)?;
|
||||
let backend = Backend::new(toml_account_config, account_config.clone(), true).await?;
|
||||
|
||||
let is_tty = atty::is(Stream::Stdin);
|
||||
let is_json = printer.is_json();
|
||||
let body = if !self.body.is_empty() && (is_tty || is_json) {
|
||||
self.body.raw()
|
||||
} else {
|
||||
io::stdin()
|
||||
.lock()
|
||||
.lines()
|
||||
.filter_map(Result::ok)
|
||||
.collect::<Vec<String>>()
|
||||
.join("\r\n")
|
||||
};
|
||||
|
||||
let tpl = Message::new_tpl_builder(&account_config)
|
||||
.with_headers(self.headers.raw)
|
||||
.with_body(body)
|
||||
.with_body(self.body.raw())
|
||||
.build()
|
||||
.await?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user