Skip to content

Commit

Permalink
refactor account with clap derive api
Browse files Browse the repository at this point in the history
  • Loading branch information
soywod committed Dec 6, 2023
1 parent d230822 commit abe4c7f
Show file tree
Hide file tree
Showing 21 changed files with 1,044 additions and 1,017 deletions.
13 changes: 12 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ version = "0.2"
version = "0.4.24"

[dependencies.clap]
version = "4.0"
features = ["derive"]
version = "4.4"
features = ["derive", "wrap_help"]

[dependencies.clap_complete]
version = "4.0"
version = "4.4"

[dependencies.clap_mangen]
version = "0.2"
Expand Down
160 changes: 0 additions & 160 deletions src/account/args.rs

This file was deleted.

115 changes: 115 additions & 0 deletions src/account/command/configure.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
use anyhow::Result;
use clap::Parser;
#[cfg(feature = "imap")]
use email::imap::config::ImapAuthConfig;
#[cfg(feature = "smtp")]
use email::smtp::config::SmtpAuthConfig;
use log::{debug, info, warn};

use crate::{
config::{
wizard::{prompt_passwd, prompt_secret},
TomlConfig,
},
printer::Printer,
};

/// Configure the given account
#[derive(Debug, Parser)]
pub struct Command {
/// The name of the account that needs to be configured
///
/// The account names are taken from the table at the root level
/// of your TOML configuration file.
#[arg(value_name = "NAME")]
pub account_name: String,

/// Force the account to reconfigure, even if it is already
/// configured
#[arg(long, short)]
pub force: bool,
}

impl Command {
pub async fn execute(self, printer: &mut impl Printer, config: &TomlConfig) -> Result<()> {
info!("executing account configure command");

let (_, account_config) =
config.into_toml_account_config(Some(self.account_name.as_str()))?;

if self.force {
#[cfg(feature = "imap")]
if let Some(ref config) = account_config.imap {
let reset = match &config.auth {
ImapAuthConfig::Passwd(config) => config.reset(),
ImapAuthConfig::OAuth2(config) => config.reset(),
};
if let Err(err) = reset {
warn!("error while resetting imap secrets: {err}");
debug!("error while resetting imap secrets: {err:?}");
}
}

#[cfg(feature = "smtp")]
if let Some(ref config) = account_config.smtp {
let reset = match &config.auth {
SmtpAuthConfig::Passwd(config) => config.reset(),
SmtpAuthConfig::OAuth2(config) => config.reset(),
};
if let Err(err) = reset {
warn!("error while resetting smtp secrets: {err}");
debug!("error while resetting smtp secrets: {err:?}");
}
}

#[cfg(feature = "pgp")]
if let Some(ref config) = account_config.pgp {
account_config.pgp.reset().await?;
}
}

#[cfg(feature = "imap")]
if let Some(ref config) = account_config.imap {
match &config.auth {
ImapAuthConfig::Passwd(config) => {
config.configure(|| prompt_passwd("IMAP password")).await
}
ImapAuthConfig::OAuth2(config) => {
config
.configure(|| prompt_secret("IMAP OAuth 2.0 client secret"))
.await
}
}?;
}

#[cfg(feature = "smtp")]
if let Some(ref config) = account_config.smtp {
match &config.auth {
SmtpAuthConfig::Passwd(config) => {
config.configure(|| prompt_passwd("SMTP password")).await
}
SmtpAuthConfig::OAuth2(config) => {
config
.configure(|| prompt_secret("SMTP OAuth 2.0 client secret"))
.await
}
}?;
}

#[cfg(feature = "pgp")]
if let Some(ref config) = config.pgp {
config
.pgp
.configure(&config.email, || prompt_passwd("PGP secret key password"))
.await?;
}

printer.print(format!(
"Account {} successfully {}configured!",
self.account_name,
if self.force { "re" } else { "" }
))?;

Ok(())
}
}
Loading

0 comments on commit abe4c7f

Please sign in to comment.