Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command-line options for database credentials #135

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
48 changes: 47 additions & 1 deletion src/Console/Command/DumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}

/**
Expand Down Expand Up @@ -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.
*/
Expand Down
Loading