3 final class PhabricatorConfigManagementSetWorkflow
4 extends PhabricatorConfigManagementWorkflow
{
6 protected function didConstruct() {
10 "**set** __key__ __value__\n".
11 "**set** __key__ --stdin < value.json")
12 ->setSynopsis(pht('Set a local configuration value.'))
18 'Update configuration in the database instead of '.
19 'in local configuration.'),
23 'help' => pht('Read option value from stdin.'),
32 public function execute(PhutilArgumentParser
$args) {
33 $argv = $args->getArg('args');
35 throw new PhutilArgumentUsageException(
36 pht('Specify the configuration key you want to set.'));
39 $is_stdin = $args->getArg('stdin');
44 if (count($argv) > 1) {
45 throw new PhutilArgumentUsageException(
47 'Too many arguments: expected only a configuration key when '.
51 fprintf(STDERR
, tsprintf("%s\n", pht('Reading value from stdin...')));
52 $value = file_get_contents('php://stdin');
54 if (count($argv) == 1) {
55 throw new PhutilArgumentUsageException(
57 'Specify a value to set the configuration key "%s" to, or '.
58 'use "--stdin" to read a value from stdin.',
62 if (count($argv) > 2) {
63 throw new PhutilArgumentUsageException(
65 'Too many arguments: expected one key and one value.'));
71 $options = PhabricatorApplicationConfigOptions
::loadAllOptions();
72 if (empty($options[$key])) {
73 throw new PhutilArgumentUsageException(
75 'Configuration key "%s" is unknown. Use "bin/config list" to list '.
80 $option = $options[$key];
82 $type = $option->newOptionType();
85 $value = $type->newValueFromCommandLineValue(
88 $type->validateStoredValue($option, $value);
89 } catch (PhabricatorConfigValidationException
$ex) {
90 throw new PhutilArgumentUsageException($ex->getMessage());
93 // NOTE: For now, this handles both "wild" values and custom types.
94 $type = $option->getType();
97 $value = json_decode($value, true);
98 if (!is_array($value)) {
102 'Configuration key "%s" is of type "%s". Specify it in JSON.',
107 throw new PhutilArgumentUsageException($message);
113 $use_database = $args->getArg('database');
114 if ($option->getLocked() && $use_database) {
115 throw new PhutilArgumentUsageException(
117 'Config key "%s" is locked and can only be set in local '.
118 'configuration. To learn more, see "%s" in the documentation.',
120 pht('Configuration Guide: Locked and Hidden Configuration')));
124 $option->getGroup()->validateOption($option, $value);
125 } catch (PhabricatorConfigValidationException
$validation) {
126 // Convert this into a usage exception so we don't dump a stack trace.
127 throw new PhutilArgumentUsageException($validation->getMessage());
131 $config_entry = PhabricatorConfigEntry
::loadConfigEntry($key);
132 $config_entry->setValue($value);
134 // If the entry has been deleted, resurrect it.
135 $config_entry->setIsDeleted(0);
137 $config_entry->save();
139 $write_message = pht(
140 'Wrote configuration key "%s" to database storage.',
143 $config_source = new PhabricatorConfigLocalSource();
145 $local_path = $config_source->getReadablePath();
148 $config_source->setKeys(array($key => $value));
149 } catch (FilesystemException
$ex) {
150 throw new PhutilArgumentUsageException(
152 'Local path "%s" is not writable. This file must be writable '.
153 'so that "bin/config" can store configuration.',
154 Filesystem
::readablePath($local_path)));
157 $write_message = pht(
158 'Wrote configuration key "%s" to local storage (in file "%s").',
164 "<bg:green>** %s **</bg> %s\n",