Skip to content

Commit

Permalink
Add db password show (#555)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischabraam committed Feb 19, 2024
1 parent 6cf2403 commit c88962f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
28 changes: 16 additions & 12 deletions cli/ValetPlus/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
);
Expand All @@ -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" .
' <fg=yellow>valet-plus db password --show</>' . "\n" .
"\n" .
'And try to set the root password manually with the following command:' . "\n" .
' <fg=yellow>valet-plus db password <old> <new></>' . "\n"
);
}
if ($success !== false) {
$this->setConfigRootPassword($newPwd);
}
Expand Down Expand Up @@ -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'])) {
Expand Down
15 changes: 13 additions & 2 deletions cli/valet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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('<fg=yellow>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 <old> <new>"');
}

Expand Down

0 comments on commit c88962f

Please sign in to comment.