Browse Source

Added --json and --merge options to ConfigCommand (#8779)

effulgentsia 4 years ago
parent
commit
46efcefefa
2 changed files with 30 additions and 1 deletions
  1. 9 0
      doc/03-cli.md
  2. 21 1
      src/Composer/Command/ConfigCommand.php

+ 9 - 0
doc/03-cli.md

@@ -605,6 +605,8 @@ See the [Config](06-config.md) chapter for valid configuration options.
   that this cannot be used in conjunction with the `--global` option.
 * **--absolute:** Returns absolute paths when fetching *-dir config values
   instead of relative.
+* **--json:** JSON decode the setting value, to be used with `extra.*` keys.
+* **--merge:** Merge the setting value with the current value, to be used with `extra.*` keys in combination with `--json`.
 
 ### Modifying Repositories
 
@@ -633,6 +635,13 @@ php composer.phar config extra.foo.bar value
 The dots indicate array nesting, a max depth of 3 levels is allowed though. The above
 would set `"extra": { "foo": { "bar": "value" } }`.
 
+If you have a complex value to add/modify, you can use the `--json` and `--merge` flags
+to edit extra fields as json:
+
+```sh
+php composer.phar config --json extra.foo.bar '{"baz": true, "qux": []}'
+```
+
 ## create-project
 
 You can use Composer to create new projects from an existing package. This is

+ 21 - 1
src/Composer/Command/ConfigCommand.php

@@ -73,6 +73,8 @@ class ConfigCommand extends BaseCommand
                 new InputOption('list', 'l', InputOption::VALUE_NONE, 'List configuration settings'),
                 new InputOption('file', 'f', InputOption::VALUE_REQUIRED, 'If you want to choose a different composer.json or config.json'),
                 new InputOption('absolute', null, InputOption::VALUE_NONE, 'Returns absolute paths when fetching *-dir config values instead of relative'),
+                new InputOption('json', 'j', InputOption::VALUE_NONE, 'JSON decode the setting value, to be used with extra.* keys'),
+                new InputOption('merge', 'm', InputOption::VALUE_NONE, 'Merge the setting value with the current value, to be used with extra.* keys in combination with --json'),
                 new InputArgument('setting-key', null, 'Setting key'),
                 new InputArgument('setting-value', InputArgument::IS_ARRAY, 'Setting value'),
             ))
@@ -119,6 +121,10 @@ To add or edit extra properties you can use:
 
     <comment>%command.full_name% extra.property value</comment>
 
+Or to add a complex value you can use json with:
+
+    <comment>%command.full_name% extra.property --json '{"foo":true, "bar": []}'</comment>
+
 To edit the file in an external editor:
 
     <comment>%command.full_name% --editor</comment>
@@ -622,7 +628,21 @@ EOT
                 return 0;
             }
 
-            $this->configSource->addProperty($settingKey, $values[0]);
+            $value = $values[0];
+            if ($input->getOption('json')) {
+                $value = JsonFile::parseJson($value);
+                if ($input->getOption('merge')) {
+                    $currentValue = $this->configFile->read();
+                    $bits = explode('.', $settingKey);
+                    foreach ($bits as $bit) {
+                        $currentValue = isset($currentValue[$bit]) ? $currentValue[$bit] : null;
+                    }
+                    if (is_array($currentValue)) {
+                        $value = array_merge($currentValue, $value);
+                    }
+                }
+            }
+            $this->configSource->addProperty($settingKey, $value);
 
             return 0;
         }