Optimize empty password checking
This commit is contained in:
parent
3cba92c494
commit
1099a80c79
3 changed files with 32 additions and 15 deletions
|
@ -2,7 +2,7 @@ use clap::{Arg, ArgMatches};
|
|||
|
||||
use cmd::matcher::{MainMatcher, Matcher};
|
||||
use super::{CmdArg, CmdArgFlag, CmdArgOption};
|
||||
use util::{ErrorHintsBuilder, quit_error_msg, prompt_password};
|
||||
use util::{check_empty_password, prompt_password};
|
||||
|
||||
/// The password argument.
|
||||
pub struct ArgPassword { }
|
||||
|
@ -44,17 +44,8 @@ impl<'a> CmdArgOption<'a> for ArgPassword {
|
|||
None => prompt_password(&matcher_main),
|
||||
};
|
||||
|
||||
// Do not allow empty passwords unless forced
|
||||
if !matcher_main.force() && password.is_empty() {
|
||||
quit_error_msg(
|
||||
"An empty password is not supported by the web interface",
|
||||
ErrorHintsBuilder::default()
|
||||
.force(true)
|
||||
.verbose(false)
|
||||
.build()
|
||||
.unwrap(),
|
||||
)
|
||||
}
|
||||
// Check for empty passwords
|
||||
check_empty_password(&password, &matcher_main);
|
||||
|
||||
Some(password)
|
||||
}
|
||||
|
|
|
@ -3,7 +3,8 @@ use ffsend_api::url::Url;
|
|||
use rpassword::prompt_password_stderr;
|
||||
|
||||
use cmd::arg::{ArgOwner, ArgPassword, ArgUrl, CmdArgOption};
|
||||
use super::Matcher;
|
||||
use cmd::matcher::{MainMatcher, Matcher};
|
||||
use util::check_empty_password;
|
||||
|
||||
/// The password command matcher.
|
||||
pub struct PasswordMatcher<'a> {
|
||||
|
@ -30,7 +31,7 @@ impl<'a: 'b, 'b> PasswordMatcher<'a> {
|
|||
/// Get the password.
|
||||
pub fn password(&'a self) -> String {
|
||||
// Get the password, or prompt for it
|
||||
match ArgPassword::value(self.matches) {
|
||||
let password = match ArgPassword::value(self.matches) {
|
||||
Some(password) => password,
|
||||
None => {
|
||||
// Prompt for the password
|
||||
|
@ -39,7 +40,15 @@ impl<'a: 'b, 'b> PasswordMatcher<'a> {
|
|||
prompt_password_stderr("New password: ")
|
||||
.expect("failed to read password from stdin")
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
// Create a main matcher
|
||||
let matcher_main = MainMatcher::with(self.matches).unwrap();
|
||||
|
||||
// Check for empty passwords
|
||||
check_empty_password(&password, &matcher_main);
|
||||
|
||||
password
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -163,6 +163,23 @@ pub fn set_clipboard(content: String) -> Result<(), Box<StdError>> {
|
|||
context.set_contents(content)
|
||||
}
|
||||
|
||||
/// Check for an emtpy password in the given `password`.
|
||||
/// If the password is emtpy the program will quit with an error unless
|
||||
/// forced.
|
||||
// TODO: move this to a better module
|
||||
pub fn check_empty_password(password: &str, matcher_main: &MainMatcher) {
|
||||
if !matcher_main.force() && password.is_empty() {
|
||||
quit_error_msg(
|
||||
"An empty password is not supported by the web interface",
|
||||
ErrorHintsBuilder::default()
|
||||
.force(true)
|
||||
.verbose(false)
|
||||
.build()
|
||||
.unwrap(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Prompt the user to enter a password.
|
||||
///
|
||||
/// If `empty` is `false`, emtpy passwords aren't allowed unless forced.
|
||||
|
|
Loading…
Reference in a new issue