Differential D20711 Diff 49391 src/applications/config/management/PhabricatorConfigManagementSetWorkflow.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/config/management/PhabricatorConfigManagementSetWorkflow.php
| Show All 24 Lines | $this | ||||
| array( | array( | ||||
| 'name' => 'args', | 'name' => 'args', | ||||
| 'wildcard' => true, | 'wildcard' => true, | ||||
| ), | ), | ||||
| )); | )); | ||||
| } | } | ||||
| public function execute(PhutilArgumentParser $args) { | public function execute(PhutilArgumentParser $args) { | ||||
| $console = PhutilConsole::getConsole(); | |||||
| $argv = $args->getArg('args'); | $argv = $args->getArg('args'); | ||||
| if (count($argv) == 0) { | if (!$argv) { | ||||
| throw new PhutilArgumentUsageException( | throw new PhutilArgumentUsageException( | ||||
| pht('Specify a configuration key and a value to set it to.')); | pht('Specify the configuration key you want to set.')); | ||||
| } | } | ||||
| $is_stdin = $args->getArg('stdin'); | $is_stdin = $args->getArg('stdin'); | ||||
| $key = $argv[0]; | $key = $argv[0]; | ||||
| if ($is_stdin) { | if ($is_stdin) { | ||||
| if (count($argv) > 1) { | if (count($argv) > 1) { | ||||
| throw new PhutilArgumentUsageException( | throw new PhutilArgumentUsageException( | ||||
| pht( | pht( | ||||
| 'Too many arguments: expected only a key when using "--stdin".')); | 'Too many arguments: expected only a configuration key when '. | ||||
| 'using "--stdin".')); | |||||
| } | } | ||||
| fprintf(STDERR, tsprintf("%s\n", pht('Reading value from stdin...'))); | fprintf(STDERR, tsprintf("%s\n", pht('Reading value from stdin...'))); | ||||
| $value = file_get_contents('php://stdin'); | $value = file_get_contents('php://stdin'); | ||||
| } else { | } else { | ||||
| if (count($argv) == 1) { | if (count($argv) == 1) { | ||||
| throw new PhutilArgumentUsageException( | throw new PhutilArgumentUsageException( | ||||
| pht( | pht( | ||||
| "Specify a value to set the key '%s' to.", | 'Specify a value to set the configuration key "%s" to, or '. | ||||
| 'use "--stdin" to read a value from stdin.', | |||||
| $key)); | $key)); | ||||
| } | } | ||||
| if (count($argv) > 2) { | if (count($argv) > 2) { | ||||
| throw new PhutilArgumentUsageException( | throw new PhutilArgumentUsageException( | ||||
| pht( | pht( | ||||
| 'Too many arguments: expected one key and one value.')); | 'Too many arguments: expected one key and one value.')); | ||||
| } | } | ||||
| $value = $argv[1]; | $value = $argv[1]; | ||||
| } | } | ||||
| $options = PhabricatorApplicationConfigOptions::loadAllOptions(); | $options = PhabricatorApplicationConfigOptions::loadAllOptions(); | ||||
| if (empty($options[$key])) { | if (empty($options[$key])) { | ||||
| throw new PhutilArgumentUsageException( | throw new PhutilArgumentUsageException( | ||||
| pht( | pht( | ||||
| "No such configuration key '%s'! Use `%s` to list all keys.", | 'Configuration key "%s" is unknown. Use "bin/config list" to list '. | ||||
| $key, | 'all known keys.', | ||||
| 'config list')); | $key)); | ||||
| } | } | ||||
| $option = $options[$key]; | $option = $options[$key]; | ||||
| $type = $option->newOptionType(); | $type = $option->newOptionType(); | ||||
| if ($type) { | if ($type) { | ||||
| try { | try { | ||||
| $value = $type->newValueFromCommandLineValue( | $value = $type->newValueFromCommandLineValue( | ||||
| $option, | $option, | ||||
| $value); | $value); | ||||
| $type->validateStoredValue($option, $value); | $type->validateStoredValue($option, $value); | ||||
| } catch (PhabricatorConfigValidationException $ex) { | } catch (PhabricatorConfigValidationException $ex) { | ||||
| throw new PhutilArgumentUsageException($ex->getMessage()); | throw new PhutilArgumentUsageException($ex->getMessage()); | ||||
| } | } | ||||
| } else { | } else { | ||||
| // NOTE: For now, this handles both "wild" values and custom types. | // NOTE: For now, this handles both "wild" values and custom types. | ||||
| $type = $option->getType(); | $type = $option->getType(); | ||||
| switch ($type) { | switch ($type) { | ||||
| default: | default: | ||||
| $value = json_decode($value, true); | $value = json_decode($value, true); | ||||
| if (!is_array($value)) { | if (!is_array($value)) { | ||||
| switch ($type) { | switch ($type) { | ||||
| default: | default: | ||||
| $message = pht( | $message = pht( | ||||
| 'Config key "%s" is of type "%s". Specify it in JSON.', | 'Configuration key "%s" is of type "%s". Specify it in JSON.', | ||||
| $key, | $key, | ||||
| $type); | $type); | ||||
| break; | break; | ||||
| } | } | ||||
| throw new PhutilArgumentUsageException($message); | throw new PhutilArgumentUsageException($message); | ||||
| } | } | ||||
| break; | break; | ||||
| } | } | ||||
| Show All 12 Lines | public function execute(PhutilArgumentParser $args) { | ||||
| try { | try { | ||||
| $option->getGroup()->validateOption($option, $value); | $option->getGroup()->validateOption($option, $value); | ||||
| } catch (PhabricatorConfigValidationException $validation) { | } catch (PhabricatorConfigValidationException $validation) { | ||||
| // Convert this into a usage exception so we don't dump a stack trace. | // Convert this into a usage exception so we don't dump a stack trace. | ||||
| throw new PhutilArgumentUsageException($validation->getMessage()); | throw new PhutilArgumentUsageException($validation->getMessage()); | ||||
| } | } | ||||
| if ($use_database) { | if ($use_database) { | ||||
| $config_type = 'database'; | |||||
| $config_entry = PhabricatorConfigEntry::loadConfigEntry($key); | $config_entry = PhabricatorConfigEntry::loadConfigEntry($key); | ||||
| $config_entry->setValue($value); | $config_entry->setValue($value); | ||||
| // If the entry has been deleted, resurrect it. | // If the entry has been deleted, resurrect it. | ||||
| $config_entry->setIsDeleted(0); | $config_entry->setIsDeleted(0); | ||||
| $config_entry->save(); | $config_entry->save(); | ||||
| $write_message = pht( | |||||
| 'Wrote configuration key "%s" to database storage.', | |||||
| $key); | |||||
| } else { | } else { | ||||
| $config_type = 'local'; | $config_source = id(new PhabricatorConfigLocalSource()) | ||||
| id(new PhabricatorConfigLocalSource()) | |||||
| ->setKeys(array($key => $value)); | ->setKeys(array($key => $value)); | ||||
| $local_path = $config_source->getReadablePath(); | |||||
| $write_message = pht( | |||||
| 'Wrote configuration key "%s" to local storage (in file "%s").', | |||||
| $key, | |||||
| $local_path); | |||||
| } | } | ||||
| $console->writeOut( | echo tsprintf( | ||||
| "%s\n", | "<bg:green>** %s **</bg> %s\n", | ||||
| pht("Set '%s' in %s configuration.", $key, $config_type)); | pht('DONE'), | ||||
| $write_message); | |||||
| return 0; | |||||
| } | } | ||||
| } | } | ||||