From 10232c5f8ccf602f0ae143d58c13437ff9e30039 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Klein?= Date: Sat, 13 Feb 2016 00:34:09 +0100 Subject: [PATCH] Support whitelist of parameters in interactive mode --- Processor.php | 13 ++++++++++-- README.md | 21 +++++++++++++++++++ .../interaction_filtered_keys/dist.yml | 10 +++++++++ .../interaction_filtered_keys/expected.yml | 10 +++++++++ .../interaction_filtered_keys/setup.yml | 16 ++++++++++++++ 5 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 Tests/fixtures/testcases/interaction_filtered_keys/dist.yml create mode 100644 Tests/fixtures/testcases/interaction_filtered_keys/expected.yml create mode 100644 Tests/fixtures/testcases/interaction_filtered_keys/setup.yml diff --git a/Processor.php b/Processor.php index 6dc208f..6a78f70 100644 --- a/Processor.php +++ b/Processor.php @@ -104,7 +104,9 @@ private function processParams(array $config, array $expectedParams, array $actu // Add the params coming from the environment values $actualParams = array_replace($actualParams, $this->getEnvValues($envMap)); - return $this->getParams($expectedParams, $actualParams); + $interactiveParams = empty($config['interactive-keys']) ? array() : $config['interactive-keys']; + + return $this->getParams($expectedParams, $actualParams, $interactiveParams); } private function getEnvValues(array $envMap) @@ -137,7 +139,7 @@ private function processRenamedValues(array $renameMap, array $actualParams) return $actualParams; } - private function getParams(array $expectedParams, array $actualParams) + private function getParams(array $expectedParams, array $actualParams, array $interactiveParams) { // Simply use the expectedParams value as default for the missing params. if (!$this->io->isInteractive()) { @@ -145,12 +147,19 @@ private function getParams(array $expectedParams, array $actualParams) } $isStarted = false; + $hasWhitelist = count($interactiveParams) > 0; foreach ($expectedParams as $key => $message) { if (array_key_exists($key, $actualParams)) { continue; } + if ($hasWhitelist && !in_array($key, $interactiveParams)) { + $actualParams[$key] = $message; + + continue; + } + if (!$isStarted) { $isStarted = true; $this->io->write('Some parameters are missing. Please provide them.'); diff --git a/README.md b/README.md index 0f42600..7cfdbd0 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,27 @@ As environment variables can only be strings, they are also parsed as inline Yaml values to allows specifying ``null``, ``false``, ``true`` or numbers easily. +### Filtering interactive parameters + +By default, the interactive prompt asks for every parameter missing in the +parameters file. If you want to restrict that behaviour to a particular set +of parameters, and let the handler use the default values otherwise, you can +do so by specifying a whitelist in the `interactive-keys` property: + +```json +{ + "extra": { + "incenteev-parameters": { + "interactive-keys": [ + "param_1", + "param_3", + "param_8" + ] + } + } +} +``` + ### Renaming parameters If you are renaming a parameter, the new key will be set according to the usual diff --git a/Tests/fixtures/testcases/interaction_filtered_keys/dist.yml b/Tests/fixtures/testcases/interaction_filtered_keys/dist.yml new file mode 100644 index 0000000..f971cdb --- /dev/null +++ b/Tests/fixtures/testcases/interaction_filtered_keys/dist.yml @@ -0,0 +1,10 @@ +parameters: + boolean: false + another: test + nested: + foo: bar + bar: + - foo + - test + - null + diff --git a/Tests/fixtures/testcases/interaction_filtered_keys/expected.yml b/Tests/fixtures/testcases/interaction_filtered_keys/expected.yml new file mode 100644 index 0000000..5a46d35 --- /dev/null +++ b/Tests/fixtures/testcases/interaction_filtered_keys/expected.yml @@ -0,0 +1,10 @@ +# This file is auto-generated during the composer install +parameters: + boolean: true + another: test + nested: + foo: bar + bar: + - foo + - test + - null diff --git a/Tests/fixtures/testcases/interaction_filtered_keys/setup.yml b/Tests/fixtures/testcases/interaction_filtered_keys/setup.yml new file mode 100644 index 0000000..7919f9c --- /dev/null +++ b/Tests/fixtures/testcases/interaction_filtered_keys/setup.yml @@ -0,0 +1,16 @@ +title: Non-interactive keys are not asked interactively + +interactive: true + +config: + interactive-keys: + - boolean + - another + +requested_params: + boolean: + default: 'false' + input: 'true' + another: + default: test + input: test