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
+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
}
}