From 46efcefefa744bc0712bda6011930778d9d4ed8b Mon Sep 17 00:00:00 2001 From: effulgentsia Date: Thu, 23 Apr 2020 05:27:14 -0700 Subject: [PATCH] Added --json and --merge options to ConfigCommand (#8779) --- doc/03-cli.md | 9 +++++++++ src/Composer/Command/ConfigCommand.php | 22 +++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/doc/03-cli.md b/doc/03-cli.md index 3a964c390..217d2ea0a 100644 --- a/doc/03-cli.md +++ b/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 diff --git a/src/Composer/Command/ConfigCommand.php b/src/Composer/Command/ConfigCommand.php index 8c186e407..099e5c872 100644 --- a/src/Composer/Command/ConfigCommand.php +++ b/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: %command.full_name% extra.property value +Or to add a complex value you can use json with: + + %command.full_name% extra.property --json '{"foo":true, "bar": []}' + To edit the file in an external editor: %command.full_name% --editor @@ -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; }