From 2fb7be9941f15265980daccdfd22eb2c2128e9b8 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Tue, 28 Feb 2023 01:44:43 +0100 Subject: [PATCH] Cli:: add prompt_for_password() helper method --- src/Cli.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/Cli.php b/src/Cli.php index 5c09aa5..b687e56 100644 --- a/src/Cli.php +++ b/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; + } + }