Path: blob/master/src/applications/config/management/PhabricatorConfigManagementSetWorkflow.php
12256 views
<?php12final class PhabricatorConfigManagementSetWorkflow3extends PhabricatorConfigManagementWorkflow {45protected function didConstruct() {6$this7->setName('set')8->setExamples(9"**set** __key__ __value__\n".10"**set** __key__ --stdin < value.json")11->setSynopsis(pht('Set a local configuration value.'))12->setArguments(13array(14array(15'name' => 'database',16'help' => pht(17'Update configuration in the database instead of '.18'in local configuration.'),19),20array(21'name' => 'stdin',22'help' => pht('Read option value from stdin.'),23),24array(25'name' => 'args',26'wildcard' => true,27),28));29}3031public function execute(PhutilArgumentParser $args) {32$argv = $args->getArg('args');33if (!$argv) {34throw new PhutilArgumentUsageException(35pht('Specify the configuration key you want to set.'));36}3738$is_stdin = $args->getArg('stdin');3940$key = $argv[0];4142if ($is_stdin) {43if (count($argv) > 1) {44throw new PhutilArgumentUsageException(45pht(46'Too many arguments: expected only a configuration key when '.47'using "--stdin".'));48}4950fprintf(STDERR, tsprintf("%s\n", pht('Reading value from stdin...')));51$value = file_get_contents('php://stdin');52} else {53if (count($argv) == 1) {54throw new PhutilArgumentUsageException(55pht(56'Specify a value to set the configuration key "%s" to, or '.57'use "--stdin" to read a value from stdin.',58$key));59}6061if (count($argv) > 2) {62throw new PhutilArgumentUsageException(63pht(64'Too many arguments: expected one key and one value.'));65}6667$value = $argv[1];68}6970$options = PhabricatorApplicationConfigOptions::loadAllOptions();71if (empty($options[$key])) {72throw new PhutilArgumentUsageException(73pht(74'Configuration key "%s" is unknown. Use "bin/config list" to list '.75'all known keys.',76$key));77}7879$option = $options[$key];8081$type = $option->newOptionType();82if ($type) {83try {84$value = $type->newValueFromCommandLineValue(85$option,86$value);87$type->validateStoredValue($option, $value);88} catch (PhabricatorConfigValidationException $ex) {89throw new PhutilArgumentUsageException($ex->getMessage());90}91} else {92// NOTE: For now, this handles both "wild" values and custom types.93$type = $option->getType();94switch ($type) {95default:96$value = json_decode($value, true);97if (!is_array($value)) {98switch ($type) {99default:100$message = pht(101'Configuration key "%s" is of type "%s". Specify it in JSON.',102$key,103$type);104break;105}106throw new PhutilArgumentUsageException($message);107}108break;109}110}111112$use_database = $args->getArg('database');113if ($option->getLocked() && $use_database) {114throw new PhutilArgumentUsageException(115pht(116'Config key "%s" is locked and can only be set in local '.117'configuration. To learn more, see "%s" in the documentation.',118$key,119pht('Configuration Guide: Locked and Hidden Configuration')));120}121122try {123$option->getGroup()->validateOption($option, $value);124} catch (PhabricatorConfigValidationException $validation) {125// Convert this into a usage exception so we don't dump a stack trace.126throw new PhutilArgumentUsageException($validation->getMessage());127}128129if ($use_database) {130$config_entry = PhabricatorConfigEntry::loadConfigEntry($key);131$config_entry->setValue($value);132133// If the entry has been deleted, resurrect it.134$config_entry->setIsDeleted(0);135136$config_entry->save();137138$write_message = pht(139'Wrote configuration key "%s" to database storage.',140$key);141} else {142$config_source = new PhabricatorConfigLocalSource();143144$local_path = $config_source->getReadablePath();145146try {147$config_source->setKeys(array($key => $value));148} catch (FilesystemException $ex) {149throw new PhutilArgumentUsageException(150pht(151'Local path "%s" is not writable. This file must be writable '.152'so that "bin/config" can store configuration.',153Filesystem::readablePath($local_path)));154}155156$write_message = pht(157'Wrote configuration key "%s" to local storage (in file "%s").',158$key,159$local_path);160}161162echo tsprintf(163"<bg:green>** %s **</bg> %s\n",164pht('DONE'),165$write_message);166167return 0;168}169170}171172173