init folder watch command

This commit is contained in:
Clément DOUIN
2023-12-14 12:13:08 +01:00
parent a68d297366
commit 7fccdd822a
9 changed files with 329 additions and 7 deletions
+8
View File
@@ -191,6 +191,14 @@ impl TomlAccountConfig {
.or_else(|| self.backend.as_ref())
}
pub fn get_watch_message_kind(&self) -> Option<&BackendKind> {
self.message
.as_ref()
.and_then(|msg| msg.watch.as_ref())
.and_then(|watch| watch.backend.as_ref())
.or_else(|| self.backend.as_ref())
}
pub fn get_used_backends(&self) -> HashSet<&BackendKind> {
let mut used_backends = HashSet::default();
+28
View File
@@ -11,6 +11,7 @@ use email::imap::{ImapSessionBuilder, ImapSessionSync};
use email::smtp::{SmtpClientBuilder, SmtpClientSync};
use email::{
account::config::AccountConfig,
email::watch::{imap::WatchImapEmails, maildir::WatchMaildirEmails},
envelope::{
get::{imap::GetEnvelopeImap, maildir::GetEnvelopeMaildir},
list::{imap::ListEnvelopesImap, maildir::ListEnvelopesMaildir},
@@ -355,6 +356,33 @@ impl BackendBuilder {
_ => (),
}
match toml_account_config.backend {
Some(BackendKind::Maildir) => {
backend_builder = backend_builder.with_watch_emails(|ctx| {
ctx.maildir.as_ref().and_then(WatchMaildirEmails::new)
});
}
Some(BackendKind::MaildirForSync) => {
backend_builder = backend_builder.with_watch_emails(|ctx| {
ctx.maildir_for_sync
.as_ref()
.and_then(WatchMaildirEmails::new)
});
}
#[cfg(feature = "imap")]
Some(BackendKind::Imap) => {
backend_builder = backend_builder
.with_watch_emails(|ctx| ctx.imap.as_ref().and_then(WatchImapEmails::new));
}
#[cfg(feature = "notmuch")]
Some(BackendKind::Notmuch) => {
backend_builder = backend_builder.with_watch_emails(|ctx| {
ctx.notmuch.as_ref().and_then(WatchNotmuchEmails::new)
});
}
_ => (),
}
match toml_account_config.get_envelope_kind() {
Some(BackendKind::Maildir) => {
backend_builder = backend_builder.with_get_envelope(|ctx| {
+1
View File
@@ -211,6 +211,7 @@ impl TomlConfig {
read: c.read.map(|c| c.remote),
write: c.write.map(|c| c.remote),
send: c.send.map(|c| c.remote),
watch: c.watch.map(|c| c.remote),
}),
sync: config.sync,
#[cfg(feature = "pgp")]
+26
View File
@@ -12,6 +12,8 @@ pub struct MessageConfig {
pub copy: Option<MessageCopyConfig>,
#[serde(rename = "move")]
pub move_: Option<MessageMoveConfig>,
pub watch: Option<WatchMessageConfig>,
}
impl MessageConfig {
@@ -42,6 +44,10 @@ impl MessageConfig {
kinds.extend(move_.get_used_backends());
}
if let Some(watch) = &self.watch {
kinds.extend(watch.get_used_backends());
}
kinds
}
}
@@ -156,3 +162,23 @@ impl MessageMoveConfig {
kinds
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize, Serialize)]
pub struct WatchMessageConfig {
pub backend: Option<BackendKind>,
#[serde(flatten)]
pub remote: email::message::watch::config::WatchMessageConfig,
}
impl WatchMessageConfig {
pub fn get_used_backends(&self) -> HashSet<&BackendKind> {
let mut kinds = HashSet::default();
if let Some(kind) = &self.backend {
kinds.insert(kind);
}
kinds
}
}
+6 -1
View File
@@ -3,6 +3,7 @@ mod delete;
mod expunge;
mod list;
mod purge;
mod watch;
use anyhow::Result;
use clap::Subcommand;
@@ -11,7 +12,7 @@ use crate::{config::TomlConfig, printer::Printer};
use self::{
create::FolderCreateCommand, delete::FolderDeleteCommand, expunge::FolderExpungeCommand,
list::FolderListCommand, purge::FolderPurgeCommand,
list::FolderListCommand, purge::FolderPurgeCommand, watch::FolderWatchCommand,
};
/// Manage folders.
@@ -26,6 +27,9 @@ pub enum FolderSubcommand {
#[command(alias = "lst")]
List(FolderListCommand),
#[command()]
Watch(FolderWatchCommand),
#[command()]
Expunge(FolderExpungeCommand),
@@ -41,6 +45,7 @@ impl FolderSubcommand {
match self {
Self::Create(cmd) => cmd.execute(printer, config).await,
Self::List(cmd) => cmd.execute(printer, config).await,
Self::Watch(cmd) => cmd.execute(printer, config).await,
Self::Expunge(cmd) => cmd.execute(printer, config).await,
Self::Purge(cmd) => cmd.execute(printer, config).await,
Self::Delete(cmd) => cmd.execute(printer, config).await,
+42
View File
@@ -0,0 +1,42 @@
use anyhow::Result;
use clap::Parser;
use log::info;
use crate::{
account::arg::name::AccountNameFlag, backend::Backend, cache::arg::disable::CacheDisableFlag,
config::TomlConfig, folder::arg::name::FolderNameArg, printer::Printer,
};
/// Watch a folder for changes.
///
/// This command allows you to watch a new folder using the given
/// name.
#[derive(Debug, Parser)]
pub struct FolderWatchCommand {
#[command(flatten)]
pub folder: FolderNameArg,
#[command(flatten)]
pub cache: CacheDisableFlag,
#[command(flatten)]
pub account: AccountNameFlag,
}
impl FolderWatchCommand {
pub async fn execute(self, printer: &mut impl Printer, config: &TomlConfig) -> Result<()> {
info!("executing folder watch command");
let folder = &self.folder.name;
let some_account_name = self.account.name.as_ref().map(String::as_str);
let (toml_account_config, account_config) = config
.clone()
.into_account_configs(some_account_name, self.cache.disable)?;
let backend = Backend::new(toml_account_config, account_config.clone(), false).await?;
printer.print_log(format!("Start watching folder {folder} for changes…"))?;
backend.watch_emails(&folder).await
}
}