Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add db password show (#555) #630

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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