From 02fd8d100bd2003718a49059d3ec453777e6f683 Mon Sep 17 00:00:00 2001 From: TheAwiteb Date: Mon, 25 Dec 2023 04:41:16 +0300 Subject: [PATCH] Add `remove` command --- src/cli/mod.rs | 3 ++- src/cli/remove_command.rs | 49 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/cli/remove_command.rs diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 8fe71f2..f541683 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -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 ); diff --git a/src/cli/remove_command.rs b/src/cli/remove_command.rs new file mode 100644 index 0000000..1c80414 --- /dev/null +++ b/src/cli/remove_command.rs @@ -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 . + +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(()) + } +}