|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Kiboko\Cloud\Platform\Console\Command\Environment\Variable; |
| 6 | + |
| 7 | +use Kiboko\Cloud\Domain\Environment\DTO\Context; |
| 8 | +use Kiboko\Cloud\Domain\Environment\DTO\DirectValueEnvironmentVariable; |
| 9 | +use Kiboko\Cloud\Domain\Environment\DTO\EnvironmentVariable; |
| 10 | +use Kiboko\Cloud\Domain\Environment\DTO\EnvironmentVariableInterface; |
| 11 | +use Kiboko\Cloud\Domain\Environment\DTO\SecretValueEnvironmentVariable; |
| 12 | +use Kiboko\Cloud\Domain\Environment\DTO\ValuedEnvironmentVariableInterface; |
| 13 | +use Kiboko\Cloud\Domain\Environment\VariableNotFoundException; |
| 14 | +use Kiboko\Cloud\Platform\Console\EnvironmentWizard; |
| 15 | +use Symfony\Component\Console\Command\Command; |
| 16 | +use Symfony\Component\Console\Input\InputInterface; |
| 17 | +use Symfony\Component\Console\Output\OutputInterface; |
| 18 | +use Symfony\Component\Console\Question\ConfirmationQuestion; |
| 19 | +use Symfony\Component\Console\Question\Question; |
| 20 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 21 | +use Symfony\Component\Finder\Finder; |
| 22 | +use Symfony\Component\Finder\SplFileInfo; |
| 23 | +use Symfony\Component\Serializer\Encoder\YamlEncoder; |
| 24 | +use Symfony\Component\Serializer\Normalizer\CustomNormalizer; |
| 25 | +use Symfony\Component\Serializer\Normalizer\PropertyNormalizer; |
| 26 | +use Symfony\Component\Serializer\Serializer; |
| 27 | + |
| 28 | +final class SetCommand extends Command |
| 29 | +{ |
| 30 | + public static $defaultName = 'environment:variable:set'; |
| 31 | + |
| 32 | + private EnvironmentWizard $wizard; |
| 33 | + |
| 34 | + public function __construct(?string $name) |
| 35 | + { |
| 36 | + $this->wizard = new EnvironmentWizard(); |
| 37 | + parent::__construct($name); |
| 38 | + } |
| 39 | + |
| 40 | + protected function configure() |
| 41 | + { |
| 42 | + $this->setDescription('Prints an environment variable'); |
| 43 | + |
| 44 | + $this->wizard->configureConsoleCommand($this); |
| 45 | + } |
| 46 | + |
| 47 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 48 | + { |
| 49 | + $workingDirectory = $input->getOption('working-directory') ?: getcwd(); |
| 50 | + |
| 51 | + $finder = (new Finder()) |
| 52 | + ->files() |
| 53 | + ->ignoreDotFiles(false) |
| 54 | + ->in($workingDirectory); |
| 55 | + |
| 56 | + $format = new SymfonyStyle($input, $output); |
| 57 | + |
| 58 | + $serializer = new Serializer( |
| 59 | + [ |
| 60 | + new CustomNormalizer(), |
| 61 | + new PropertyNormalizer(), |
| 62 | + ], |
| 63 | + [ |
| 64 | + new YamlEncoder(), |
| 65 | + ] |
| 66 | + ); |
| 67 | + |
| 68 | + if ($finder->hasResults()) { |
| 69 | + /** @var SplFileInfo $file */ |
| 70 | + foreach ($finder->name('/^\.?kloud.environment.ya?ml$/') as $file) { |
| 71 | + try { |
| 72 | + /** @var Context $context */ |
| 73 | + $context = $serializer->deserialize($file->getContents(), Context::class, 'yaml'); |
| 74 | + } catch (\Throwable $exception) { |
| 75 | + $format->error($exception->getMessage()); |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + break; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if (!isset($context)) { |
| 84 | + $format->error('No .kloud.environment.yaml file found in your directory. You must initialize it using environment:init command'); |
| 85 | + |
| 86 | + return 1; |
| 87 | + } |
| 88 | + |
| 89 | + $variableName = $format->askQuestion(new Question('Please enter a variable name')); |
| 90 | + |
| 91 | + try { |
| 92 | + /** @var EnvironmentVariableInterface $variable */ |
| 93 | + $variable = $context->getVariable($variableName); |
| 94 | + } catch (VariableNotFoundException $exception) { |
| 95 | + $format->error($exception->getMessage()); |
| 96 | + |
| 97 | + return 1; |
| 98 | + } |
| 99 | + |
| 100 | + $format->table( |
| 101 | + ['Variable', 'Value'], |
| 102 | + [ |
| 103 | + [ |
| 104 | + $variableName, |
| 105 | + $variable instanceof ValuedEnvironmentVariableInterface ? |
| 106 | + $variable->getValue() : |
| 107 | + ($variable instanceof SecretValueEnvironmentVariable ? |
| 108 | + sprintf('SECRET: %s', $variable->getSecret()) : |
| 109 | + null), |
| 110 | + ], |
| 111 | + ] |
| 112 | + ); |
| 113 | + |
| 114 | + // If value is empty, $variable becomes/stay an EnvironmentVariable without any value. |
| 115 | + if (!$value = $this->verifyValue($context, $format, $variable)) { |
| 116 | + $this->sendResponse($context, $format, $serializer, $workingDirectory); |
| 117 | + |
| 118 | + return 0; |
| 119 | + } |
| 120 | + |
| 121 | + $isSecret = $format->askQuestion(new ConfirmationQuestion('Is this a secret variable ?', false)); |
| 122 | + |
| 123 | + // Test $variable type and potentially change it according to the answer |
| 124 | + if ($variable instanceof ValuedEnvironmentVariableInterface) { |
| 125 | + if ($isSecret) { |
| 126 | + $context->setVariable(new SecretValueEnvironmentVariable($variable->getVariable(), $value)); |
| 127 | + } else { |
| 128 | + $variable->setValue($value); |
| 129 | + } |
| 130 | + } |
| 131 | + if ($variable instanceof SecretValueEnvironmentVariable) { |
| 132 | + if ($isSecret) { |
| 133 | + $variable->setSecret($value); |
| 134 | + } else { |
| 135 | + $context->setVariable(new DirectValueEnvironmentVariable($variable->getVariable(), $value)); |
| 136 | + } |
| 137 | + } |
| 138 | + if ($variable instanceof EnvironmentVariable) { |
| 139 | + if ($isSecret) { |
| 140 | + $context->setVariable(new SecretValueEnvironmentVariable($variable->getVariable(), $value)); |
| 141 | + } else { |
| 142 | + $context->setVariable(new DirectValueEnvironmentVariable($variable->getVariable(), $value)); |
| 143 | + } |
| 144 | + } |
| 145 | + $this->sendResponse($context, $format, $serializer, $workingDirectory); |
| 146 | + |
| 147 | + return 0; |
| 148 | + } |
| 149 | + |
| 150 | + private function sendResponse(Context $context, SymfonyStyle $format, Serializer $serializer, string $workingDirectory): void |
| 151 | + { |
| 152 | + $format->success('Variable was successfully changed'); |
| 153 | + file_put_contents($workingDirectory.'/.kloud.environment.yaml', $serializer->serialize($context, 'yaml', [ |
| 154 | + 'yaml_inline' => 4, |
| 155 | + 'yaml_indent' => 0, |
| 156 | + 'yaml_flags' => 0, |
| 157 | + ])); |
| 158 | + } |
| 159 | + |
| 160 | + private function verifyValue(Context $context, SymfonyStyle $format, EnvironmentVariableInterface $variable): ?string |
| 161 | + { |
| 162 | + if (!$value = $format->askQuestion(new Question('Please provide the new value'))) { |
| 163 | + $context->setVariable(new EnvironmentVariable($variable->getVariable())); |
| 164 | + |
| 165 | + return null; |
| 166 | + } |
| 167 | + |
| 168 | + return $value; |
| 169 | + } |
| 170 | +} |
0 commit comments