Cli:: add prompt_for_password() helper method

This commit is contained in:
Benjamin Renard 2023-02-28 01:44:43 +01:00
parent f79be675fb
commit 2fb7be9941

View file

@ -341,4 +341,24 @@ class Cli {
exit($exit_code);
}
/**
* Helper method to ask user to enter a password
* Note: this method use the bash binary and a fatal error will be trigger if it's not available.
* @param string|null $prompt The prompt message (optional)
* @return string
*/
public static function prompt_for_password($prompt=null) {
// Check bash is available
$command = "/usr/bin/env bash -c 'echo OK'";
if (rtrim(shell_exec($command)) !== 'OK')
Log::fatal(I18n::_("Can't invoke bash. Can't ask password prompt."));
$command = "/usr/bin/env bash -c 'read -s -p \"";
$command .= addslashes($prompt?_($prompt):I18n::_("Please enter password:"));
$command .= "\" mypassword && echo \$mypassword'";
$password = rtrim(shell_exec($command));
echo "\n";
return $password;
}
}