diff --git a/scripts/setup/manage_config.php b/scripts/setup/manage_config.php index 7e649a7ac1..1610ebd257 100755 --- a/scripts/setup/manage_config.php +++ b/scripts/setup/manage_config.php @@ -15,7 +15,10 @@ EOSYNOPSIS $args->parseStandardArguments(); $workflows = array( + new PhabricatorConfigManagementListWorkflow(), new PhabricatorConfigManagementSetWorkflow(), + new PhabricatorConfigManagementGetWorkflow(), + new PhabricatorConfigManagementDeleteWorkflow(), new PhutilHelpArgumentWorkflow(), ); diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index e782b4f2ba..055cc010ed 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -719,8 +719,11 @@ phutil_register_library_map(array( 'PhabricatorConfigJSON' => 'applications/config/json/PhabricatorConfigJSON.php', 'PhabricatorConfigListController' => 'applications/config/controller/PhabricatorConfigListController.php', 'PhabricatorConfigLocalSource' => 'infrastructure/env/PhabricatorConfigLocalSource.php', - 'PhabricatorConfigManagementSetWorkflow' => 'infrastructure/env/management/PhabricatorConfigManagementSetWorkflow.php', - 'PhabricatorConfigManagementWorkflow' => 'infrastructure/env/management/PhabricatorConfigManagementWorkflow.php', + 'PhabricatorConfigManagementDeleteWorkflow' => 'applications/config/management/PhabricatorConfigManagementDeleteWorkflow.php', + 'PhabricatorConfigManagementGetWorkflow' => 'applications/config/management/PhabricatorConfigManagementGetWorkflow.php', + 'PhabricatorConfigManagementListWorkflow' => 'applications/config/management/PhabricatorConfigManagementListWorkflow.php', + 'PhabricatorConfigManagementSetWorkflow' => 'applications/config/management/PhabricatorConfigManagementSetWorkflow.php', + 'PhabricatorConfigManagementWorkflow' => 'applications/config/management/PhabricatorConfigManagementWorkflow.php', 'PhabricatorConfigOption' => 'applications/config/option/PhabricatorConfigOption.php', 'PhabricatorConfigProxySource' => 'infrastructure/env/PhabricatorConfigProxySource.php', 'PhabricatorConfigSource' => 'infrastructure/env/PhabricatorConfigSource.php', @@ -2117,6 +2120,9 @@ phutil_register_library_map(array( 'PhabricatorConfigIssueViewController' => 'PhabricatorConfigController', 'PhabricatorConfigListController' => 'PhabricatorConfigController', 'PhabricatorConfigLocalSource' => 'PhabricatorConfigProxySource', + 'PhabricatorConfigManagementDeleteWorkflow' => 'PhabricatorConfigManagementWorkflow', + 'PhabricatorConfigManagementGetWorkflow' => 'PhabricatorConfigManagementWorkflow', + 'PhabricatorConfigManagementListWorkflow' => 'PhabricatorConfigManagementWorkflow', 'PhabricatorConfigManagementSetWorkflow' => 'PhabricatorConfigManagementWorkflow', 'PhabricatorConfigManagementWorkflow' => 'PhutilArgumentWorkflow', 'PhabricatorConfigOption' => diff --git a/src/applications/config/management/PhabricatorConfigManagementDeleteWorkflow.php b/src/applications/config/management/PhabricatorConfigManagementDeleteWorkflow.php new file mode 100644 index 0000000000..d765b5ffe9 --- /dev/null +++ b/src/applications/config/management/PhabricatorConfigManagementDeleteWorkflow.php @@ -0,0 +1,56 @@ +setName('delete') + ->setExamples('**delete** __key__') + ->setSynopsis('Delete a local configuration value.') + ->setArguments( + array( + array( + 'name' => 'args', + 'wildcard' => true, + ), + )); + } + + public function execute(PhutilArgumentParser $args) { + $console = PhutilConsole::getConsole(); + + $argv = $args->getArg('args'); + if (count($argv) == 0) { + throw new PhutilArgumentUsageException( + "Specify a configuration key to delete."); + } + + $key = $argv[0]; + + if (count($argv) > 1) { + throw new PhutilArgumentUsageException( + "Too many arguments: expected one key."); + } + + $options = PhabricatorApplicationConfigOptions::loadAllOptions(); + if (empty($options[$key])) { + throw new PhutilArgumentUsageException( + "No such configuration key '{$key}'! Use `config list` to list all ". + "keys."); + } + + $config = new PhabricatorConfigLocalSource(); + $values = $config->getKeys(array($key)); + if (!$values) { + throw new PhutilArgumentUsageException( + "Configuration key '{$key}' is not set in local configuration!"); + } + + $config->deleteKeys(array($key)); + + $console->writeOut( + pht("Deleted '%s' from local configuration.", $key)."\n"); + } + +} diff --git a/src/applications/config/management/PhabricatorConfigManagementGetWorkflow.php b/src/applications/config/management/PhabricatorConfigManagementGetWorkflow.php new file mode 100644 index 0000000000..49758e084d --- /dev/null +++ b/src/applications/config/management/PhabricatorConfigManagementGetWorkflow.php @@ -0,0 +1,62 @@ +setName('get') + ->setExamples('**get** __key__') + ->setSynopsis('Get a local configuration value.') + ->setArguments( + array( + array( + 'name' => 'args', + 'wildcard' => true, + ), + )); + } + + public function execute(PhutilArgumentParser $args) { + $console = PhutilConsole::getConsole(); + + $argv = $args->getArg('args'); + if (count($argv) == 0) { + throw new PhutilArgumentUsageException( + "Specify a configuration key to get."); + } + + $key = $argv[0]; + + if (count($argv) > 1) { + throw new PhutilArgumentUsageException( + "Too many arguments: expected one key."); + } + + $options = PhabricatorApplicationConfigOptions::loadAllOptions(); + if (empty($options[$key])) { + throw new PhutilArgumentUsageException( + "No such configuration key '{$key}'! Use `config list` to list all ". + "keys."); + } + + $config = new PhabricatorConfigLocalSource(); + $values = $config->getKeys(array($key)); + + $result = array(); + foreach ($values as $key => $value) { + $result[] = array( + 'key' => $key, + 'source' => 'local', + 'value' => $value, + ); + } + $result = array( + 'config' => $result, + ); + + $json = new PhutilJSON(); + $console->writeOut($json->encodeFormatted($result)); + } + +} diff --git a/src/applications/config/management/PhabricatorConfigManagementListWorkflow.php b/src/applications/config/management/PhabricatorConfigManagementListWorkflow.php new file mode 100644 index 0000000000..10bd6202b2 --- /dev/null +++ b/src/applications/config/management/PhabricatorConfigManagementListWorkflow.php @@ -0,0 +1,25 @@ +setName('list') + ->setExamples('**list**') + ->setSynopsis('List all configuration keys.'); + } + + public function execute(PhutilArgumentParser $args) { + $options = PhabricatorApplicationConfigOptions::loadAllOptions(); + ksort($options); + + $console = PhutilConsole::getConsole(); + foreach ($options as $option) { + $console->writeOut($option->getKey()."\n"); + } + + return 0; + } + +} diff --git a/src/applications/config/management/PhabricatorConfigManagementSetWorkflow.php b/src/applications/config/management/PhabricatorConfigManagementSetWorkflow.php new file mode 100644 index 0000000000..df678b79a1 --- /dev/null +++ b/src/applications/config/management/PhabricatorConfigManagementSetWorkflow.php @@ -0,0 +1,99 @@ +setName('set') + ->setExamples('**set** __key__ __value__') + ->setSynopsis('Set a local configuration value.') + ->setArguments( + array( + array( + 'name' => 'args', + 'wildcard' => true, + ), + )); + } + + public function execute(PhutilArgumentParser $args) { + $console = PhutilConsole::getConsole(); + + $argv = $args->getArg('args'); + if (count($argv) == 0) { + throw new PhutilArgumentUsageException( + "Specify a configuration key and a value to set it to."); + } + + $key = $argv[0]; + + if (count($argv) == 1) { + throw new PhutilArgumentUsageException( + "Specify a value to set the key '{$key}' to."); + } + + $value = $argv[1]; + + if (count($argv) > 2) { + throw new PhutilArgumentUsageException( + "Too many arguments: expected one key and one value."); + } + + $options = PhabricatorApplicationConfigOptions::loadAllOptions(); + if (empty($options[$key])) { + throw new PhutilArgumentUsageException( + "No such configuration key '{$key}'! Use `config list` to list all ". + "keys."); + } + + $option = $options[$key]; + + $type = $option->getType(); + switch ($type) { + case 'string': + case 'class': + $value = (string)$value; + break; + case 'int': + if (!ctype_digit($value)) { + throw new PhutilArgumentUsageException( + "Config key '{$key}' is of type '{$type}'. Specify an integer."); + } + $value = (int)$value; + break; + case 'bool': + if ($value == 'true') { + $value = true; + } else if ($value == 'false') { + $value = false; + } else { + throw new PhutilArgumentUsageException( + "Config key '{$key}' is of type '{$type}'. ". + "Specify 'true' or 'false'."); + } + break; + default: + $value = json_decode($value, true); + if (!is_array($value)) { + throw new PhutilArgumentUsageException( + "Config key '{$key}' is of type '{$type}'. Specify it in JSON."); + } + break; + } + + 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()); + } + + $config = new PhabricatorConfigLocalSource(); + $config->setKeys(array($key => $value)); + + $console->writeOut( + pht("Set '%s' in local configuration.", $key)."\n"); + } + +} diff --git a/src/infrastructure/env/management/PhabricatorConfigManagementWorkflow.php b/src/applications/config/management/PhabricatorConfigManagementWorkflow.php similarity index 100% rename from src/infrastructure/env/management/PhabricatorConfigManagementWorkflow.php rename to src/applications/config/management/PhabricatorConfigManagementWorkflow.php diff --git a/src/infrastructure/env/PhabricatorConfigProxySource.php b/src/infrastructure/env/PhabricatorConfigProxySource.php index d2804e5ab8..cb0ede7113 100644 --- a/src/infrastructure/env/PhabricatorConfigProxySource.php +++ b/src/infrastructure/env/PhabricatorConfigProxySource.php @@ -38,7 +38,7 @@ abstract class PhabricatorConfigProxySource } public function deleteKeys(array $keys) { - $this->getSource()->deleteKeys(); + $this->getSource()->deleteKeys($keys); return $this; } diff --git a/src/infrastructure/env/management/PhabricatorConfigManagementSetWorkflow.php b/src/infrastructure/env/management/PhabricatorConfigManagementSetWorkflow.php deleted file mode 100644 index c94db76958..0000000000 --- a/src/infrastructure/env/management/PhabricatorConfigManagementSetWorkflow.php +++ /dev/null @@ -1,50 +0,0 @@ -setName('set') - ->setExamples('**set** __key__ __value__') - ->setSynopsis('Set a local configuration value.') - ->setArguments( - array( - array( - 'name' => 'args', - 'wildcard' => true, - ), - )); - } - - public function execute(PhutilArgumentParser $args) { - $console = PhutilConsole::getConsole(); - - $argv = $args->getArg('args'); - if (count($argv) == 0) { - throw new PhutilArgumentUsageException( - "Specify a configuration key and a value to set it to."); - } - - $key = $argv[0]; - - if (count($argv) == 1) { - throw new PhutilArgumentUsageException( - "Specify a value to set the key '{$key}' to."); - } - - $value = $argv[1]; - - if (count($argv) > 2) { - throw new PhutilArgumentUsageException( - "Too many arguments: expected one key and one value."); - } - - $config = new PhabricatorConfigLocalSource(); - $config->setKeys(array($key => $value)); - - $console->writeOut( - pht("Set '%s' to '%s' in local configuration.", $key, $value)."\n"); - } - -}