diff --git a/src/Domain/Environment/DTO/Context.php b/src/Domain/Environment/DTO/Context.php index 75b930b..282961a 100644 --- a/src/Domain/Environment/DTO/Context.php +++ b/src/Domain/Environment/DTO/Context.php @@ -1,7 +1,10 @@ -environmentVariables = []; } - public function addVariables(EnvironmentVariableInterface ...$variables): void + public function addVariable(EnvironmentVariableInterface ...$variable): void { - array_push($this->environmentVariables, ...$variables); + array_push($this->environmentVariables, ...$variable); } - public function getVariableValue(string $variableName) + public function getVariable(string $variableName): EnvironmentVariableInterface { foreach ($this->environmentVariables as $variable) { - if ($variable instanceof DirectValueEnvironmentVariable) { - if ($variableName === $variable->getVariable()->__toString()) { - $value = $variable->getValue()->__toString(); - if (!$value) { - $value = ' '; - } - return $value; - } - } else if ($variable instanceof SecretValueEnvironmentVariable) { - if ($variableName === $variable->getVariable()->__toString()) { - return '**secret**'; - } - } else { - if ($variableName === $variable->getVariable()->__toString()) { - return ' '; - } + if ($variableName !== (string) $variable->getVariable()) { + continue; } + + return $variable; } + + throw new VariableNotFoundException(strtr('The variable %name% does not exist.', ['%name%' => $variableName])); } public function denormalize(DenormalizerInterface $denormalizer, $data, string $format = null, array $context = []) @@ -52,13 +45,13 @@ public function denormalize(DenormalizerInterface $denormalizer, $data, string $ $this->environmentVariables = []; $parser = new ExpressionParser(); - foreach ($data['environmentVariables'] as $variable) { + foreach ($data['environment'] as $variable) { if (isset($variable['value'])) { $this->environmentVariables[] = new DirectValueEnvironmentVariable( new Variable($variable['name']), $parser->parse($variable['value']) ); - } else if (isset($variable['secret'])) { + } elseif (isset($variable['secret'])) { $this->environmentVariables[] = new SecretValueEnvironmentVariable( new Variable($variable['name']), $variable['secret'] @@ -75,7 +68,7 @@ public function normalize(NormalizerInterface $normalizer, string $format = null { return [ 'deployment' => $normalizer->normalize($this->deployment, $format, $context), - 'environment' => (function($variables) { + 'environment' => iterator_to_array((function ($variables) { /** @var EnvironmentVariableInterface $variable */ foreach ($variables as $variable) { if ($variable instanceof DirectValueEnvironmentVariable) { @@ -83,7 +76,7 @@ public function normalize(NormalizerInterface $normalizer, string $format = null 'name' => (string) $variable->getVariable(), 'value' => $variable->getValue(), ]; - } else if ($variable instanceof SecretValueEnvironmentVariable){ + } elseif ($variable instanceof SecretValueEnvironmentVariable) { yield [ 'name' => (string) $variable->getVariable(), 'secret' => $variable->getSecret(), @@ -94,7 +87,7 @@ public function normalize(NormalizerInterface $normalizer, string $format = null ]; } } - })($this->environmentVariables), + })($this->environmentVariables)), ]; } } diff --git a/src/Domain/Environment/VariableNotFoundException.php b/src/Domain/Environment/VariableNotFoundException.php new file mode 100644 index 0000000..8d2ed8c --- /dev/null +++ b/src/Domain/Environment/VariableNotFoundException.php @@ -0,0 +1,8 @@ +name('/^\.?kloud.environment.ya?ml$/') as $file) { $format->error('The directory was already initialized with an environment file. You should update it using commands listed in environment:variable'); + return 0; } } @@ -76,21 +85,30 @@ protected function execute(InputInterface $input, OutputInterface $output) ), ); - $envDistPath = getcwd() . '/.env.dist'; + $envDistPath = getcwd().'/.env.dist'; if (file_exists($envDistPath)) { $envDist = parse_ini_file($envDistPath); foreach (array_keys($envDist) as $name) { - $value = $format->askQuestion(new Question('Value of ' . $name)); - $variable = [$name, $value]; - $context->environmentVariables[$name] = $value; + $value = $format->askQuestion(new Question('Value of '.$name)); + + $isSecret = false; + if ($value) { + $isSecret = $format->askQuestion(new ConfirmationQuestion('Is this a secret variable ?', false)); + } + + if ($isSecret) { + $context->addVariable(new SecretValueEnvironmentVariable(new Variable($name), $value)); + } else { + $context->addVariable(new DirectValueEnvironmentVariable(new Variable($name), $value)); + } } } $format->note('Writing a new .kloud.environment.yaml file.'); - file_put_contents($workingDirectory . '/.kloud.environment.yaml', $serializer->serialize($context, 'yaml', [ - 'yaml_inline' => 2, + file_put_contents($workingDirectory.'/.kloud.environment.yaml', $serializer->serialize($context, 'yaml', [ + 'yaml_inline' => 4, 'yaml_indent' => 0, - 'yaml_flags' => 0 + 'yaml_flags' => 0, ])); return 0; diff --git a/src/Platform/Console/Command/Environment/Variable/GetCommand.php b/src/Platform/Console/Command/Environment/Variable/GetCommand.php index 48802cd..b7d36d1 100644 --- a/src/Platform/Console/Command/Environment/Variable/GetCommand.php +++ b/src/Platform/Console/Command/Environment/Variable/GetCommand.php @@ -1,8 +1,14 @@ -name('/^\.?kloud.environment.ya?ml$/') as $file) { try { - /** @var \Kiboko\Cloud\Domain\Stack\DTO\Context $context */ + /** @var Context $context */ $context = $serializer->deserialize($file->getContents(), Context::class, 'yaml'); } catch (\Throwable $exception) { $format->error($exception->getMessage()); @@ -73,23 +79,32 @@ protected function execute(InputInterface $input, OutputInterface $output) if (!isset($context)) { $format->error('No .kloud.environment.yaml file found in your directory. You must initialize it using environment:init command'); + return 1; } $variableName = $format->askQuestion(new Question('Please enter a variable name')); - /** @var Context $context */ - $value = $context->getVariableValue($variableName); + try { + /** @var EnvironmentVariableInterface $variable */ + $variable = $context->getVariable($variableName); + } catch (VariableNotFoundException $exception) { + $format->error($exception->getMessage()); - if (!$value) { - $format->error('This variable does not exist'); - return 0; + return 1; } $format->table( ['Variable', 'Value'], [ - [$variableName, $value], + [ + $variableName, + $variable instanceof ValuedEnvironmentVariableInterface ? + $variable->getValue() : + ($variable instanceof SecretValueEnvironmentVariable ? + sprintf('SECRET: %s', $variable->getSecret()) : + null), + ], ] ); diff --git a/src/Platform/Console/Command/Environment/Variable/ListCommand.php b/src/Platform/Console/Command/Environment/Variable/ListCommand.php index 6fa2dc1..aaae839 100644 --- a/src/Platform/Console/Command/Environment/Variable/ListCommand.php +++ b/src/Platform/Console/Command/Environment/Variable/ListCommand.php @@ -1,9 +1,12 @@ -error('No .kloud.environment.yaml file found in your directory. You must initialize it using environment:init command'); + return 1; } @@ -84,7 +88,11 @@ protected function execute(InputInterface $input, OutputInterface $output) foreach ($context->environmentVariables as $variable) { yield [ (string) $variable->getVariable(), - ($context->getVariableValue((string) $variable->getVariable())) + $variable instanceof ValuedEnvironmentVariableInterface ? + $variable->getValue() : + ($variable instanceof SecretValueEnvironmentVariable ? + sprintf('SECRET: %s', $variable->getSecret()) : + null), ]; } })($context)),