feat: Ability to edit & remove by name (not index only)
All checks were successful
Rust CI / Rust CI (pull_request) Successful in 3m20s
Write changelog / write-changelog (push) Successful in 3s
Rust CI / Rust CI (push) Successful in 1m49s

This commit is contained in:
Awiteb 2024-05-10 18:00:29 +03:00
parent 6e8c214b9e
commit d8350e636e
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F
2 changed files with 31 additions and 39 deletions

View file

@ -14,8 +14,6 @@
// 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>.
use std::num::NonZeroU64;
use clap::Args; use clap::Args;
use crate::{clap_parsers, utils, vault::Vaults, LprsCommand, LprsError, LprsResult}; use crate::{clap_parsers, utils, vault::Vaults, LprsCommand, LprsError, LprsResult};
@ -24,8 +22,9 @@ use crate::{clap_parsers, utils, vault::Vaults, LprsCommand, LprsError, LprsResu
#[command(author, version, about, long_about = None)] #[command(author, version, about, long_about = None)]
/// Edit command, used to edit the vault content /// Edit command, used to edit the vault content
pub struct Edit { pub struct Edit {
/// The password index. You can get it from the list command /// The vault to edit, index or name
index: NonZeroU64, #[arg(name = "INDEX-or-NAME")]
location: String,
#[arg(short, long)] #[arg(short, long)]
/// The new vault name /// The new vault name
@ -58,15 +57,15 @@ pub struct Edit {
impl LprsCommand for Edit { impl LprsCommand for Edit {
fn run(self, mut vault_manager: Vaults) -> LprsResult<()> { fn run(self, mut vault_manager: Vaults) -> LprsResult<()> {
let index = self.index.get() as usize; let vault =
log::debug!("Editing vault at index: {index}"); match utils::vault_by_index_or_name(self.location.trim(), &mut vault_manager.vaults) {
Ok((_, v)) => v,
let Some(vault) = vault_manager.vaults.get_mut(index - 1) else { Err(err) => {
return Err(LprsError::InvalidVaultIndex(format!( if self.force {
"The index `{}` is greater than the vaults count {}", return Ok(());
self.index, }
vault_manager.vaults.len() return Err(err);
))); }
}; };
log::info!("Applying the new values to the vault"); log::info!("Applying the new values to the vault");

View file

@ -14,44 +14,37 @@
// 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>.
use std::num::NonZeroU64;
use clap::Args; use clap::Args;
use crate::{vault::Vaults, LprsCommand, LprsError, LprsResult}; use crate::{utils, vault::Vaults, LprsCommand, LprsResult};
#[derive(Debug, Args)] #[derive(Debug, Args)]
#[command(author, version, about, long_about = None)] #[command(author, version, about, long_about = None)]
/// Remove command, used to remove a vault from the vaults file /// Remove command, used to remove a vault from the vaults file
pub struct Remove { pub struct Remove {
/// The password index /// The vault to remove, index or name
index: NonZeroU64, #[arg(name = "INDEX-or-NAME")]
location: String,
/// Force remove, will not return error if there is no password with this /// Force remove, will not return error if there is no vault with the given
/// index /// index or name
#[arg(short, long)] #[arg(short, long)]
force: bool, force: bool,
} }
impl LprsCommand for Remove { impl LprsCommand for Remove {
fn run(self, mut vault_manager: Vaults) -> LprsResult<()> { fn run(self, mut vault_manager: Vaults) -> LprsResult<()> {
let index = (self.index.get() - 1) as usize; let index =
log::debug!("Removing vault at index: {index}"); match utils::vault_by_index_or_name(self.location.trim(), &mut vault_manager.vaults) {
Ok((idx, _)) => idx,
if index > vault_manager.vaults.len() { Err(err) => {
if self.force { if self.force {
log::error!( return Ok(());
"The index is greater than the passwords counts, but the force flag is enabled"
);
} else {
return Err(LprsError::Other(
"The index is greater than the passwords counts".to_owned(),
));
} }
} else { return Err(err);
}
};
vault_manager.vaults.remove(index); vault_manager.vaults.remove(index);
vault_manager.try_export()?; vault_manager.try_export()
}
Ok(())
} }
} }