Skip to content

Commit

Permalink
Add command-line options for database credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
guvra committed Jun 4, 2024
1 parent 4b4ba07 commit aac708b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
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

0 comments on commit aac708b

Please sign in to comment.