2013-01-22 00:27:42 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhabricatorConfigManagementSetWorkflow
|
|
|
|
extends PhabricatorConfigManagementWorkflow {
|
|
|
|
|
|
|
|
protected function didConstruct() {
|
|
|
|
$this
|
|
|
|
->setName('set')
|
|
|
|
->setExamples('**set** __key__ __value__')
|
2014-10-09 01:15:05 +02:00
|
|
|
->setSynopsis(pht('Set a local configuration value.'))
|
2013-01-22 00:27:42 +01:00
|
|
|
->setArguments(
|
|
|
|
array(
|
2014-10-09 01:15:05 +02:00
|
|
|
array(
|
|
|
|
'name' => 'database',
|
|
|
|
'help' => pht('Update configuration in the database instead of '.
|
|
|
|
'in local configuration.'),
|
|
|
|
),
|
2013-01-22 00:27:42 +01:00
|
|
|
array(
|
|
|
|
'name' => 'args',
|
|
|
|
'wildcard' => true,
|
|
|
|
),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute(PhutilArgumentParser $args) {
|
|
|
|
$console = PhutilConsole::getConsole();
|
|
|
|
$argv = $args->getArg('args');
|
|
|
|
if (count($argv) == 0) {
|
2014-10-09 01:15:05 +02:00
|
|
|
throw new PhutilArgumentUsageException(pht(
|
|
|
|
'Specify a configuration key and a value to set it to.'));
|
2013-01-22 00:27:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$key = $argv[0];
|
|
|
|
|
|
|
|
if (count($argv) == 1) {
|
2014-10-09 01:15:05 +02:00
|
|
|
throw new PhutilArgumentUsageException(pht(
|
|
|
|
"Specify a value to set the key '%s' to.",
|
|
|
|
$key));
|
2013-01-22 00:27:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$value = $argv[1];
|
|
|
|
|
|
|
|
if (count($argv) > 2) {
|
2014-10-09 01:15:05 +02:00
|
|
|
throw new PhutilArgumentUsageException(pht(
|
|
|
|
'Too many arguments: expected one key and one value.'));
|
2013-01-22 00:27:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$options = PhabricatorApplicationConfigOptions::loadAllOptions();
|
|
|
|
if (empty($options[$key])) {
|
2014-10-09 01:15:05 +02:00
|
|
|
throw new PhutilArgumentUsageException(pht(
|
|
|
|
"No such configuration key '%s'! Use `config list` to list all ".
|
|
|
|
"keys.",
|
|
|
|
$key));
|
2013-01-22 00:27:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$option = $options[$key];
|
|
|
|
|
|
|
|
$type = $option->getType();
|
|
|
|
switch ($type) {
|
|
|
|
case 'string':
|
|
|
|
case 'class':
|
2013-01-22 03:46:21 +01:00
|
|
|
case 'enum':
|
2013-01-22 00:27:42 +01:00
|
|
|
$value = (string)$value;
|
|
|
|
break;
|
|
|
|
case 'int':
|
|
|
|
if (!ctype_digit($value)) {
|
2014-10-09 01:15:05 +02:00
|
|
|
throw new PhutilArgumentUsageException(pht(
|
|
|
|
"Config key '%s' is of type '%s'. Specify an integer.",
|
|
|
|
$key,
|
|
|
|
$type));
|
2013-01-22 00:27:42 +01:00
|
|
|
}
|
|
|
|
$value = (int)$value;
|
|
|
|
break;
|
|
|
|
case 'bool':
|
|
|
|
if ($value == 'true') {
|
|
|
|
$value = true;
|
|
|
|
} else if ($value == 'false') {
|
|
|
|
$value = false;
|
|
|
|
} else {
|
2014-10-09 01:15:05 +02:00
|
|
|
throw new PhutilArgumentUsageException(pht(
|
|
|
|
"Config key '%s' is of type '%s'. ".
|
|
|
|
"Specify 'true' or 'false'.",
|
|
|
|
$key,
|
|
|
|
$type));
|
2013-01-22 00:27:42 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$value = json_decode($value, true);
|
|
|
|
if (!is_array($value)) {
|
2014-10-09 01:15:05 +02:00
|
|
|
throw new PhutilArgumentUsageException(pht(
|
|
|
|
"Config key '%s' is of type '%s'. Specify it in JSON.",
|
|
|
|
$key,
|
|
|
|
$type));
|
2013-01-22 00:27:42 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-10-09 01:15:05 +02:00
|
|
|
$use_database = $args->getArg('database');
|
|
|
|
if ($option->getLocked() && $use_database) {
|
|
|
|
throw new PhutilArgumentUsageException(pht(
|
|
|
|
"Config key '%s' is locked and can only be set in local ".
|
|
|
|
'configuration.',
|
|
|
|
$key));
|
|
|
|
}
|
2013-01-22 00:27:42 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
$option->getGroup()->validateOption($option, $value);
|
|
|
|
} catch (PhabricatorConfigValidationException $validation) {
|
|
|
|
// Convert this into a usage exception so we don't dump a stack trace.
|
|
|
|
throw new PhutilArgumentUsageException($validation->getMessage());
|
|
|
|
}
|
|
|
|
|
2014-10-09 01:15:05 +02:00
|
|
|
if ($use_database) {
|
|
|
|
$config_type = 'database';
|
2015-01-09 22:58:11 +01:00
|
|
|
$config_entry = PhabricatorConfigEntry::loadConfigEntry($key);
|
|
|
|
$config_entry->setValue($value);
|
|
|
|
$config_entry->save();
|
2014-10-09 01:15:05 +02:00
|
|
|
} else {
|
|
|
|
$config_type = 'local';
|
|
|
|
id(new PhabricatorConfigLocalSource())
|
|
|
|
->setKeys(array($key => $value));
|
|
|
|
}
|
2013-01-22 00:27:42 +01:00
|
|
|
|
|
|
|
$console->writeOut(
|
2014-10-09 01:15:05 +02:00
|
|
|
pht("Set '%s' in %s configuration.", $key, $config_type)."\n");
|
2013-01-22 00:27:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|