diff --git a/Processor.php b/Processor.php index 6dc208f..7bfbcab 100644 --- a/Processor.php +++ b/Processor.php @@ -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) @@ -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()) { @@ -156,8 +157,13 @@ private function getParams(array $expectedParams, array $actualParams) $this->io->write('Some parameters are missing. Please provide them.'); } - $default = Inline::dump($message); - $value = $this->io->ask(sprintf('%s (%s): ', $key, $default), $default); + if (in_array($key, $assumeDefaultParams)) { + $value = Inline::dump($message); + $this->io->write(sprintf('Assumed default value "%s" for parameter %s.', $value, $key)); + } else { + $default = Inline::dump($message); + $value = $this->io->ask(sprintf('%s (%s): ', $key, $default), $default); + } $actualParams[$key] = Inline::parse($value); } diff --git a/README.md b/README.md index 0f42600..a80d51d 100644 --- a/README.md +++ b/README.md @@ -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, diff --git a/Tests/ProcessorTest.php b/Tests/ProcessorTest.php index babf44a..4a42f96 100644 --- a/Tests/ProcessorTest.php +++ b/Tests/ProcessorTest.php @@ -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(), @@ -111,7 +112,7 @@ public function testParameterHandling($testCaseName) $message = sprintf('%s the "%s" file', $exists ? 'Updating' : 'Creating', $testCase['config']['file']); $this->io->write($message)->shouldBeCalled(); - $this->setInteractionExpectations($testCase); + $this->setInteractionExpectations($testCase, $dataDir); $this->processor->processFile($testCase['config']); @@ -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']); @@ -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('Assumed default value "%s" for parameter %s.', $default, $param)) + ->shouldBeCalled(); + } + } } public function provideParameterHandlingTestCases() diff --git a/Tests/fixtures/testcases/interaction_assume_default_keys/dist.yml b/Tests/fixtures/testcases/interaction_assume_default_keys/dist.yml new file mode 100644 index 0000000..4134d19 --- /dev/null +++ b/Tests/fixtures/testcases/interaction_assume_default_keys/dist.yml @@ -0,0 +1,4 @@ +parameters: + foo: bar + boolean: false + another: ~ diff --git a/Tests/fixtures/testcases/interaction_assume_default_keys/expected.yml b/Tests/fixtures/testcases/interaction_assume_default_keys/expected.yml new file mode 100644 index 0000000..b36e6c4 --- /dev/null +++ b/Tests/fixtures/testcases/interaction_assume_default_keys/expected.yml @@ -0,0 +1,5 @@ +# This file is auto-generated during the composer install +parameters: + foo: bar + boolean: true + another: test_input diff --git a/Tests/fixtures/testcases/interaction_assume_default_keys/setup.yml b/Tests/fixtures/testcases/interaction_assume_default_keys/setup.yml new file mode 100644 index 0000000..608c2e7 --- /dev/null +++ b/Tests/fixtures/testcases/interaction_assume_default_keys/setup.yml @@ -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'