diff --git a/cli/src/cmd/arg/password.rs b/cli/src/cmd/arg/password.rs index 7351eae..7bdc544 100644 --- a/cli/src/cmd/arg/password.rs +++ b/cli/src/cmd/arg/password.rs @@ -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) } diff --git a/cli/src/cmd/matcher/password.rs b/cli/src/cmd/matcher/password.rs index 550ad24..db541f7 100644 --- a/cli/src/cmd/matcher/password.rs +++ b/cli/src/cmd/matcher/password.rs @@ -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 } } diff --git a/cli/src/util.rs b/cli/src/util.rs index fa13427..5740178 100644 --- a/cli/src/util.rs +++ b/cli/src/util.rs @@ -163,6 +163,23 @@ pub fn set_clipboard(content: String) -> Result<(), Box> { 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.