use clap::ArgMatches;
use ffsend_api::url::Url;
use rpassword::prompt_password_stderr;
use crate::cmd::arg::{ArgGenPassphrase, ArgOwner, ArgPassword, ArgUrl, CmdArgFlag, CmdArgOption};
use crate::cmd::matcher::{MainMatcher, Matcher};
use crate::util::check_empty_password;
pub struct PasswordMatcher<'a> {
matches: &'a ArgMatches<'a>,
}
impl<'a: 'b, 'b> PasswordMatcher<'a> {
pub fn url(&'a self) -> Url {
ArgUrl::value(self.matches)
}
pub fn owner(&'a self) -> Option<String> {
ArgOwner::value(self.matches).map(|token| token.to_owned())
}
pub fn password(&'a self) -> (String, bool) {
if ArgGenPassphrase::is_present(self.matches) {
return (ArgGenPassphrase::gen_passphrase(), true);
}
let password = match ArgPassword::value(self.matches) {
Some(password) => password,
None => {
prompt_password_stderr("New password: ")
.expect("failed to read password from stdin")
}
};
let matcher_main = MainMatcher::with(self.matches).unwrap();
check_empty_password(&password, &matcher_main);
(password, false)
}
}
impl<'a> Matcher<'a> for PasswordMatcher<'a> {
fn with(matches: &'a ArgMatches) -> Option<Self> {
matches
.subcommand_matches("password")
.map(|matches| PasswordMatcher { matches })
}
}