Compare commits

..

No commits in common. "d8350e636e733c6d49d46d95e0c3ca6c403d72c5" and "095aa37219dbacbdad78d4bf38e68efe3b3ad173" have entirely different histories.

5 changed files with 60 additions and 68 deletions

View file

@ -50,7 +50,6 @@ pub struct Add {
impl LprsCommand for Add { impl LprsCommand for Add {
fn run(mut self, mut vault_manager: Vaults) -> LprsResult<()> { fn run(mut self, mut vault_manager: Vaults) -> LprsResult<()> {
if !self.vault_info.is_empty() { if !self.vault_info.is_empty() {
self.vault_info.name = self.vault_info.name.trim().to_string();
self.vault_info.password = utils::user_password(self.password, "Vault password:")?; self.vault_info.password = utils::user_password(self.password, "Vault password:")?;
self.vault_info.custom_fields = self.custom_fields.into_iter().collect(); self.vault_info.custom_fields = self.custom_fields.into_iter().collect();
vault_manager.add_vault(self.vault_info); vault_manager.add_vault(self.vault_info);

View file

@ -14,6 +14,8 @@
// 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};
@ -22,9 +24,8 @@ 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 vault to edit, index or name /// The password index. You can get it from the list command
#[arg(name = "INDEX-or-NAME")] index: NonZeroU64,
location: String,
#[arg(short, long)] #[arg(short, long)]
/// The new vault name /// The new vault name
@ -57,16 +58,16 @@ 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 vault = let index = self.index.get() as usize;
match utils::vault_by_index_or_name(self.location.trim(), &mut vault_manager.vaults) { log::debug!("Editing vault at index: {index}");
Ok((_, v)) => v,
Err(err) => { let Some(vault) = vault_manager.vaults.get_mut(index - 1) else {
if self.force { return Err(LprsError::InvalidVaultIndex(format!(
return Ok(()); "The index `{}` is greater than the vaults count {}",
} self.index,
return Err(err); vault_manager.vaults.len()
} )));
}; };
log::info!("Applying the new values to the vault"); log::info!("Applying the new values to the vault");
if let Some(new_name) = self.name { if let Some(new_name) = self.name {

View file

@ -19,7 +19,6 @@ use std::str::FromStr;
use clap::Args; use clap::Args;
use crate::{ use crate::{
utils,
vault::{Vault, Vaults}, vault::{Vault, Vaults},
LprsCommand, LprsCommand,
LprsError, LprsError,
@ -97,9 +96,27 @@ pub struct Get {
} }
impl LprsCommand for Get { impl LprsCommand for Get {
fn run(self, mut vault_manager: Vaults) -> LprsResult<()> { fn run(self, vault_manager: Vaults) -> LprsResult<()> {
let (index, vault) = let parsed_index = self.location.trim().parse::<usize>();
utils::vault_by_index_or_name(self.location.trim(), &mut vault_manager.vaults)?; let Some((index, vault)) = (if let Ok(index) = parsed_index {
vault_manager.vaults.get(index - 1).map(|v| (index, v))
} else {
vault_manager
.vaults
.iter()
.enumerate()
.find(|(_, v)| v.name == self.location)
}) else {
return Err(LprsError::Other(format!(
"There is no vault with the given {} `{}`",
if parsed_index.is_ok() {
"index"
} else {
"name"
},
self.location.trim(),
)));
};
if let Some(field) = self.field { if let Some(field) = self.field {
if field == VaultGetField::Index { if field == VaultGetField::Index {

View file

@ -14,37 +14,44 @@
// 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::{utils, vault::Vaults, LprsCommand, LprsResult}; use crate::{vault::Vaults, LprsCommand, LprsError, 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 vault to remove, index or name /// The password index
#[arg(name = "INDEX-or-NAME")] index: NonZeroU64,
location: String,
/// Force remove, will not return error if there is no vault with the given /// Force remove, will not return error if there is no password with this
/// index or name /// index
#[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 = let index = (self.index.get() - 1) as usize;
match utils::vault_by_index_or_name(self.location.trim(), &mut vault_manager.vaults) { log::debug!("Removing vault at index: {index}");
Ok((idx, _)) => idx,
Err(err) => { if index > vault_manager.vaults.len() {
if self.force { if self.force {
return Ok(()); log::error!(
} "The index is greater than the passwords counts, but the force flag is enabled"
return Err(err); );
} } else {
}; return Err(LprsError::Other(
vault_manager.vaults.remove(index); "The index is greater than the passwords counts".to_owned(),
vault_manager.try_export() ));
}
} else {
vault_manager.vaults.remove(index);
vault_manager.try_export()?;
}
Ok(())
} }
} }

View file

@ -23,7 +23,6 @@ use passwords::{analyzer, scorer};
use reqwest::blocking::Client as BlockingClient; use reqwest::blocking::Client as BlockingClient;
use sha2::Digest; use sha2::Digest;
use crate::vault::Vault;
use crate::{LprsError, LprsResult}; use crate::{LprsError, LprsResult};
/// Returns the local project dir joined with the given file name /// Returns the local project dir joined with the given file name
@ -206,34 +205,3 @@ pub fn apply_custom_fields(
} }
} }
} }
/// Returns the vault with its index by its index or name
///
/// ## Errors
/// - If there is no vault with the given index or name
pub fn vault_by_index_or_name<'a>(
index_or_name: &str,
vaults: &'a mut [Vault],
) -> LprsResult<(usize, &'a mut Vault)> {
let parsed_index = index_or_name.parse::<usize>();
let Some((index, vault)) = (if let Ok(index) = parsed_index {
vaults.get_mut(index - 1).map(|v| (index, v))
} else {
vaults
.iter_mut()
.enumerate()
.find(|(_, v)| v.name == index_or_name)
}) else {
return Err(LprsError::Other(format!(
"There is no vault with the given {} `{}`",
if parsed_index.is_ok() {
"index"
} else {
"name"
},
index_or_name,
)));
};
Ok((index, vault))
}