From 6f74cd8bb548052124fb3c11f1bf4b01f0990d89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20TAMARELLE?= Date: Wed, 20 Oct 2021 22:56:56 +0200 Subject: [PATCH] Add completion to commands secrets:set and fix secrets:remove --- .../Command/SecretsRemoveCommand.php | 10 ++++- .../Command/SecretsSetCommand.php | 9 +++++ .../Command/SecretsRemoveCommandTest.php | 37 +++++++++++++++++++ .../Tests/Command/SecretsSetCommandTest.php | 31 ++++++++++++++++ 4 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/Command/SecretsRemoveCommandTest.php create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/Command/SecretsSetCommandTest.php diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/SecretsRemoveCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/SecretsRemoveCommand.php index 624fc1b44c38e..0451ef300f634 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/SecretsRemoveCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/SecretsRemoveCommand.php @@ -89,8 +89,14 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti return; } - $vault = $input->getOption('local') ? $this->localVault : $this->vault; $vaultKeys = array_keys($this->vault->list(false)); - $suggestions->suggestValues(array_intersect($vaultKeys, array_keys($vault->list(false)))); + if ($input->getOption('local')) { + if (null === $this->localVault) { + return; + } + $vaultKeys = array_intersect($vaultKeys, array_keys($this->localVault->list(false))); + } + + $suggestions->suggestValues($vaultKeys); } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/SecretsSetCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/SecretsSetCommand.php index 20b898f073cbc..412247da70636 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/SecretsSetCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/SecretsSetCommand.php @@ -13,6 +13,8 @@ use Symfony\Bundle\FrameworkBundle\Secrets\AbstractVault; use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Completion\CompletionInput; +use Symfony\Component\Console\Completion\CompletionSuggestions; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -137,4 +139,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 0; } + + public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void + { + if ($input->mustSuggestArgumentValuesFor('name')) { + $suggestions->suggestValues(array_keys($this->vault->list(false))); + } + } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/SecretsRemoveCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/SecretsRemoveCommandTest.php new file mode 100644 index 0000000000000..213e639f06698 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/SecretsRemoveCommandTest.php @@ -0,0 +1,37 @@ +createMock(AbstractVault::class); + $vault->method('list')->willReturn(['SECRET' => null, 'OTHER_SECRET' => null]); + if ($withLocalVault) { + $localVault = $this->createMock(AbstractVault::class); + $localVault->method('list')->willReturn(['SECRET' => null]); + } else { + $localVault = null; + } + $command = new SecretsRemoveCommand($vault, $localVault); + $tester = new CommandCompletionTester($command); + $suggestions = $tester->complete($input); + $this->assertSame($expectedSuggestions, $suggestions); + } + + public function provideCompletionSuggestions() + { + yield 'name' => [true, [''], ['SECRET', 'OTHER_SECRET']]; + yield '--local name (with local vault)' => [true, ['--local', ''], ['SECRET']]; + yield '--local name (without local vault)' => [false, ['--local', ''], []]; + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/SecretsSetCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/SecretsSetCommandTest.php new file mode 100644 index 0000000000000..4f0d2225d148a --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/SecretsSetCommandTest.php @@ -0,0 +1,31 @@ +createMock(AbstractVault::class); + $vault->method('list')->willReturn(['SECRET' => null, 'OTHER_SECRET' => null]); + $localVault = $this->createMock(AbstractVault::class); + $command = new SecretsSetCommand($vault, $localVault); + $tester = new CommandCompletionTester($command); + $suggestions = $tester->complete($input); + $this->assertSame($expectedSuggestions, $suggestions); + } + + public function provideCompletionSuggestions() + { + yield 'name' => [[''], ['SECRET', 'OTHER_SECRET']]; + yield '--local name (with local vault)' => [['--local', ''], ['SECRET', 'OTHER_SECRET']]; + } +}