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',
|
2015-05-22 09:27:56 +02:00
|
|
|
'help' => pht(
|
|
|
|
'Update configuration in the database instead of '.
|
|
|
|
'in local configuration.'),
|
2014-10-09 01:15:05 +02:00
|
|
|
),
|
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) {
|
2015-05-22 09:27:56 +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) {
|
2015-05-22 09:27:56 +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) {
|
2015-05-22 09:27:56 +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])) {
|
2015-05-22 09:27:56 +02:00
|
|
|
throw new PhutilArgumentUsageException(
|
|
|
|
pht(
|
|
|
|
"No such configuration key '%s'! Use `%s` to list all keys.",
|
|
|
|
$key,
|
|
|
|
'config list'));
|
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)) {
|
2015-05-22 09:27:56 +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 {
|
2015-05-22 09:27:56 +02:00
|
|
|
throw new PhutilArgumentUsageException(
|
|
|
|
pht(
|
|
|
|
"Config key '%s' is of type '%s'. Specify '%s' or '%s'.",
|
|
|
|
$key,
|
|
|
|
$type,
|
|
|
|
'true',
|
|
|
|
'false'));
|
2013-01-22 00:27:42 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$value = json_decode($value, true);
|
|
|
|
if (!is_array($value)) {
|
2015-03-02 16:51:19 +01:00
|
|
|
switch ($type) {
|
|
|
|
case 'set':
|
2015-09-08 17:49:33 +02:00
|
|
|
$command = csprintf(
|
|
|
|
'./bin/config set %R %s',
|
|
|
|
$key,
|
|
|
|
'{"value1": true, "value2": true}');
|
|
|
|
|
2015-05-22 09:27:56 +02:00
|
|
|
$message = sprintf(
|
2015-09-08 17:49:33 +02:00
|
|
|
"%s\n\n %s\n",
|
|
|
|
pht(
|
|
|
|
'Config key "%s" is of type "%s". Specify it in JSON. '.
|
|
|
|
'For example:',
|
|
|
|
$key,
|
|
|
|
$type),
|
|
|
|
$command);
|
2015-03-02 16:51:19 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (preg_match('/^list</', $type)) {
|
2015-09-08 17:49:33 +02:00
|
|
|
$command = csprintf(
|
|
|
|
'./bin/config set %R %s',
|
|
|
|
$key,
|
|
|
|
'["a", "b", "c"]');
|
|
|
|
|
2015-05-22 09:27:56 +02:00
|
|
|
$message = sprintf(
|
2015-09-08 17:49:33 +02:00
|
|
|
"%s\n\n %s\n",
|
2015-05-22 09:27:56 +02:00
|
|
|
pht(
|
2015-09-08 17:49:33 +02:00
|
|
|
'Config key "%s" is of type "%s". Specify it in JSON. '.
|
|
|
|
'For example:',
|
2015-05-22 09:27:56 +02:00
|
|
|
$key,
|
|
|
|
$type),
|
2015-09-08 17:49:33 +02:00
|
|
|
$command);
|
2015-03-02 16:51:19 +01:00
|
|
|
} else {
|
|
|
|
$message = pht(
|
|
|
|
'Config key "%s" is of type "%s". Specify it in JSON.',
|
|
|
|
$key,
|
|
|
|
$type);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
throw new PhutilArgumentUsageException($message);
|
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) {
|
2015-05-22 09:27:56 +02:00
|
|
|
throw new PhutilArgumentUsageException(
|
|
|
|
pht(
|
|
|
|
"Config key '%s' is locked and can only be set in local ".
|
|
|
|
"configuration.",
|
|
|
|
$key));
|
2014-10-09 01:15:05 +02:00
|
|
|
}
|
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(
|
2015-05-22 09:27:56 +02:00
|
|
|
"%s\n",
|
|
|
|
pht("Set '%s' in %s configuration.", $key, $config_type));
|
2013-01-22 00:27:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|