Add remove command

This commit is contained in:
TheAwiteb 2023-12-25 04:41:16 +03:00
parent be1ab5d527
commit 02fd8d100b
No known key found for this signature in database
GPG key ID: ABF818BD15DC2D34
2 changed files with 51 additions and 1 deletions

View file

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