From 728f2555d7bf05b7680ae4727e7f1b24755e5f01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20DOUIN?= Date: Sun, 7 May 2023 10:18:58 +0200 Subject: [PATCH] improve oauth2 config reset --- src/domain/account/handlers.rs | 51 +++++++++++++++++++++++++--------- src/main.rs | 2 +- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/domain/account/handlers.rs b/src/domain/account/handlers.rs index c354941..b4d5606 100644 --- a/src/domain/account/handlers.rs +++ b/src/domain/account/handlers.rs @@ -4,10 +4,10 @@ use anyhow::Result; use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; -use log::{info, trace}; +use log::{info, trace, warn}; use pimalaya_email::{ folder::sync::Strategy as SyncFoldersStrategy, AccountConfig, Backend, BackendConfig, - BackendSyncBuilder, BackendSyncProgressEvent, + BackendSyncBuilder, BackendSyncProgressEvent, EmailSender, }; use crate::{ @@ -17,20 +17,45 @@ use crate::{ }; /// Configure the current selected account -pub fn configure(backend_config: &BackendConfig, reset: bool) -> Result<()> { +pub fn configure( + account_config: &AccountConfig, + backend_config: &BackendConfig, + reset: bool, +) -> Result<()> { info!("entering the configure account handler"); - match backend_config { - BackendConfig::None => (), - BackendConfig::Maildir(_) => (), + + if reset { #[cfg(feature = "imap-backend")] - BackendConfig::Imap(imap_config) => { - imap_config - .auth - .configure(reset, configure_oauth2_client_secret)?; + if let BackendConfig::Imap(imap_config) = backend_config { + println!("Resetting IMAP secrets…"); + if let Err(err) = imap_config.auth.reset() { + warn!("error while resetting imap secrets, skipping it"); + warn!("{err}"); + } } - #[cfg(feature = "notmuch-backend")] - BackendConfig::Notmuch(config) => (), - }; + + #[cfg(feature = "smtp-sender")] + if let EmailSender::Smtp(smtp_config) = &account_config.email_sender { + println!("Resetting SMTP secrets…"); + if let Err(err) = smtp_config.auth.reset() { + warn!("error while resetting smtp secrets, skipping it"); + warn!("{err}"); + } + } + } + + #[cfg(feature = "imap-backend")] + if let BackendConfig::Imap(imap_config) = backend_config { + println!("Configuring IMAP secrets…"); + imap_config.auth.configure(configure_oauth2_client_secret)?; + } + + #[cfg(feature = "smtp-sender")] + if let EmailSender::Smtp(smtp_config) = &account_config.email_sender { + println!("Configuring SMTP secrets…"); + smtp_config.auth.configure(configure_oauth2_client_secret)?; + } + println!("Account successfully configured!"); Ok(()) } diff --git a/src/main.rs b/src/main.rs index 15e6834..f6fff46 100644 --- a/src/main.rs +++ b/src/main.rs @@ -138,7 +138,7 @@ fn main() -> Result<()> { return Ok(()); } Some(account::args::Cmd::Configure(reset)) => { - return account::handlers::configure(&backend_config, reset); + return account::handlers::configure(&account_config, &backend_config, reset); } _ => (), }