|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Symfony\Bundle\FrameworkBundle\Command; |
| 4 | + |
| 5 | +use Symfony\Bundle\FrameworkBundle\Secret\SecretStorageInterface; |
| 6 | +use Symfony\Component\Console\Command\Command; |
| 7 | +use Symfony\Component\Console\Helper\QuestionHelper; |
| 8 | +use Symfony\Component\Console\Input\InputArgument; |
| 9 | +use Symfony\Component\Console\Input\InputInterface; |
| 10 | +use Symfony\Component\Console\Output\OutputInterface; |
| 11 | +use Symfony\Component\Console\Question\Question; |
| 12 | + |
| 13 | +final class SecretsAddCommand extends Command |
| 14 | +{ |
| 15 | + protected static $defaultName = 'secrets:add'; |
| 16 | + |
| 17 | + /** |
| 18 | + * @var SecretStorageInterface |
| 19 | + */ |
| 20 | + private $secretStorage; |
| 21 | + |
| 22 | + public function __construct(SecretStorageInterface $secretStorage) |
| 23 | + { |
| 24 | + $this->secretStorage = $secretStorage; |
| 25 | + |
| 26 | + parent::__construct(); |
| 27 | + } |
| 28 | + |
| 29 | + protected function configure() |
| 30 | + { |
| 31 | + $this |
| 32 | + ->setDescription('Adds a secret with the key.') |
| 33 | + ->addArgument( |
| 34 | + 'key', |
| 35 | + InputArgument::REQUIRED |
| 36 | + ) |
| 37 | + ->addArgument( |
| 38 | + 'secret', |
| 39 | + InputArgument::REQUIRED |
| 40 | + ) |
| 41 | + ; |
| 42 | + } |
| 43 | + |
| 44 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 45 | + { |
| 46 | + $key = $input->getArgument('key'); |
| 47 | + $secret = $input->getArgument('secret'); |
| 48 | + |
| 49 | + $this->secretStorage->putSecret($key, $secret); |
| 50 | + } |
| 51 | + |
| 52 | + protected function interact(InputInterface $input, OutputInterface $output) |
| 53 | + { |
| 54 | + /** @var QuestionHelper $helper */ |
| 55 | + $helper = $this->getHelper('question'); |
| 56 | + |
| 57 | + $question = new Question('Key of the secret: ', $input->getArgument('key')); |
| 58 | + |
| 59 | + $key = $helper->ask($input, $output, $question); |
| 60 | + $input->setArgument('key', $key); |
| 61 | + |
| 62 | + $question = new Question('Plaintext secret value: ', $input->getArgument('secret')); |
| 63 | + $question->setHidden(true); |
| 64 | + |
| 65 | + $secret = $helper->ask($input, $output, $question); |
| 66 | + $input->setArgument('secret', $secret); |
| 67 | + } |
| 68 | +} |
0 commit comments