From 17231426b5d8609106d302b2c9f8fd13bac2c125 Mon Sep 17 00:00:00 2001 From: guvra Date: Tue, 4 Jun 2024 16:54:39 +0200 Subject: [PATCH] Add command-line options for database credentials --- CHANGELOG.md | 6 +++- src/Console/Command/DumpCommand.php | 48 ++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bac6ae14..acb36590 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,9 +4,13 @@ All notable changes to this project will be documented in this file. ## 5.0.0 (WIP) +New features: + +- Added command-line options for database credentials (`--host`, `--port`, `--user`, `--password`, `--database`) + Breaking changes: -- Added table column validation. GdprDump now throws an exception if a config file contains an undefined column ([#125](https://github.com/Smile-SA/gdpr-dump/pull/125)) +- GdprDump now throws an exception if a config file contains an undefined column ([#125](https://github.com/Smile-SA/gdpr-dump/pull/125)) - Removed support of the `filters` parameter. Use the `where` parameter instead ([#128](https://github.com/Smile-SA/gdpr-dump/pull/128)) - Removed undefined column customer_address.vat_id from shopware6 template ([#132](https://github.com/Smile-SA/gdpr-dump/pull/132)) - Stricter config file validation: string parameters don't accept integer values anymore ([#129](https://github.com/Smile-SA/gdpr-dump/pull/129)) diff --git a/src/Console/Command/DumpCommand.php b/src/Console/Command/DumpCommand.php index e050e088..f7928707 100644 --- a/src/Console/Command/DumpCommand.php +++ b/src/Console/Command/DumpCommand.php @@ -18,6 +18,7 @@ use Symfony\Component\Console\Helper\QuestionHelper; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\ConsoleOutputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\Question; @@ -45,7 +46,12 @@ public function configure(): void 'config_file', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'Dump configuration file(s)' - ); + ) + ->addOption('host', null, InputOption::VALUE_REQUIRED, 'Database host') + ->addOption('port', null, InputOption::VALUE_REQUIRED, 'Database port') + ->addOption('user', null, InputOption::VALUE_REQUIRED, 'Database user') + ->addOption('password', null, InputOption::VALUE_REQUIRED, 'Database password') + ->addOption('database', null, InputOption::VALUE_REQUIRED, 'Database name'); } /** @@ -102,12 +108,52 @@ private function loadConfig(InputInterface $input): ConfigInterface $this->configLoader->load($configFile, $config); } + // Add database config from input options + $this->addInputOptionsToConfig($config, $input); + // Compile the config $this->compiler->compile($config); return $config; } + /** + * Add input option values to the config. + * + * @throws ConfigException + */ + private function addInputOptionsToConfig(ConfigInterface $config, InputInterface $input): void + { + $databaseConfig = $config->get('database', []); + + foreach (['host', 'port', 'user', 'password', 'database'] as $option) { + $value = $input->getOption($option); + if ($value === null) { + // Option was not provided + continue; + } + + if ($value === '' && $option !== 'password') { + // Option must have a value (except the "password" option) + throw new ConfigException(sprintf('Please provide a value for the option "%s".', $option)); + } + + $configKey = $option === 'database' ? 'name' : $option; + if ($value === '') { + // Remove the password from the config if an empty value was provided + unset($databaseConfig[$configKey]); + continue; + } + + // Override the config value with the provided option value + $databaseConfig[$configKey] = $value; + } + + if (!empty($databaseConfig)) { + $config->set('database', $databaseConfig); + } + } + /** * Display a password prompt, and return the user input. */