Skip to content

Commit

Permalink
Add --database option
Browse files Browse the repository at this point in the history
  • Loading branch information
CasEbb committed Mar 12, 2024
1 parent f27d366 commit 1fb0802
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ protected function configure()
->addOption('branch', null, InputOption::VALUE_REQUIRED, 'The branch that should be created for a new repository', $this->defaultBranch())
->addOption('github', null, InputOption::VALUE_OPTIONAL, 'Create a new repository on GitHub', false)
->addOption('organization', null, InputOption::VALUE_REQUIRED, 'The GitHub organization to create the new repository for')
->addOption('database', null, InputOption::VALUE_REQUIRED, 'The database driver that should be configured')
->addOption('stack', null, InputOption::VALUE_OPTIONAL, 'The Breeze / Jetstream stack that should be installed')
->addOption('breeze', null, InputOption::VALUE_NONE, 'Installs the Laravel Breeze scaffolding')
->addOption('jet', null, InputOption::VALUE_NONE, 'Installs the Laravel Jetstream scaffolding')
Expand Down Expand Up @@ -137,6 +138,8 @@ protected function interact(InputInterface $input, OutputInterface $output)
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->validateDatabaseOption($input);

$this->validateStackOption($input);

$name = $input->getArgument('name');
Expand Down Expand Up @@ -449,8 +452,8 @@ protected function promptForDatabaseOptions(string $directory, InputInterface $i
{
$defaultDatabase = 'sqlite';

if ($input->isInteractive()) {
$database = select(
if (! $input->getOption('database') && $input->isInteractive()) {
$input->setOption('database', select(
label: 'Which database will your application use?',
options: [
'mysql' => 'MySQL',
Expand All @@ -460,20 +463,19 @@ protected function promptForDatabaseOptions(string $directory, InputInterface $i
'sqlsrv' => 'SQL Server',
],
default: $defaultDatabase
);
));

if ($database !== $defaultDatabase) {
if ($input->getOption('database') !== $defaultDatabase) {
$migrate = confirm(label: 'Default database updated. Would you like to run the default database migrations?', default: true);
}
}

return [$database ?? $defaultDatabase, $migrate ?? false];
return [$input->getOption('database') ?? $defaultDatabase, $migrate ?? false];
}

/**
* Determine the stack for Breeze.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @return void
*/
protected function promptForBreezeOptions(InputInterface $input)
Expand Down Expand Up @@ -555,6 +557,18 @@ protected function promptForJetstreamOptions(InputInterface $input)
))->each(fn ($option) => $input->setOption($option, true));
}

/**
* Validate the database driver input.
*
* @param \Symfony\Components\Console\Input\InputInterface
*/
protected function validateDatabaseOption(InputInterface $input)
{
if ($input->getOption('database') && ! in_array($input->getOption('database'), $drivers = ['mysql', 'mariadb', 'pgsql', 'sqlite', 'sqlsrv'])) {
throw new \InvalidArgumentException("Invalid database driver [{$input->getOption('database')}]. Valid options are: ".implode(', ', $drivers).'.');
}
}

/**
* Validate the starter kit stack input.
*
Expand Down

0 comments on commit 1fb0802

Please sign in to comment.