Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / config / management / PhabricatorConfigManagementSetWorkflow.php
blobd69e903bcc955626a35956d3a3bc7bf18ca5c299
1 <?php
3 final class PhabricatorConfigManagementSetWorkflow
4 extends PhabricatorConfigManagementWorkflow {
6 protected function didConstruct() {
7 $this
8 ->setName('set')
9 ->setExamples(
10 "**set** __key__ __value__\n".
11 "**set** __key__ --stdin < value.json")
12 ->setSynopsis(pht('Set a local configuration value.'))
13 ->setArguments(
14 array(
15 array(
16 'name' => 'database',
17 'help' => pht(
18 'Update configuration in the database instead of '.
19 'in local configuration.'),
21 array(
22 'name' => 'stdin',
23 'help' => pht('Read option value from stdin.'),
25 array(
26 'name' => 'args',
27 'wildcard' => true,
29 ));
32 public function execute(PhutilArgumentParser $args) {
33 $argv = $args->getArg('args');
34 if (!$argv) {
35 throw new PhutilArgumentUsageException(
36 pht('Specify the configuration key you want to set.'));
39 $is_stdin = $args->getArg('stdin');
41 $key = $argv[0];
43 if ($is_stdin) {
44 if (count($argv) > 1) {
45 throw new PhutilArgumentUsageException(
46 pht(
47 'Too many arguments: expected only a configuration key when '.
48 'using "--stdin".'));
51 fprintf(STDERR, tsprintf("%s\n", pht('Reading value from stdin...')));
52 $value = file_get_contents('php://stdin');
53 } else {
54 if (count($argv) == 1) {
55 throw new PhutilArgumentUsageException(
56 pht(
57 'Specify a value to set the configuration key "%s" to, or '.
58 'use "--stdin" to read a value from stdin.',
59 $key));
62 if (count($argv) > 2) {
63 throw new PhutilArgumentUsageException(
64 pht(
65 'Too many arguments: expected one key and one value.'));
68 $value = $argv[1];
71 $options = PhabricatorApplicationConfigOptions::loadAllOptions();
72 if (empty($options[$key])) {
73 throw new PhutilArgumentUsageException(
74 pht(
75 'Configuration key "%s" is unknown. Use "bin/config list" to list '.
76 'all known keys.',
77 $key));
80 $option = $options[$key];
82 $type = $option->newOptionType();
83 if ($type) {
84 try {
85 $value = $type->newValueFromCommandLineValue(
86 $option,
87 $value);
88 $type->validateStoredValue($option, $value);
89 } catch (PhabricatorConfigValidationException $ex) {
90 throw new PhutilArgumentUsageException($ex->getMessage());
92 } else {
93 // NOTE: For now, this handles both "wild" values and custom types.
94 $type = $option->getType();
95 switch ($type) {
96 default:
97 $value = json_decode($value, true);
98 if (!is_array($value)) {
99 switch ($type) {
100 default:
101 $message = pht(
102 'Configuration key "%s" is of type "%s". Specify it in JSON.',
103 $key,
104 $type);
105 break;
107 throw new PhutilArgumentUsageException($message);
109 break;
113 $use_database = $args->getArg('database');
114 if ($option->getLocked() && $use_database) {
115 throw new PhutilArgumentUsageException(
116 pht(
117 'Config key "%s" is locked and can only be set in local '.
118 'configuration. To learn more, see "%s" in the documentation.',
119 $key,
120 pht('Configuration Guide: Locked and Hidden Configuration')));
123 try {
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());
130 if ($use_database) {
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.',
141 $key);
142 } else {
143 $config_source = new PhabricatorConfigLocalSource();
145 $local_path = $config_source->getReadablePath();
147 try {
148 $config_source->setKeys(array($key => $value));
149 } catch (FilesystemException $ex) {
150 throw new PhutilArgumentUsageException(
151 pht(
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").',
159 $key,
160 $local_path);
163 echo tsprintf(
164 "<bg:green>** %s **</bg> %s\n",
165 pht('DONE'),
166 $write_message);
168 return 0;