add account check-up command

This commit is contained in:
Clément DOUIN 2024-02-24 07:55:37 +01:00
parent 79da9404f3
commit 0e35a0cd64
No known key found for this signature in database
GPG key ID: 353E4A18EE0FAB72
2 changed files with 129 additions and 1 deletions

View file

@ -0,0 +1,121 @@
use anyhow::Result;
use clap::Parser;
use email::backend::context::BackendContextBuilder;
use log::info;
use crate::{
account::arg::name::OptionalAccountNameArg, backend, config::TomlConfig, printer::Printer,
};
/// Check up the given account.
///
/// This command performs a checkup of the given account. It checks if
/// the configuration is valid, if backend can be created and if
/// sessions work.
#[derive(Debug, Parser)]
pub struct AccountCheckUpCommand {
#[command(flatten)]
pub account: OptionalAccountNameArg,
}
impl AccountCheckUpCommand {
pub async fn execute(self, printer: &mut impl Printer, config: &TomlConfig) -> Result<()> {
info!("executing check up account command");
let account = self.account.name.as_ref().map(String::as_str);
printer.print_log("Checking configuration integrity…")?;
let (toml_account_config, account_config) =
config.clone().into_account_configs(account, true)?;
let used_backends = toml_account_config.get_used_backends();
printer.print_log("Checking backend context integrity…")?;
let ctx_builder = backend::BackendContextBuilder::new(
toml_account_config.clone(),
account_config,
Vec::from_iter(used_backends),
)
.await?;
let ctx = ctx_builder.clone().build().await?;
#[cfg(feature = "maildir")]
{
printer.print_log("Checking Maildir integrity…")?;
let maildir = ctx_builder
.maildir
.as_ref()
.and_then(|maildir| maildir.check_up())
.and_then(|f| ctx.maildir.as_ref().and_then(|ctx| f(ctx)));
if let Some(maildir) = maildir.as_ref() {
maildir.check_up().await?;
}
}
#[cfg(feature = "imap")]
{
printer.print_log("Checking IMAP integrity…")?;
let imap = ctx_builder
.imap
.as_ref()
.and_then(|imap| imap.check_up())
.and_then(|f| ctx.imap.as_ref().and_then(|ctx| f(ctx)));
if let Some(imap) = imap.as_ref() {
imap.check_up().await?;
}
}
#[cfg(feature = "notmuch")]
{
printer.print_log("Checking Notmuch integrity…")?;
let notmuch = ctx_builder
.notmuch
.as_ref()
.and_then(|notmuch| notmuch.check_up())
.and_then(|f| ctx.notmuch.as_ref().and_then(|ctx| f(ctx)));
if let Some(notmuch) = notmuch.as_ref() {
notmuch.check_up().await?;
}
}
#[cfg(feature = "smtp")]
{
printer.print_log("Checking SMTP integrity…")?;
let smtp = ctx_builder
.smtp
.as_ref()
.and_then(|smtp| smtp.check_up())
.and_then(|f| ctx.smtp.as_ref().and_then(|ctx| f(ctx)));
if let Some(smtp) = smtp.as_ref() {
smtp.check_up().await?;
}
}
#[cfg(feature = "sendmail")]
{
printer.print_log("Checking Sendmail integrity…")?;
let sendmail = ctx_builder
.sendmail
.as_ref()
.and_then(|sendmail| sendmail.check_up())
.and_then(|f| ctx.sendmail.as_ref().and_then(|ctx| f(ctx)));
if let Some(sendmail) = sendmail.as_ref() {
sendmail.check_up().await?;
}
}
printer.print("Checkup successfully completed!")
}
}

View file

@ -1,3 +1,4 @@
mod check_up;
mod configure;
mod list;
#[cfg(feature = "account-sync")]
@ -10,7 +11,9 @@ use crate::{config::TomlConfig, printer::Printer};
#[cfg(feature = "account-sync")]
use self::sync::AccountSyncCommand;
use self::{configure::AccountConfigureCommand, list::AccountListCommand};
use self::{
check_up::AccountCheckUpCommand, configure::AccountConfigureCommand, list::AccountListCommand,
};
/// Manage accounts.
///
@ -19,6 +22,9 @@ use self::{configure::AccountConfigureCommand, list::AccountListCommand};
/// file. This subcommand allows you to manage them.
#[derive(Debug, Subcommand)]
pub enum AccountSubcommand {
#[command(alias = "checkup")]
CheckUp(AccountCheckUpCommand),
#[command(alias = "cfg")]
Configure(AccountConfigureCommand),
@ -34,6 +40,7 @@ impl AccountSubcommand {
#[allow(unused)]
pub async fn execute(self, printer: &mut impl Printer, config: &TomlConfig) -> Result<()> {
match self {
Self::CheckUp(cmd) => cmd.execute(printer, config).await,
Self::Configure(cmd) => cmd.execute(printer, config).await,
Self::List(cmd) => cmd.execute(printer, config).await,
#[cfg(feature = "account-sync")]