From 1fb08020f564a0113a3385485624fdee09ca77ca Mon Sep 17 00:00:00 2001 From: CasEbb <617080+CasEbb@users.noreply.github.com> Date: Tue, 12 Mar 2024 08:54:13 +0100 Subject: [PATCH] Add `--database` option --- src/NewCommand.php | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/NewCommand.php b/src/NewCommand.php index 68d464a..1fb5a62 100644 --- a/src/NewCommand.php +++ b/src/NewCommand.php @@ -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') @@ -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'); @@ -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', @@ -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) @@ -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. *