Cli:: add prompt_for_password() helper method
This commit is contained in:
parent
f79be675fb
commit
2fb7be9941
1 changed files with 20 additions and 0 deletions
20
src/Cli.php
20
src/Cli.php
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue