feat: Support rm and ls aliases (#22)
All checks were successful
Write changelog / write-changelog (push) Successful in 3s
Rust CI / Rust CI (push) Successful in 1m15s

Reviewed-on: #22
Fixes: #18
Co-authored-by: Awiteb <a@4rs.nl>
Co-committed-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb 2024-04-29 23:50:45 +02:00 committed by awiteb
parent c8139a1bbc
commit 791d390e63
2 changed files with 38 additions and 35 deletions

View file

@ -18,7 +18,7 @@ use std::path::PathBuf;
use clap::Parser; use clap::Parser;
use crate::{utils, vault::Vaults, LprsCommand, LprsResult}; use crate::{impl_commands, utils, vault::Vaults, LprsCommand, LprsResult};
pub mod add_command; pub mod add_command;
pub mod clean_command; pub mod clean_command;
@ -29,17 +29,30 @@ pub mod import_command;
pub mod list_command; pub mod list_command;
pub mod remove_command; pub mod remove_command;
crate::create_commands!( /// The lprs commands
enum Commands #[derive(Debug, clap::Subcommand)]
"Add new vault", Add => add_command::Add pub enum Commands {
"Remove vault", Remove => remove_command::Remove /// Add new vault
"List your vaults and search", List => list_command::List Add(add_command::Add),
"Clean the vaults file", Clean => clean_command::Clean /// Remove vault [alias `rm`]
"Edit the vault content", Edit => edit_command::Edit #[command(alias = "rm")]
"Generate a password", Gen => gen_command::Gen Remove(remove_command::Remove),
"Export the vaults", Export => export_command::Export /// List your vaults and search [alias `ls`]
"Import vaults", Import => import_command::Import #[command(alias = "ls")]
); List(list_command::List),
/// Clean the vaults file
Clean(clean_command::Clean),
/// Edit the vault content
Edit(edit_command::Edit),
/// Generate a password
Gen(gen_command::Gen),
/// Export the vaults
Export(export_command::Export),
/// Import vaults
Import(import_command::Import),
}
impl_commands!(Commands, Add Remove List Clean Edit Gen Export Import);
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)] #[command(author, version, about, long_about = None)]

View file

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>. // along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>.
/// Creates commands macro, to create the `Commands` enum and impl `LprsCommand` to it. /// Impl `LprsCommand` to the given subcommand.
/// ///
/// ### Notes: /// ### Notes:
/// The `$command` must impl `LprsCommand` trait /// The `$command` must impl `LprsCommand` trait
@ -22,21 +22,12 @@
/// ### Example: /// ### Example:
/// ```rust /// ```rust
/// create_commands!( /// create_commands!(
/// enum TestCommands /// TestCommands,
/// "Test command", Test => TestArgs /// Test Some
/// "Do something", Some => SomeArgs
/// ); /// );
/// ``` /// ```
/// #### Output /// #### Output
/// ```rust /// ```rust
/// ///The lprs commands
/// pub enum TestCommands {
/// ///Test command
/// Test(TestArgs),
/// ///Do something
/// Some(SomeArgs),
/// }
///
/// impl crate::LprsCommand for TestCommands { /// impl crate::LprsCommand for TestCommands {
/// fn run( /// fn run(
/// &self, /// &self,
@ -47,20 +38,19 @@
/// Self::Some(command) => command.run(vault_manager), /// Self::Some(command) => command.run(vault_manager),
/// } /// }
/// } /// }
///
/// fn validate_args(&self) -> crate::LprsResult<()> {
/// match self {
/// Self::Test(command) => command.validate_args(),
/// Self::Some(command) => command.validate_args(),
/// } /// }
/// }
/// }
/// ``` /// ```
#[macro_export] #[macro_export]
macro_rules! create_commands { macro_rules! impl_commands {
(enum $enum_name: ident $($doc:tt, $varint: ident => $command: ty)+) => { ($enum_name: ident, $($varint: ident)+) => {
#[doc = "The lprs commands"]
#[derive(Debug, clap::Subcommand)]
pub enum $enum_name {
$(
#[doc = $doc]
$varint($command),
)+
}
#[automatically_derived] #[automatically_derived]
impl $crate::LprsCommand for $enum_name{ impl $crate::LprsCommand for $enum_name{
fn run(self, vault_manager: $crate::vault::Vaults<$crate::vault::vault_state::Plain>) -> $crate::LprsResult<()> { fn run(self, vault_manager: $crate::vault::Vaults<$crate::vault::vault_state::Plain>) -> $crate::LprsResult<()> {