Skip to content

Commit

Permalink
Add --database option (#320)
Browse files Browse the repository at this point in the history
* Add `--database` option

* formatting

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
CasEbb and taylorotwell authored Mar 14, 2024
1 parent 8612fb7 commit fec8b4e
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 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 your application will use')
->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,7 @@ 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 @@ -448,8 +450,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 @@ -459,20 +461,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 @@ -554,6 +555,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 fec8b4e

Please sign in to comment.