Skip to content

Commit

Permalink
#12 : refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas43000 committed Sep 30, 2020
1 parent de6bd0a commit 5c58d79
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 46 deletions.
43 changes: 18 additions & 25 deletions src/Domain/Environment/DTO/Context.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Kiboko\Cloud\Domain\Environment\DTO;

use Kiboko\Cloud\Domain\Environment\VariableNotFoundException;
use Symfony\Component\Serializer\Normalizer\DenormalizableInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
Expand All @@ -18,32 +21,22 @@ public function __construct(?Deployment $deployment = null)
$this->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 = [])
Expand All @@ -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']
Expand All @@ -75,15 +68,15 @@ 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) {
yield [
'name' => (string) $variable->getVariable(),
'value' => $variable->getValue(),
];
} else if ($variable instanceof SecretValueEnvironmentVariable){
} elseif ($variable instanceof SecretValueEnvironmentVariable) {
yield [
'name' => (string) $variable->getVariable(),
'secret' => $variable->getSecret(),
Expand All @@ -94,7 +87,7 @@ public function normalize(NormalizerInterface $normalizer, string $format = null
];
}
}
})($this->environmentVariables),
})($this->environmentVariables)),
];
}
}
8 changes: 8 additions & 0 deletions src/Domain/Environment/VariableNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Kiboko\Cloud\Domain\Environment;

class VariableNotFoundException extends \RuntimeException
{

}
36 changes: 27 additions & 9 deletions src/Platform/Console/Command/Environment/InitCommand.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Kiboko\Cloud\Platform\Console\Command\Environment;

use Kiboko\Cloud\Domain\Environment\DTO\Context;
use Kiboko\Cloud\Domain\Environment\DTO\Deployment;
use Kiboko\Cloud\Domain\Environment\DTO\DirectValueEnvironmentVariable;
use Kiboko\Cloud\Domain\Environment\DTO\SecretValueEnvironmentVariable;
use Kiboko\Cloud\Domain\Environment\DTO\Server;
use Kiboko\Cloud\Domain\Environment\DTO\Variable;
use Kiboko\Cloud\Platform\Console\EnvironmentWizard;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Serializer\Encoder\YamlEncoder;
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
use Symfony\Component\Serializer\Serializer;

Expand Down Expand Up @@ -48,17 +55,19 @@ protected function execute(InputInterface $input, OutputInterface $output)

$serializer = new Serializer(
[
new CustomNormalizer(),
new PropertyNormalizer(),
],
[
new YamlEncoder()
new YamlEncoder(),
]
);

if ($finder->hasResults()) {
/** @var \SplFileInfo $file */
foreach ($finder->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;
}
}
Expand All @@ -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;
Expand Down
33 changes: 24 additions & 9 deletions src/Platform/Console/Command/Environment/Variable/GetCommand.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Kiboko\Cloud\Platform\Console\Command\Environment\Variable;

use Kiboko\Cloud\Domain\Environment\DTO\Context;
use Kiboko\Cloud\Domain\Environment\DTO\SecretValueEnvironmentVariable;
use Kiboko\Cloud\Domain\Environment\DTO\ValuedEnvironmentVariableInterface;
use Kiboko\Cloud\Domain\Environment\VariableNotFoundException;
use Kiboko\Cloud\Domain\Stack\Compose\EnvironmentVariableInterface;
use Kiboko\Cloud\Platform\Console\EnvironmentWizard;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -52,15 +58,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
new PropertyNormalizer(),
],
[
new YamlEncoder()
new YamlEncoder(),
]
);

if ($finder->hasResults()) {
/** @var SplFileInfo $file */
foreach ($finder->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());
Expand All @@ -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),
],
]
);

Expand Down
14 changes: 11 additions & 3 deletions src/Platform/Console/Command/Environment/Variable/ListCommand.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Kiboko\Cloud\Platform\Console\Command\Environment\Variable;

use Kiboko\Cloud\Domain\Environment\DTO\Context;
use Kiboko\Cloud\Domain\Environment\DTO\EnvironmentVariableInterface;
use Kiboko\Cloud\Domain\Environment\DTO\SecretValueEnvironmentVariable;
use Kiboko\Cloud\Domain\Environment\DTO\ValuedEnvironmentVariableInterface;
use Kiboko\Cloud\Platform\Console\EnvironmentWizard;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -53,7 +56,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
new PropertyNormalizer(),
],
[
new YamlEncoder()
new YamlEncoder(),
]
);

Expand All @@ -74,6 +77,7 @@ 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;
}

Expand All @@ -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)),
Expand Down

0 comments on commit 5c58d79

Please sign in to comment.