feat: Validate args before ask for the master password
All checks were successful
Check PR title / check_pr_title (pull_request) Successful in 3s
Rust CI / Rust CI (pull_request) Successful in 1m19s

Fixes: #13
This commit is contained in:
Awiteb 2024-04-25 08:31:33 +03:00
parent a6870b2dca
commit 75bf74d512
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F
5 changed files with 85 additions and 73 deletions

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,7 +46,6 @@ 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()
@ -60,10 +59,14 @@ impl LprsCommand for Gen {
.expect("The length cannot be zero") .expect("The length cannot be zero")
); );
Ok(()) Ok(())
} else {
Err(LprsError::Other(
"You need to enable at least one kind of characters".to_owned(),
))
} }
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(),
));
}
Ok(())
} }
} }

View file

@ -36,16 +36,9 @@ 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<()> {
if self.path.exists() {
if self
.path
.extension()
.is_some_and(|e| e.to_string_lossy().eq_ignore_ascii_case("json"))
{
let imported_passwords_len = match self.format { let imported_passwords_len = match self.format {
Format::Lprs => { Format::Lprs => {
let vaults = let vaults = Vaults::try_reload(self.path, vault_manager.master_password.to_vec())?;
Vaults::try_reload(self.path, vault_manager.master_password.to_vec())?;
let vaults_len = vaults.vaults.len(); let vaults_len = vaults.vaults.len();
vault_manager.vaults.extend(vaults.vaults); vault_manager.vaults.extend(vaults.vaults);
@ -53,8 +46,7 @@ impl LprsCommand for Import {
vaults_len vaults_len
} }
Format::BitWarden => { Format::BitWarden => {
let vaults: BitWardenPasswords = let vaults: BitWardenPasswords = serde_json::from_reader(File::open(&self.path)?)?;
serde_json::from_reader(File::open(&self.path)?)?;
let vaults_len = vaults.items.len(); let vaults_len = vaults.items.len();
vault_manager vault_manager
@ -68,7 +60,16 @@ impl LprsCommand for Import {
"{imported_passwords_len} vault{s} were imported successfully", "{imported_passwords_len} vault{s} were imported successfully",
s = if imported_passwords_len >= 2 { "s" } else { "" } s = if imported_passwords_len >= 2 { "s" } else { "" }
); );
Ok(())
}
fn validate_args(&self) -> LprsResult<()> {
if self.path.exists() {
if self
.path
.extension()
.is_some_and(|e| e.to_string_lossy().eq_ignore_ascii_case("json"))
{
Ok(()) Ok(())
} else { } else {
Err(LprsError::Io(IoError::new( Err(LprsError::Io(IoError::new(