1
0
Fork 0

Add --unset flag

pull/1032/merge
Jordi Boggiano 2012-10-07 16:07:48 +02:00
parent d00d7eef7f
commit 1b7906e82d
1 changed files with 44 additions and 29 deletions

View File

@ -24,6 +24,7 @@ use Composer\Json\JsonValidationException;
/** /**
* @author Joshua Estes <Joshua.Estes@iostudio.com> * @author Joshua Estes <Joshua.Estes@iostudio.com>
* @author Jordi Boggiano <j.boggiano@seld.be>
*/ */
class ConfigCommand extends Command class ConfigCommand extends Command
{ {
@ -43,6 +44,7 @@ class ConfigCommand extends Command
->setDefinition(array( ->setDefinition(array(
new InputOption('global', 'g', InputOption::VALUE_NONE, 'Apply command to the global config file'), new InputOption('global', 'g', InputOption::VALUE_NONE, 'Apply command to the global config file'),
new InputOption('editor', 'e', InputOption::VALUE_NONE, 'Open editor'), new InputOption('editor', 'e', InputOption::VALUE_NONE, 'Open editor'),
new InputOption('unset', null, InputOption::VALUE_NONE, 'Unset the given setting-key'),
new InputOption('list', 'l', InputOption::VALUE_NONE, 'List configuration settings'), new InputOption('list', 'l', InputOption::VALUE_NONE, 'List configuration settings'),
new InputOption('file', 'f', InputOption::VALUE_REQUIRED, 'If you want to choose a different composer.json or config.json', 'composer.json'), new InputOption('file', 'f', InputOption::VALUE_REQUIRED, 'If you want to choose a different composer.json or config.json', 'composer.json'),
new InputArgument('setting-key', null, 'Setting key'), new InputArgument('setting-key', null, 'Setting key'),
@ -134,8 +136,11 @@ EOT
// If the user enters in a config variable, parse it and save to file // If the user enters in a config variable, parse it and save to file
if ($input->getArgument('setting-key')) { if ($input->getArgument('setting-key')) {
if (null === $input->getArgument('setting-value')) { if (array() !== $input->getArgument('setting-value') && $input->getOption('unset')) {
throw new \RuntimeException('You must include a setting value.'); throw new \RuntimeException('You can not combine a setting value with --unset');
}
if (array() === $input->getArgument('setting-value') && !$input->getOption('unset')) {
throw new \RuntimeException('You must include a setting value or pass --unset to clear the value');
} }
/** /**
@ -143,58 +148,68 @@ EOT
* For example "config -g repository.foo 'vcs http://example.com' * For example "config -g repository.foo 'vcs http://example.com'
*/ */
$configSettings = $this->configFile->read(); // what is current in the config $configSettings = $this->configFile->read(); // what is current in the config
$settings = array(); // This will what will be merged into the above
$values = $input->getArgument('setting-value'); // what the user is trying to add/change $values = $input->getArgument('setting-value'); // what the user is trying to add/change
// Checking for each known config value is going to make this method very large // Checking for each known config value is going to make this method very large
// what is a better way to do this? // what is a better way to do this?
// repositories.foo // repositories.foo
if (preg_match('/^repositories\.(.+)/', $input->getArgument('setting-key'), $matches)) { if (preg_match('/^repos?(?:itories)?\.(.+)/', $input->getArgument('setting-key'), $matches)) {
if (2 !== count($values)) { if ($input->getOption('unset')) {
throw new \RuntimeException('You must pass the type and a url. Example: php composer.phar config repositories.foo vcs http://bar.com'); unset($configSettings['repositories'][$matches[1]]);
} } else {
$setting = $this->parseSetting($input->getArgument('setting-key'), array( $settingKey = 'repositories.'.$matches[1];
'type' => $values[0], if (2 !== count($values)) {
'url' => $values[1], throw new \RuntimeException('You must pass the type and a url. Example: php composer.phar config repositories.foo vcs http://bar.com');
)); }
$setting = $this->parseSetting($settingKey, array(
'type' => $values[0],
'url' => $values[1],
));
// Could there be a better way to do this? // Could there be a better way to do this?
$settings = array_merge_recursive($configSettings, $setting); $configSettings = array_merge_recursive($configSettings, $setting);
$this->validateSchema($settings); $this->validateSchema($configSettings);
}
} }
// process-timeout // process-timeout
elseif (preg_match('/^process-timeout/', $input->getArgument('setting-key'))) { elseif (preg_match('/^process-timeout/', $input->getArgument('setting-key'))) {
if (1 !== count($values)) { if ($input->getOption('unset')) {
throw new \RuntimeException('You can only pass one value. Example: php composer.phar config process-timeout 300'); unset($configSettings['config']['process-timeout']);
} } else {
if (1 !== count($values)) {
throw new \RuntimeException('You can only pass one value. Example: php composer.phar config process-timeout 300');
}
if (!is_numeric($values[0])) { if (!is_numeric($values[0])) {
throw new \RuntimeException(sprintf('"%s" is not a number.', $values[0])); throw new \RuntimeException(sprintf('"%s" is not a number.', $values[0]));
} }
$setting = $this->parseSetting('config.'.$input->getArgument('setting-key'), (integer) $values[0]); $setting = $this->parseSetting('config.'.$input->getArgument('setting-key'), (integer) $values[0]);
$settings = array_merge($configSettings, $setting); $configSettings = array_merge($configSettings, $setting);
$this->validateSchema($settings); $this->validateSchema($configSettings);
}
} }
// Make sure we have something to write to disk // clean up empty sections
if (!count($settings)) { if (empty($configSettings['repositories'])) {
$output->writeln('Trying to update a setting that is supported with this command.'); unset($configSettings['repositories']);
return 0; }
if (empty($configSettings['config'])) {
unset($configSettings['config']);
} }
// Make confirmation // Make confirmation
if ($input->isInteractive()) { if ($input->isInteractive()) {
$dialog = $this->getHelperSet()->get('dialog'); $dialog = $this->getHelperSet()->get('dialog');
$output->writeln(JsonFile::encode($settings)); $output->writeln(JsonFile::encode($configSettings));
if (!$dialog->askConfirmation($output, $dialog->getQuestion('Do you confirm?', 'yes', '?'), true)) { if (!$dialog->askConfirmation($output, $dialog->getQuestion('Do you confirm?', 'yes', '?'), true)) {
$output->writeln('<error>Command Aborted by User</error>'); $output->writeln('<error>Command Aborted by User</error>');
return 1; return 1;
} }
} }
$this->configFile->write($settings); $this->configFile->write($configSettings);
} }
} }