Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support whitelist of parameters in interactive mode #95

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -137,20 +139,27 @@ 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()) {
return array_replace($expectedParams, $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('<comment>Some parameters are missing. Please provide them.</comment>');
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions Tests/fixtures/testcases/interaction_filtered_keys/dist.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
parameters:
boolean: false
another: test
nested:
foo: bar
bar:
- foo
- test
- null

10 changes: 10 additions & 0 deletions Tests/fixtures/testcases/interaction_filtered_keys/expected.yml
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions Tests/fixtures/testcases/interaction_filtered_keys/setup.yml
Original file line number Diff line number Diff line change
@@ -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