From c88962f600f66ee24383c19c4241ea2846a760e6 Mon Sep 17 00:00:00 2001 From: Mischa Braam Date: Fri, 16 Feb 2024 15:58:00 +0100 Subject: [PATCH] Add db password show (#555) --- cli/ValetPlus/Mysql.php | 28 ++++++++++++++++------------ cli/valet.php | 15 +++++++++++++-- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/cli/ValetPlus/Mysql.php b/cli/ValetPlus/Mysql.php index 502ca839..f8648046 100644 --- a/cli/ValetPlus/Mysql.php +++ b/cli/ValetPlus/Mysql.php @@ -13,6 +13,7 @@ use Valet\Site; use function Valet\info; +use function Valet\output; use function Valet\table; use function Valet\tap; use function Valet\warning; @@ -200,20 +201,10 @@ public function setRootPassword($oldPwd = '', $newPwd = self::MYSQL_ROOT_PASSWOR switch ($version) { case 'mariadb': - $this->cli->run( - "mysqladmin -u root --password='" . $oldPwd . "' password " . $newPwd, - function () use (&$success) { - warning('Setting mysql password for root user failed. '); - $success = false; - } - ); - break; - case 'mysql@5.7': $this->cli->runAsUser( "mysqladmin -u root --password='" . $oldPwd . "' password " . $newPwd, function () use (&$success) { - warning('Setting mysql password for root user failed. '); $success = false; } ); @@ -227,12 +218,25 @@ function () use (&$success) { true ); if (!$retval) { - warning('Setting mysql password for root user failed. '); $success = false; } break; } + if ($success === false) { + warning( + "\n" . + 'Setting mysql password for root user failed.' + ); + output( + "\n" . + 'You can show the current configured password with the following command:' . "\n" . + ' valet-plus db password --show' . "\n" . + "\n" . + 'And try to set the root password manually with the following command:' . "\n" . + ' valet-plus db password ' . "\n" + ); + } if ($success !== false) { $this->setConfigRootPassword($newPwd); } @@ -510,7 +514,7 @@ protected function removeConfiguration() /** * Returns the stored password from the config. If not configured returns the default root password. */ - protected function getConfigRootPassword() + public function getConfigRootPassword() { $config = $this->configuration->read(); if (isset($config['mysql']) && isset($config['mysql']['password'])) { diff --git a/cli/valet.php b/cli/valet.php index 4b237b6a..e768c803 100755 --- a/cli/valet.php +++ b/cli/valet.php @@ -8,6 +8,7 @@ use Symfony\Component\Console\Question\ConfirmationQuestion; use function Valet\info; +use function Valet\output; use function Valet\table; use function Valet\warning; @@ -408,7 +409,7 @@ * Database services and commands. */ $app - ->command('db [run] [name] [optional] [-y|--yes]', function ($input, $output, $run, $name, $optional) { + ->command('db [run] [name] [optional] [-y|--yes] [-s|--show]', function ($input, $output, $run, $name, $optional) { $helper = $this->getHelperSet()->get('question'); $defaults = $input->getOptions(); @@ -524,7 +525,17 @@ } if ($run === 'pwd' || $run === 'password') { - if (!$name || !$optional) { + if ($defaults['show']) { + $question = new ConfirmationQuestion('Are you sure you want to show the configured root password? [y/N] ', false); + if ($defaults['yes'] || $helper->ask($input, $output, $question)) { + info('Current configured password for root user: ' . Mysql::getConfigRootPassword()); + output('Please note this is the password as configured in Valet+!'); + } + + return; + } + + if ($name === null || $optional === null) { throw new Exception('Missing arguments to change root user password. Use: "valet db pwd "'); }