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

Automatically assume default value for some selected parameters under interactive mode #93

Open
wants to merge 3 commits 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
14 changes: 10 additions & 4 deletions Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,12 @@ private function processParams(array $config, array $expectedParams, array $actu
}

$envMap = empty($config['env-map']) ? array() : (array) $config['env-map'];
$assumeDefaultParams = empty($config['assume-default-for-keys']) ? array() : (array) $config['assume-default-for-keys'];

// Add the params coming from the environment values
$actualParams = array_replace($actualParams, $this->getEnvValues($envMap));

return $this->getParams($expectedParams, $actualParams);
return $this->getParams($expectedParams, $actualParams, $assumeDefaultParams);
}

private function getEnvValues(array $envMap)
Expand Down Expand Up @@ -137,7 +138,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 $assumeDefaultParams)
{
// Simply use the expectedParams value as default for the missing params.
if (!$this->io->isInteractive()) {
Expand All @@ -156,8 +157,13 @@ private function getParams(array $expectedParams, array $actualParams)
$this->io->write('<comment>Some parameters are missing. Please provide them.</comment>');
}

$default = Inline::dump($message);
$value = $this->io->ask(sprintf('<question>%s</question> (<comment>%s</comment>): ', $key, $default), $default);
if (in_array($key, $assumeDefaultParams)) {
$value = Inline::dump($message);
$this->io->write(sprintf('<comment>Assumed default value "<info>%s</info>" for parameter <question>%s</question>.</comment>', $value, $key));
} else {
$default = Inline::dump($message);
$value = $this->io->ask(sprintf('<question>%s</question> (<comment>%s</comment>): ', $key, $default), $default);
}

$actualParams[$key] = Inline::parse($value);
}
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,25 @@ If the old parameter is no longer present (maybe because it has been renamed and
removed already), no parameters are overwritten. You don't need to remove obsolete
parameters from the rename map once they have been renamed.

### Automatic assumption of parameters default value on interaction mode

If you have several parameters that hardly change their values you can identify them so that
the default value is automatically assumed even when using the interactive prompt.
This is achieved by providing a list (array) of parameters that will assume this behaviour
under the `assume-default-for-keys` param in the configuration:
```json
{
"extra": {
"incenteev-parameters": {
"assume-default-for-keys": [
"my_first_param",
"my_second_param"
]
}
}
}
```

### Managing multiple ignored files

The parameter handler can manage multiple ignored files. To use this feature,
Expand Down
15 changes: 13 additions & 2 deletions Tests/ProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function testParameterHandling($testCaseName)
'title' => 'unknown test',
'config' => array(
'file' => 'parameters.yml',
'parameter-key' => 'parameters',
),
'dist-file' => 'parameters.yml.dist',
'environment' => array(),
Expand All @@ -111,7 +112,7 @@ public function testParameterHandling($testCaseName)
$message = sprintf('<info>%s the "%s" file</info>', $exists ? 'Updating' : 'Creating', $testCase['config']['file']);
$this->io->write($message)->shouldBeCalled();

$this->setInteractionExpectations($testCase);
$this->setInteractionExpectations($testCase, $dataDir);

$this->processor->processFile($testCase['config']);

Expand Down Expand Up @@ -142,7 +143,7 @@ private function initializeTestCase(array $testCase, $dataDir, $workingDir)
return $exists;
}

private function setInteractionExpectations(array $testCase)
private function setInteractionExpectations(array $testCase, $dataDir)
{
$this->io->isInteractive()->willReturn($testCase['interactive']);

Expand All @@ -159,6 +160,16 @@ private function setInteractionExpectations(array $testCase)
->willReturn($settings['input'])
->shouldBeCalled();
}

if (!empty($testCase['config']['assume-default-for-keys'])) {
$distValues = (array) Yaml::parse(file_get_contents($dataDir.'/dist.yml'));

foreach ($testCase['config']['assume-default-for-keys'] as $param) {
$default = $distValues[$testCase['config']['parameter-key']][$param];
$this->io->write(sprintf('<comment>Assumed default value "<info>%s</info>" for parameter <question>%s</question>.</comment>', $default, $param))
->shouldBeCalled();
}
}
}

public function provideParameterHandlingTestCases()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
foo: bar
boolean: false
another: ~
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This file is auto-generated during the composer install
parameters:
foo: bar
boolean: true
another: test_input
14 changes: 14 additions & 0 deletions Tests/fixtures/testcases/interaction_assume_default_keys/setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
title: Existing values are not asked interactively again

interactive: true

config:
assume-default-for-keys: [foo]

requested_params:
boolean:
default: 'false'
input: 'true'
another:
default: 'null'
input: 'test_input'