SecretsRemoveCommand.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Command;
  11. use Symfony\Bundle\FrameworkBundle\Secrets\AbstractVault;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. use Symfony\Component\Console\Style\SymfonyStyle;
  19. /**
  20. * @author Jérémy Derussé <jeremy@derusse.com>
  21. * @author Nicolas Grekas <p@tchwork.com>
  22. *
  23. * @internal
  24. */
  25. final class SecretsRemoveCommand extends Command
  26. {
  27. protected static $defaultName = 'secrets:remove';
  28. private $vault;
  29. private $localVault;
  30. public function __construct(AbstractVault $vault, AbstractVault $localVault = null)
  31. {
  32. $this->vault = $vault;
  33. $this->localVault = $localVault;
  34. parent::__construct();
  35. }
  36. protected function configure()
  37. {
  38. $this
  39. ->setDescription('Remove a secret from the vault')
  40. ->addArgument('name', InputArgument::REQUIRED, 'The name of the secret')
  41. ->addOption('local', 'l', InputOption::VALUE_NONE, 'Update the local vault.')
  42. ->setHelp(<<<'EOF'
  43. The <info>%command.name%</info> command removes a secret from the vault.
  44. <info>%command.full_name% <name></info>
  45. EOF
  46. )
  47. ;
  48. }
  49. protected function execute(InputInterface $input, OutputInterface $output): int
  50. {
  51. $io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
  52. $vault = $input->getOption('local') ? $this->localVault : $this->vault;
  53. if (null === $vault) {
  54. $io->success('The local vault is disabled.');
  55. return 1;
  56. }
  57. if ($vault->remove($name = $input->getArgument('name'))) {
  58. $io->success($vault->getLastMessage() ?? 'Secret was removed from the vault.');
  59. } else {
  60. $io->comment($vault->getLastMessage() ?? 'Secret was not found in the vault.');
  61. }
  62. if ($this->vault === $vault && null !== $this->localVault->reveal($name)) {
  63. $io->comment('Note that this secret is overridden in the local vault.');
  64. }
  65. return 0;
  66. }
  67. }