Skip to content

Commit

Permalink
#12 : Add Set & Unset commands + changed PHP requirements + small cs …
Browse files Browse the repository at this point in the history
…fixer
  • Loading branch information
nicolas43000 committed Oct 2, 2020
1 parent 5c58d79 commit 2e5bdf5
Show file tree
Hide file tree
Showing 15 changed files with 353 additions and 15 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This project aims at building your Docker stack for [OroCommerce](https://oroinc

> ⚠️ Nota: Those stacks are not suited for production hosting, but to provide an environment based on Docker as identical as possible to OroCloud on a personal computer.
* [Requirements](#requirements)
* [Installation](#installation)
* [Usage](#usage)
* [Frequently Asked Questions](#frequently-asked-questions)
Expand All @@ -15,6 +16,10 @@ This project aims at building your Docker stack for [OroCommerce](https://oroinc
* [Marello](#marello)
* [Middleware](#middleware)

- Requirements:
---
- PHP 7.4

Installation
---

Expand Down
12 changes: 10 additions & 2 deletions bin/kloud
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env php
<?php

if (version_compare(PHP_VERSION, '5.4.0') < 0) {
throw new RuntimeException('This command requires a minimum version 5.4 for PHP. Please upgrade your PHP version or use the pre-packaged Docker image variant. See https://github.com/kiboko-labs/docker-images/#if-you-want-to-use-the-pre-packaged-docker-image');
if (version_compare(PHP_VERSION, '7.4.0') < 0) {
throw new RuntimeException('This command requires a minimum version 7.4 for PHP. Please upgrade your PHP version or use the pre-packaged Docker image variant. See https://github.com/kiboko-labs/docker-images/#if-you-want-to-use-the-pre-packaged-docker-image');
}

require __DIR__ . '/../vendor/autoload.php';
Expand Down Expand Up @@ -71,6 +71,14 @@ $app->addCommands([
(new Command\Environment\Variable\GetCommand(
Command\Environment\Variable\GetCommand::$defaultName,
)),

(new Command\Environment\Variable\SetCommand(
Command\Environment\Variable\SetCommand::$defaultName,
)),

(new Command\Environment\Variable\UnsetCommand(
Command\Environment\Variable\UnsetCommand::$defaultName,
)),
]);

$app->run(new ArgvInput($argv), new ConsoleOutput());
16 changes: 15 additions & 1 deletion src/Domain/Environment/DTO/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ public function getVariable(string $variableName): EnvironmentVariableInterface
throw new VariableNotFoundException(strtr('The variable %name% does not exist.', ['%name%' => $variableName]));
}

public function setVariable(EnvironmentVariableInterface $newVariable)
{
$i = 0;
foreach ($this->environmentVariables as $variable) {
if ((string) $newVariable->getVariable() !== (string) $variable->getVariable()) {
++$i;
continue;
}
$this->environmentVariables[$i] = $newVariable;

return;
}
}

public function denormalize(DenormalizerInterface $denormalizer, $data, string $format = null, array $context = [])
{
$this->deployment = $denormalizer->denormalize($data['deployment'], Deployment::class, $format, $context);
Expand All @@ -49,7 +63,7 @@ public function denormalize(DenormalizerInterface $denormalizer, $data, string $
if (isset($variable['value'])) {
$this->environmentVariables[] = new DirectValueEnvironmentVariable(
new Variable($variable['name']),
$parser->parse($variable['value'])
$variable['value']
);
} elseif (isset($variable['secret'])) {
$this->environmentVariables[] = new SecretValueEnvironmentVariable(
Expand Down
4 changes: 3 additions & 1 deletion src/Domain/Environment/DTO/Deployment.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Kiboko\Cloud\Domain\Environment\DTO;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Kiboko\Cloud\Domain\Environment\DTO;

Expand All @@ -23,4 +25,9 @@ public function getValue()
{
return $this->value;
}

public function setValue(string $value)
{
$this->value = $value;
}
}
4 changes: 3 additions & 1 deletion src/Domain/Environment/DTO/EnvironmentVariable.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Kiboko\Cloud\Domain\Environment\DTO;

Expand Down
4 changes: 3 additions & 1 deletion src/Domain/Environment/DTO/EnvironmentVariableInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Kiboko\Cloud\Domain\Environment\DTO;

Expand Down
4 changes: 3 additions & 1 deletion src/Domain/Environment/DTO/Expression.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Kiboko\Cloud\Domain\Environment\DTO;

Expand Down
6 changes: 4 additions & 2 deletions src/Domain/Environment/DTO/ExpressionParser.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Kiboko\Cloud\Domain\Environment\DTO;

Expand All @@ -20,7 +22,7 @@ public function parse(string $value): Expression

if (isset($matches[3])) {
$elements[] = new Variable($matches[3]);
} else if (isset($matches[2])) {
} elseif (isset($matches[2])) {
$elements[] = new Variable($matches[2]);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Kiboko\Cloud\Domain\Environment\DTO;

Expand All @@ -22,4 +24,9 @@ public function getSecret(): string
{
return $this->secret;
}

public function setSecret(string $secret)
{
$this->secret = $secret;
}
}
4 changes: 3 additions & 1 deletion src/Domain/Environment/DTO/Server.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Kiboko\Cloud\Domain\Environment\DTO;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Kiboko\Cloud\Domain\Environment\DTO;

interface ValuedEnvironmentVariableInterface extends EnvironmentVariableInterface
{
/** @return int|string|Expression|Variable */
/**
* @return int|string|Expression|Variable
*/
public function getValue();

public function setValue(string $value);
}
4 changes: 3 additions & 1 deletion src/Domain/Environment/DTO/Variable.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Kiboko\Cloud\Domain\Environment\DTO;

Expand Down
170 changes: 170 additions & 0 deletions src/Platform/Console/Command/Environment/Variable/SetCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?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\DirectValueEnvironmentVariable;
use Kiboko\Cloud\Domain\Environment\DTO\EnvironmentVariable;
use Kiboko\Cloud\Domain\Environment\DTO\EnvironmentVariableInterface;
use Kiboko\Cloud\Domain\Environment\DTO\SecretValueEnvironmentVariable;
use Kiboko\Cloud\Domain\Environment\DTO\ValuedEnvironmentVariableInterface;
use Kiboko\Cloud\Domain\Environment\VariableNotFoundException;
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\Finder\SplFileInfo;
use Symfony\Component\Serializer\Encoder\YamlEncoder;
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
use Symfony\Component\Serializer\Serializer;

final class SetCommand extends Command
{
public static $defaultName = 'environment:variable:set';

private EnvironmentWizard $wizard;

public function __construct(?string $name)
{
$this->wizard = new EnvironmentWizard();
parent::__construct($name);
}

protected function configure()
{
$this->setDescription('Prints an environment variable');

$this->wizard->configureConsoleCommand($this);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$workingDirectory = $input->getOption('working-directory') ?: getcwd();

$finder = (new Finder())
->files()
->ignoreDotFiles(false)
->in($workingDirectory);

$format = new SymfonyStyle($input, $output);

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

if ($finder->hasResults()) {
/** @var SplFileInfo $file */
foreach ($finder->name('/^\.?kloud.environment.ya?ml$/') as $file) {
try {
/** @var Context $context */
$context = $serializer->deserialize($file->getContents(), Context::class, 'yaml');
} catch (\Throwable $exception) {
$format->error($exception->getMessage());
continue;
}

break;
}
}

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'));

try {
/** @var EnvironmentVariableInterface $variable */
$variable = $context->getVariable($variableName);
} catch (VariableNotFoundException $exception) {
$format->error($exception->getMessage());

return 1;
}

$format->table(
['Variable', 'Value'],
[
[
$variableName,
$variable instanceof ValuedEnvironmentVariableInterface ?
$variable->getValue() :
($variable instanceof SecretValueEnvironmentVariable ?
sprintf('SECRET: %s', $variable->getSecret()) :
null),
],
]
);

// If value is empty, $variable becomes/stay an EnvironmentVariable without any value.
if (!$value = $this->verifyValue($context, $format, $variable)) {
$this->sendResponse($context, $format, $serializer, $workingDirectory);

return 0;
}

$isSecret = $format->askQuestion(new ConfirmationQuestion('Is this a secret variable ?', false));

// Test $variable type and potentially change it according to the answer
if ($variable instanceof ValuedEnvironmentVariableInterface) {
if ($isSecret) {
$context->setVariable(new SecretValueEnvironmentVariable($variable->getVariable(), $value));
} else {
$variable->setValue($value);
}
}
if ($variable instanceof SecretValueEnvironmentVariable) {
if ($isSecret) {
$variable->setSecret($value);
} else {
$context->setVariable(new DirectValueEnvironmentVariable($variable->getVariable(), $value));
}
}
if ($variable instanceof EnvironmentVariable) {
if ($isSecret) {
$context->setVariable(new SecretValueEnvironmentVariable($variable->getVariable(), $value));
} else {
$context->setVariable(new DirectValueEnvironmentVariable($variable->getVariable(), $value));
}
}
$this->sendResponse($context, $format, $serializer, $workingDirectory);

return 0;
}

private function sendResponse(Context $context, SymfonyStyle $format, Serializer $serializer, string $workingDirectory): void
{
$format->success('Variable was successfully changed');
file_put_contents($workingDirectory.'/.kloud.environment.yaml', $serializer->serialize($context, 'yaml', [
'yaml_inline' => 4,
'yaml_indent' => 0,
'yaml_flags' => 0,
]));
}

private function verifyValue(Context $context, SymfonyStyle $format, EnvironmentVariableInterface $variable): ?string
{
if (!$value = $format->askQuestion(new Question('Please provide the new value'))) {
$context->setVariable(new EnvironmentVariable($variable->getVariable()));

return null;
}

return $value;
}
}
Loading

0 comments on commit 2e5bdf5

Please sign in to comment.