feat: Validate args before ask for the master password #17

Merged
awiteb merged 3 commits from awiteb/fix-13 into master 2024-04-29 13:39:37 +02:00 AGit
5 changed files with 85 additions and 73 deletions
Showing only changes of commit 75bf74d512 - Show all commits

View file

@ -33,14 +33,6 @@ pub struct Add {
impl LprsCommand for Add { impl LprsCommand for Add {
fn run(mut self, mut vault_manager: Vaults<Plain>) -> LprsResult<()> { fn run(mut self, mut vault_manager: Vaults<Plain>) -> LprsResult<()> {
if self.vault_info.username.is_none()
&& self.password.is_none()
&& self.vault_info.service.is_none()
&& self.vault_info.note.is_none()
{
return Err(LprsError::Other("You can't add empty vault".to_owned()));
}
match self.password { match self.password {
Some(Some(password)) => self.vault_info.password = Some(password), Some(Some(password)) => self.vault_info.password = Some(password),
Some(None) => { Some(None) => {
@ -58,4 +50,15 @@ impl LprsCommand for Add {
vault_manager.add_vault(self.vault_info); vault_manager.add_vault(self.vault_info);
vault_manager.try_export() vault_manager.try_export()
} }
fn validate_args(&self) -> LprsResult<()> {
if self.vault_info.username.is_none()
&& self.password.is_none()
&& self.vault_info.service.is_none()
&& self.vault_info.note.is_none()
{
return Err(LprsError::Other("You can't add empty vault".to_owned()));
}
Ok(())
}
} }

View file

@ -58,17 +58,6 @@ impl LprsCommand for Edit {
))); )));
}; };
if self.name.is_none()
&& self.username.is_none()
&& self.password.is_none()
&& self.service.is_none()
&& self.note.is_none()
{
return Err(LprsError::Other(
"You must edit one option at least".to_owned(),
));
}
// Get the password from stdin or from its value if provided // Get the password from stdin or from its value if provided
let password = match self.password { let password = match self.password {
Some(Some(password)) => Some(password), Some(Some(password)) => Some(password),
@ -91,4 +80,18 @@ impl LprsCommand for Edit {
); );
vault_manager.try_export() vault_manager.try_export()
} }
fn validate_args(&self) -> LprsResult<()> {
if self.name.is_none()
&& self.username.is_none()
&& self.password.is_none()
&& self.service.is_none()
&& self.note.is_none()
{
return Err(LprsError::Other(
"You must edit one option at least".to_owned(),
));
}
Ok(())
}
} }

View file

@ -35,22 +35,24 @@ pub struct Export {
impl LprsCommand for Export { impl LprsCommand for Export {
fn run(self, vault_manager: Vaults<Plain>) -> LprsResult<()> { fn run(self, vault_manager: Vaults<Plain>) -> LprsResult<()> {
let exported_data = match self.format {
Format::Lprs => {
serde_json::to_string::<Vec<Vault<Encrypted>>>(&vault_manager.encrypt_vaults()?)
}
Format::BitWarden => serde_json::to_string(&BitWardenPasswords::from(vault_manager)),
}?;
fs::write(&self.path, exported_data).map_err(LprsError::from)
}
fn validate_args(&self) -> LprsResult<()> {
if self if self
.path .path
.extension() .extension()
.is_some_and(|e| e.to_string_lossy().eq_ignore_ascii_case("json")) .is_some_and(|e| e.to_string_lossy().eq_ignore_ascii_case("json"))
{ {
if !self.path.exists() { if !self.path.exists() {
let exported_data = match self.format { Ok(())
Format::Lprs => serde_json::to_string::<Vec<Vault<Encrypted>>>(
&vault_manager.encrypt_vaults()?,
),
Format::BitWarden => {
serde_json::to_string(&BitWardenPasswords::from(vault_manager))
}
}?;
fs::write(&self.path, exported_data).map_err(LprsError::from)
} else { } else {
Err(LprsError::Io(IoError::new( Err(LprsError::Io(IoError::new(
IoErrorKind::AlreadyExists, IoErrorKind::AlreadyExists,

View file

@ -46,24 +46,27 @@ pub struct Gen {
impl LprsCommand for Gen { impl LprsCommand for Gen {
fn run(self, _vault_manager: Vaults<Plain>) -> LprsResult<()> { fn run(self, _vault_manager: Vaults<Plain>) -> LprsResult<()> {
if self.uppercase || self.lowercase || self.numbers || self.symbols { println!(
println!( "{}",
"{}", passwords::PasswordGenerator::new()
passwords::PasswordGenerator::new() .length(self.length.get() as usize)
.length(self.length.get() as usize) .uppercase_letters(self.uppercase)
.uppercase_letters(self.uppercase) .lowercase_letters(self.lowercase)
.lowercase_letters(self.lowercase) .numbers(self.numbers)
.numbers(self.numbers) .symbols(self.symbols)
.symbols(self.symbols) .strict(true)
.strict(true) .generate_one()
.generate_one() .expect("The length cannot be zero")
.expect("The length cannot be zero") );
); Ok(())
Ok(()) }
} else {
Err(LprsError::Other( fn validate_args(&self) -> LprsResult<()> {
if !(self.uppercase || self.lowercase || self.numbers || self.symbols) {
return Err(LprsError::Other(
"You need to enable at least one kind of characters".to_owned(), "You need to enable at least one kind of characters".to_owned(),
)) ));
} }
Ok(())
} }
} }

View file

@ -36,39 +36,40 @@ pub struct Import {
impl LprsCommand for Import { impl LprsCommand for Import {
fn run(self, mut vault_manager: Vaults<Plain>) -> LprsResult<()> { fn run(self, mut vault_manager: Vaults<Plain>) -> LprsResult<()> {
let imported_passwords_len = match self.format {
Format::Lprs => {
let vaults = Vaults::try_reload(self.path, vault_manager.master_password.to_vec())?;
let vaults_len = vaults.vaults.len();
vault_manager.vaults.extend(vaults.vaults);
vault_manager.try_export()?;
vaults_len
}
Format::BitWarden => {
let vaults: BitWardenPasswords = serde_json::from_reader(File::open(&self.path)?)?;
let vaults_len = vaults.items.len();
vault_manager
.vaults
.extend(vaults.items.into_iter().map(Vault::from));
vault_manager.try_export()?;
vaults_len
}
};
println!(
"{imported_passwords_len} vault{s} were imported successfully",
s = if imported_passwords_len >= 2 { "s" } else { "" }
);
Ok(())
}
fn validate_args(&self) -> LprsResult<()> {
if self.path.exists() { if self.path.exists() {
if self if self
.path .path
.extension() .extension()
.is_some_and(|e| e.to_string_lossy().eq_ignore_ascii_case("json")) .is_some_and(|e| e.to_string_lossy().eq_ignore_ascii_case("json"))
{ {
let imported_passwords_len = match self.format {
Format::Lprs => {
let vaults =
Vaults::try_reload(self.path, vault_manager.master_password.to_vec())?;
let vaults_len = vaults.vaults.len();
vault_manager.vaults.extend(vaults.vaults);
vault_manager.try_export()?;
vaults_len
}
Format::BitWarden => {
let vaults: BitWardenPasswords =
serde_json::from_reader(File::open(&self.path)?)?;
let vaults_len = vaults.items.len();
vault_manager
.vaults
.extend(vaults.items.into_iter().map(Vault::from));
vault_manager.try_export()?;
vaults_len
}
};
println!(
"{imported_passwords_len} vault{s} were imported successfully",
s = if imported_passwords_len >= 2 { "s" } else { "" }
);
Ok(()) Ok(())
} else { } else {
Err(LprsError::Io(IoError::new( Err(LprsError::Io(IoError::new(