mirror of
https://github.com/pimalaya/himalaya.git
synced 2026-06-17 05:07:55 +08:00
86 lines
2.5 KiB
Rust
86 lines
2.5 KiB
Rust
use clap::Parser;
|
|
use color_eyre::Result;
|
|
use email::backend::feature::BackendFeatureSource;
|
|
use mml::MmlCompilerBuilder;
|
|
use std::io::{self, BufRead, IsTerminal};
|
|
use tracing::info;
|
|
|
|
#[cfg(feature = "account-sync")]
|
|
use crate::cache::arg::disable::CacheDisableFlag;
|
|
use crate::{
|
|
account::arg::name::AccountNameFlag, backend::Backend, config::TomlConfig,
|
|
email::template::arg::TemplateRawArg, folder::arg::name::FolderNameOptionalFlag,
|
|
printer::Printer,
|
|
};
|
|
|
|
/// Save a template to a folder.
|
|
///
|
|
/// This command allows you to save a template to the given
|
|
/// folder. The template is compiled into a MIME message before being
|
|
/// saved to the folder. If you want to save a raw message, use the
|
|
/// message save command instead.
|
|
#[derive(Debug, Parser)]
|
|
pub struct TemplateSaveCommand {
|
|
#[command(flatten)]
|
|
pub folder: FolderNameOptionalFlag,
|
|
|
|
#[command(flatten)]
|
|
pub template: TemplateRawArg,
|
|
|
|
#[cfg(feature = "account-sync")]
|
|
#[command(flatten)]
|
|
pub cache: CacheDisableFlag,
|
|
|
|
#[command(flatten)]
|
|
pub account: AccountNameFlag,
|
|
}
|
|
|
|
impl TemplateSaveCommand {
|
|
pub async fn execute(self, printer: &mut impl Printer, config: &TomlConfig) -> Result<()> {
|
|
info!("executing save template command");
|
|
|
|
let folder = &self.folder.name;
|
|
|
|
let (toml_account_config, account_config) = config.clone().into_account_configs(
|
|
self.account.name.as_deref(),
|
|
#[cfg(feature = "account-sync")]
|
|
self.cache.disable,
|
|
)?;
|
|
|
|
let add_message_kind = toml_account_config.add_message_kind();
|
|
|
|
let backend = Backend::new(
|
|
toml_account_config.clone(),
|
|
account_config.clone(),
|
|
add_message_kind,
|
|
|builder| builder.set_add_message(BackendFeatureSource::Context),
|
|
)
|
|
.await?;
|
|
|
|
let is_tty = io::stdin().is_terminal();
|
|
let is_json = printer.is_json();
|
|
let tpl = if is_tty || is_json {
|
|
self.template.raw()
|
|
} else {
|
|
io::stdin()
|
|
.lock()
|
|
.lines()
|
|
.map_while(Result::ok)
|
|
.collect::<Vec<String>>()
|
|
.join("\n")
|
|
};
|
|
|
|
#[allow(unused_mut)]
|
|
let mut compiler = MmlCompilerBuilder::new();
|
|
|
|
#[cfg(feature = "pgp")]
|
|
compiler.set_some_pgp(account_config.pgp.clone());
|
|
|
|
let msg = compiler.build(tpl.as_str())?.compile().await?.into_vec()?;
|
|
|
|
backend.add_message(folder, &msg).await?;
|
|
|
|
printer.out(format!("Template successfully saved to {folder}!"))
|
|
}
|
|
}
|