-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
254 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use clap::Parser; | ||
use email::flag::{Flag, Flags}; | ||
use log::debug; | ||
|
||
/// The ids and/or flags arguments parser | ||
#[derive(Debug, Parser)] | ||
pub struct IdsAndFlagsArgs { | ||
/// The list of ids and/or flags | ||
/// | ||
/// Every argument that can be parsed as an integer is considered | ||
/// an id, otherwise it is considered as a flag. | ||
#[arg(value_name = "ID-OR-FLAG", required = true)] | ||
pub ids_and_flags: Vec<IdOrFlag>, | ||
} | ||
|
||
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)] | ||
pub enum IdOrFlag { | ||
Id(String), | ||
Flag(Flag), | ||
} | ||
|
||
impl From<&str> for IdOrFlag { | ||
fn from(value: &str) -> Self { | ||
value | ||
.parse::<usize>() | ||
.map(|_| Self::Id(value.to_owned())) | ||
.unwrap_or_else(|err| { | ||
let flag = Flag::from(value); | ||
debug!("cannot parse {value} as usize, parsing it as flag {flag}"); | ||
debug!("{err:?}"); | ||
Self::Flag(flag) | ||
}) | ||
} | ||
} | ||
|
||
pub fn to_tuple<'a>(ids_and_flags: &'a [IdOrFlag]) -> (Vec<&'a str>, Flags) { | ||
ids_and_flags.iter().fold( | ||
(Vec::default(), Flags::default()), | ||
|(mut ids, mut flags), arg| { | ||
match arg { | ||
IdOrFlag::Id(id) => { | ||
ids.push(id.as_str()); | ||
} | ||
IdOrFlag::Flag(flag) => { | ||
flags.insert(flag.to_owned()); | ||
} | ||
}; | ||
(ids, flags) | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod ids_and_flags; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use anyhow::Result; | ||
use clap::Parser; | ||
use log::info; | ||
|
||
use crate::{ | ||
account::arg::name::AccountNameFlag, | ||
backend::Backend, | ||
cache::arg::disable::DisableCacheFlag, | ||
config::TomlConfig, | ||
flag::arg::ids_and_flags::{to_tuple, IdsAndFlagsArgs}, | ||
folder::arg::name::FolderNameArg, | ||
printer::Printer, | ||
}; | ||
|
||
/// Add flag(s) to an envelope | ||
#[derive(Debug, Parser)] | ||
pub struct FlagAddCommand { | ||
#[command(flatten)] | ||
pub folder: FolderNameArg, | ||
|
||
#[command(flatten)] | ||
pub args: IdsAndFlagsArgs, | ||
|
||
#[command(flatten)] | ||
pub account: AccountNameFlag, | ||
|
||
#[command(flatten)] | ||
pub cache: DisableCacheFlag, | ||
} | ||
|
||
impl FlagAddCommand { | ||
pub async fn execute(self, printer: &mut impl Printer, config: &TomlConfig) -> Result<()> { | ||
info!("executing flag add command"); | ||
|
||
let folder = &self.folder.name; | ||
let account = self.account.name.as_ref().map(String::as_str); | ||
let cache = self.cache.disable; | ||
|
||
let (toml_account_config, account_config) = | ||
config.clone().into_account_configs(account, cache)?; | ||
let backend = Backend::new(toml_account_config, account_config.clone(), false).await?; | ||
|
||
let (ids, flags) = to_tuple(&self.args.ids_and_flags); | ||
backend.add_flags(folder, &ids, &flags).await?; | ||
|
||
printer.print(format!("Flag(s) {flags} successfully added!")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
mod add; | ||
mod remove; | ||
mod set; | ||
|
||
use anyhow::Result; | ||
use clap::Subcommand; | ||
|
||
use crate::{config::TomlConfig, printer::Printer}; | ||
|
||
use self::{add::FlagAddCommand, remove::FlagRemoveCommand, set::FlagSetCommand}; | ||
|
||
/// Subcommand to manage flags | ||
#[derive(Debug, Subcommand)] | ||
pub enum FlagSubcommand { | ||
/// Add flag(s) to an envelope | ||
#[command(arg_required_else_help = true)] | ||
#[command(alias = "create")] | ||
Add(FlagAddCommand), | ||
|
||
/// Replace flag(s) of an envelope | ||
#[command(arg_required_else_help = true)] | ||
#[command(aliases = ["update", "change", "replace"])] | ||
Set(FlagSetCommand), | ||
|
||
/// Remove flag(s) from an envelope | ||
#[command(arg_required_else_help = true)] | ||
#[command(aliases = ["rm", "delete", "del"])] | ||
Remove(FlagRemoveCommand), | ||
} | ||
|
||
impl FlagSubcommand { | ||
pub async fn execute(self, printer: &mut impl Printer, config: &TomlConfig) -> Result<()> { | ||
match self { | ||
Self::Add(cmd) => cmd.execute(printer, config).await, | ||
Self::Set(cmd) => cmd.execute(printer, config).await, | ||
Self::Remove(cmd) => cmd.execute(printer, config).await, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use anyhow::Result; | ||
use clap::Parser; | ||
use log::info; | ||
|
||
use crate::{ | ||
account::arg::name::AccountNameFlag, | ||
backend::Backend, | ||
cache::arg::disable::DisableCacheFlag, | ||
config::TomlConfig, | ||
flag::arg::ids_and_flags::{to_tuple, IdsAndFlagsArgs}, | ||
folder::arg::name::FolderNameArg, | ||
printer::Printer, | ||
}; | ||
|
||
/// Remove flag(s) from an envelope | ||
#[derive(Debug, Parser)] | ||
pub struct FlagRemoveCommand { | ||
#[command(flatten)] | ||
pub folder: FolderNameArg, | ||
|
||
#[command(flatten)] | ||
pub args: IdsAndFlagsArgs, | ||
|
||
#[command(flatten)] | ||
pub account: AccountNameFlag, | ||
|
||
#[command(flatten)] | ||
pub cache: DisableCacheFlag, | ||
} | ||
|
||
impl FlagRemoveCommand { | ||
pub async fn execute(self, printer: &mut impl Printer, config: &TomlConfig) -> Result<()> { | ||
info!("executing flag remove command"); | ||
|
||
let folder = &self.folder.name; | ||
let account = self.account.name.as_ref().map(String::as_str); | ||
let cache = self.cache.disable; | ||
|
||
let (toml_account_config, account_config) = | ||
config.clone().into_account_configs(account, cache)?; | ||
let backend = Backend::new(toml_account_config, account_config.clone(), false).await?; | ||
|
||
let (ids, flags) = to_tuple(&self.args.ids_and_flags); | ||
backend.remove_flags(folder, &ids, &flags).await?; | ||
|
||
printer.print(format!("Flag(s) {flags} successfully removed!")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use anyhow::Result; | ||
use clap::Parser; | ||
use log::info; | ||
|
||
use crate::{ | ||
account::arg::name::AccountNameFlag, | ||
backend::Backend, | ||
cache::arg::disable::DisableCacheFlag, | ||
config::TomlConfig, | ||
flag::arg::ids_and_flags::{to_tuple, IdsAndFlagsArgs}, | ||
folder::arg::name::FolderNameArg, | ||
printer::Printer, | ||
}; | ||
|
||
/// Replace flag(s) of an envelope | ||
#[derive(Debug, Parser)] | ||
pub struct FlagSetCommand { | ||
#[command(flatten)] | ||
pub folder: FolderNameArg, | ||
|
||
#[command(flatten)] | ||
pub args: IdsAndFlagsArgs, | ||
|
||
#[command(flatten)] | ||
pub account: AccountNameFlag, | ||
|
||
#[command(flatten)] | ||
pub cache: DisableCacheFlag, | ||
} | ||
|
||
impl FlagSetCommand { | ||
pub async fn execute(self, printer: &mut impl Printer, config: &TomlConfig) -> Result<()> { | ||
info!("executing flag set command"); | ||
|
||
let folder = &self.folder.name; | ||
let account = self.account.name.as_ref().map(String::as_str); | ||
let cache = self.cache.disable; | ||
|
||
let (toml_account_config, account_config) = | ||
config.clone().into_account_configs(account, cache)?; | ||
let backend = Backend::new(toml_account_config, account_config.clone(), false).await?; | ||
|
||
let (ids, flags) = to_tuple(&self.args.ids_and_flags); | ||
backend.set_flags(folder, &ids, &flags).await?; | ||
|
||
printer.print(format!("Flag(s) {flags} successfully set!")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.