Skip to content

Commit

Permalink
fix: validate required cli install args
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Oct 24, 2024
1 parent 4d8e6ab commit f8f2e6e
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions framework/core/src/Install/Console/UserDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Flarum\Install\BaseUrl;
use Flarum\Install\DatabaseConfig;
use Flarum\Install\Installation;
use Flarum\Install\ValidationFailed;
use Illuminate\Support\Str;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -50,21 +51,21 @@ private function getDatabaseConfiguration(): DatabaseConfig
};

if (in_array($driver, ['mysql', 'pgsql'])) {
$host = $this->ask('Database host (required):');
$host = $this->ask('Database host (required):', required: true);

if (Str::contains($host, ':')) {
list($host, $port) = explode(':', $host, 2);
}

$user = $this->ask('Database user (required):');
$user = $this->ask('Database user (required):', required: true);
$password = $this->secret('Database password:');
}

return new DatabaseConfig(
$driver,
$host ?? null,
intval($port),
$this->ask('Database name (required):'),
$this->ask('Database name (required):', required: true),
$user ?? null,
$password ?? null,
$this->ask('Prefix:')
Expand All @@ -83,7 +84,7 @@ private function getAdminUser(): AdminUser
return new AdminUser(
$this->ask('Admin username (Default: admin):', 'admin'),
$this->askForAdminPassword(),
$this->ask('Admin email address (required):')
$this->ask('Admin email address (required):', required: true)
);
}

Expand Down Expand Up @@ -119,10 +120,20 @@ private function getSettings(): array
];
}

private function ask(string $question, string $default = null): mixed
private function ask(string $question, string $default = null, bool $required = false): mixed
{
$question = new Question("<question>$question</question> ", $default);

if ($required) {
$question->setValidator(function ($value) {
if (empty($value)) {
throw new ValidationFailed('This value is required');
}

return $value;
});
}

return $this->questionHelper->ask($this->input, $this->output, $question);
}

Expand Down

0 comments on commit f8f2e6e

Please sign in to comment.