Merge pull request #4 from TheAwiteb/remove-command

Remove command
This commit is contained in:
Mohammed Alotaibi 2023-12-25 04:42:37 +03:00 committed by GitHub
commit a35227ff43
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 1 deletions

View file

@ -32,6 +32,7 @@ Usage: passrs [OPTIONS] <COMMAND>
Commands:
add Add new password
remove Remove password
list List your password and search
clean Clean the password file
edit Edit the password content

View file

@ -28,15 +28,16 @@ pub mod clean_command;
pub mod edit_command;
pub mod gen_command;
pub mod list_command;
pub mod remove_command;
crate::create_commands!(
enum Commands
"Add new password", Add => add_command::Add
"Remove password", Remove => remove_command::Remove
"List your password and search", List => list_command::List
"Clean the password file", Clean => clean_command::Clean
"Edit the password content", Edit => edit_command::Edit
"Generate password", Gen => gen_command::Gen
// TODO: Remove command
// TODO: Export command
// TODO: Import command
);

49
src/cli/remove_command.rs Normal file
View file

@ -0,0 +1,49 @@
// Local CLI password manager
// Copyright (C) 2024 Awiteb
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// 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>.
use std::num::NonZeroU64;
use clap::Args;
use crate::{password::Passwords, PassrsError, PassrsResult, RunCommand};
#[derive(Debug, Args)]
#[command(author, version, about, long_about = None)]
pub struct Remove {
/// The password index
index: NonZeroU64,
/// Force remove, will not return error if there is no password with this index
#[arg(short, long)]
force: bool,
}
impl RunCommand for Remove {
fn run(&self, mut password_manager: Passwords) -> PassrsResult<()> {
let index = (self.index.get() - 1) as usize;
if index > password_manager.passwords.len() {
if !self.force {
return Err(PassrsError::Other(
"The index is greater than the passwords counts".to_owned(),
));
}
} else {
password_manager.passwords.remove(index);
password_manager.try_export()?;
}
Ok(())
}
}