refactor folder with clap derive api

This commit is contained in:
Clément DOUIN
2023-12-06 21:46:31 +01:00
parent abe4c7f4ea
commit 4a77253c1d
29 changed files with 446 additions and 114 deletions
+1
View File
@@ -0,0 +1 @@
pub mod name;
+9
View File
@@ -0,0 +1,9 @@
use clap::Parser;
/// The folder name argument parser
#[derive(Debug, Parser)]
pub struct FolderNameArg {
/// The name of the folder
#[arg(name = "folder-name", value_name = "FOLDER")]
pub name: String,
}
+40
View File
@@ -0,0 +1,40 @@
use anyhow::Result;
use clap::Parser;
use log::info;
use crate::{
account::arg::name::AccountNameFlag, backend::Backend, cache::arg::disable::DisableCacheFlag,
config::TomlConfig, folder::arg::name::FolderNameArg, printer::Printer,
};
/// Create a new folder
#[derive(Debug, Parser)]
pub struct FolderCreateCommand {
#[command(flatten)]
pub folder: FolderNameArg,
#[command(flatten)]
pub account: AccountNameFlag,
#[command(flatten)]
pub cache: DisableCacheFlag,
}
impl FolderCreateCommand {
pub async fn execute(self, printer: &mut impl Printer, config: &TomlConfig) -> Result<()> {
info!("executing folder create 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?;
backend.add_folder(&folder).await?;
printer.print(format!("Folder {folder} successfully created!"))?;
Ok(())
}
}
+55
View File
@@ -0,0 +1,55 @@
use anyhow::Result;
use clap::Parser;
use dialoguer::Confirm;
use log::info;
use std::process;
use crate::{
account::arg::name::AccountNameFlag, backend::Backend, cache::arg::disable::DisableCacheFlag,
config::TomlConfig, folder::arg::name::FolderNameArg, printer::Printer,
};
/// Delete a folder
///
/// All emails from a given folder are definitely deleted. The folder
/// is also deleted after execution of the command.
#[derive(Debug, Parser)]
pub struct FolderDeleteCommand {
#[command(flatten)]
pub folder: FolderNameArg,
#[command(flatten)]
pub account: AccountNameFlag,
#[command(flatten)]
pub cache: DisableCacheFlag,
}
impl FolderDeleteCommand {
pub async fn execute(self, printer: &mut impl Printer, config: &TomlConfig) -> Result<()> {
info!("executing folder delete command");
let folder = &self.folder.name;
let confirm_msg = format!("Do you really want to delete the folder {folder}? All emails will be definitely deleted.");
let confirm = Confirm::new()
.with_prompt(confirm_msg)
.default(false)
.report(false)
.interact_opt()?;
if let Some(false) | None = confirm {
process::exit(0);
};
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?;
backend.delete_folder(&folder).await?;
printer.print(format!("Folder {folder} successfully deleted!"))?;
Ok(())
}
}
+44
View File
@@ -0,0 +1,44 @@
use anyhow::Result;
use clap::Parser;
use log::info;
use crate::{
account::arg::name::AccountNameFlag, backend::Backend, cache::arg::disable::DisableCacheFlag,
config::TomlConfig, folder::arg::name::FolderNameArg, printer::Printer,
};
/// Expunge a folder
///
/// The concept of expunging is similar to the IMAP one: it definitely
/// deletes emails from a given folder that contain the "deleted"
/// flag.
#[derive(Debug, Parser)]
pub struct FolderExpungeCommand {
#[command(flatten)]
pub folder: FolderNameArg,
#[command(flatten)]
pub account: AccountNameFlag,
#[command(flatten)]
pub cache: DisableCacheFlag,
}
impl FolderExpungeCommand {
pub async fn execute(self, printer: &mut impl Printer, config: &TomlConfig) -> Result<()> {
info!("executing folder expunge 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?;
backend.expunge_folder(&folder).await?;
printer.print(format!("Folder {folder} successfully expunged!"))?;
Ok(())
}
}
+50
View File
@@ -0,0 +1,50 @@
use anyhow::Result;
use clap::Parser;
use log::info;
use crate::{
account::arg::name::AccountNameFlag,
backend::Backend,
cache::arg::disable::DisableCacheFlag,
config::TomlConfig,
folder::Folders,
printer::{PrintTableOpts, Printer},
ui::arg::max_width::MaxTableWidthFlag,
};
/// List all folders
#[derive(Debug, Parser)]
pub struct FolderListCommand {
#[command(flatten)]
pub table: MaxTableWidthFlag,
#[command(flatten)]
pub account: AccountNameFlag,
#[command(flatten)]
pub cache: DisableCacheFlag,
}
impl FolderListCommand {
pub async fn execute(self, printer: &mut impl Printer, config: &TomlConfig) -> Result<()> {
info!("executing folder list command");
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?;
let folders: Folders = backend.list_folders().await?.into();
printer.print_table(
Box::new(folders),
PrintTableOpts {
format: &account_config.email_reading_format,
max_width: self.table.max_width,
},
)?;
Ok(())
}
}
+51
View File
@@ -0,0 +1,51 @@
mod create;
mod delete;
mod expunge;
mod list;
mod purge;
use anyhow::Result;
use clap::Subcommand;
use crate::{config::TomlConfig, printer::Printer};
use self::{
create::FolderCreateCommand, delete::FolderDeleteCommand, expunge::FolderExpungeCommand,
list::FolderListCommand, purge::FolderPurgeCommand,
};
/// Subcommand to manage accounts
#[derive(Debug, Subcommand)]
pub enum FolderSubcommand {
/// Create a new folder
#[command(alias = "add")]
Create(FolderCreateCommand),
/// List all folders
#[command(alias = "lst")]
List(FolderListCommand),
/// Expunge a folder
#[command()]
Expunge(FolderExpungeCommand),
/// Purge a folder
#[command()]
Purge(FolderPurgeCommand),
/// Delete a folder
#[command(alias = "remove", alias = "rm")]
Delete(FolderDeleteCommand),
}
impl FolderSubcommand {
pub async fn execute(self, printer: &mut impl Printer, config: &TomlConfig) -> Result<()> {
match self {
Self::Create(cmd) => cmd.execute(printer, config).await,
Self::List(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,
}
}
}
+55
View File
@@ -0,0 +1,55 @@
use anyhow::Result;
use clap::Parser;
use dialoguer::Confirm;
use log::info;
use std::process;
use crate::{
account::arg::name::AccountNameFlag, backend::Backend, cache::arg::disable::DisableCacheFlag,
config::TomlConfig, folder::arg::name::FolderNameArg, printer::Printer,
};
/// Purge a folder
///
/// All emails from a given folder are definitely deleted. The purged
/// folder will remain empty after executing of the command.
#[derive(Debug, Parser)]
pub struct FolderPurgeCommand {
#[command(flatten)]
pub folder: FolderNameArg,
#[command(flatten)]
pub account: AccountNameFlag,
#[command(flatten)]
pub cache: DisableCacheFlag,
}
impl FolderPurgeCommand {
pub async fn execute(self, printer: &mut impl Printer, config: &TomlConfig) -> Result<()> {
info!("executing folder purge command");
let folder = &self.folder.name;
let confirm_msg = format!("Do you really want to purge the folder {folder}? All emails will be definitely deleted.");
let confirm = Confirm::new()
.with_prompt(confirm_msg)
.default(false)
.report(false)
.interact_opt()?;
if let Some(false) | None = confirm {
process::exit(0);
};
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?;
backend.purge_folder(&folder).await?;
printer.print(format!("Folder {folder} successfully purged!"))?;
Ok(())
}
}
+2
View File
@@ -1,4 +1,6 @@
pub mod arg;
pub mod args;
pub mod command;
pub mod config;
pub mod handlers;