Skip to content

Commit 2e5bdf5

Browse files
committed
#12 : Add Set & Unset commands + changed PHP requirements + small cs fixer
1 parent 5c58d79 commit 2e5bdf5

15 files changed

+353
-15
lines changed

Diff for: README.md

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This project aims at building your Docker stack for [OroCommerce](https://oroinc
55

66
> ⚠️ 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.
77
8+
* [Requirements](#requirements)
89
* [Installation](#installation)
910
* [Usage](#usage)
1011
* [Frequently Asked Questions](#frequently-asked-questions)
@@ -15,6 +16,10 @@ This project aims at building your Docker stack for [OroCommerce](https://oroinc
1516
* [Marello](#marello)
1617
* [Middleware](#middleware)
1718

19+
- Requirements:
20+
---
21+
- PHP 7.4
22+
1823
Installation
1924
---
2025

Diff for: bin/kloud

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env php
22
<?php
33

4-
if (version_compare(PHP_VERSION, '5.4.0') < 0) {
5-
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');
4+
if (version_compare(PHP_VERSION, '7.4.0') < 0) {
5+
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');
66
}
77

88
require __DIR__ . '/../vendor/autoload.php';
@@ -71,6 +71,14 @@ $app->addCommands([
7171
(new Command\Environment\Variable\GetCommand(
7272
Command\Environment\Variable\GetCommand::$defaultName,
7373
)),
74+
75+
(new Command\Environment\Variable\SetCommand(
76+
Command\Environment\Variable\SetCommand::$defaultName,
77+
)),
78+
79+
(new Command\Environment\Variable\UnsetCommand(
80+
Command\Environment\Variable\UnsetCommand::$defaultName,
81+
)),
7482
]);
7583

7684
$app->run(new ArgvInput($argv), new ConsoleOutput());

Diff for: src/Domain/Environment/DTO/Context.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ public function getVariable(string $variableName): EnvironmentVariableInterface
3939
throw new VariableNotFoundException(strtr('The variable %name% does not exist.', ['%name%' => $variableName]));
4040
}
4141

42+
public function setVariable(EnvironmentVariableInterface $newVariable)
43+
{
44+
$i = 0;
45+
foreach ($this->environmentVariables as $variable) {
46+
if ((string) $newVariable->getVariable() !== (string) $variable->getVariable()) {
47+
++$i;
48+
continue;
49+
}
50+
$this->environmentVariables[$i] = $newVariable;
51+
52+
return;
53+
}
54+
}
55+
4256
public function denormalize(DenormalizerInterface $denormalizer, $data, string $format = null, array $context = [])
4357
{
4458
$this->deployment = $denormalizer->denormalize($data['deployment'], Deployment::class, $format, $context);
@@ -49,7 +63,7 @@ public function denormalize(DenormalizerInterface $denormalizer, $data, string $
4963
if (isset($variable['value'])) {
5064
$this->environmentVariables[] = new DirectValueEnvironmentVariable(
5165
new Variable($variable['name']),
52-
$parser->parse($variable['value'])
66+
$variable['value']
5367
);
5468
} elseif (isset($variable['secret'])) {
5569
$this->environmentVariables[] = new SecretValueEnvironmentVariable(

Diff for: src/Domain/Environment/DTO/Deployment.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace Kiboko\Cloud\Domain\Environment\DTO;
46

Diff for: src/Domain/Environment/DTO/DirectValueEnvironmentVariable.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace Kiboko\Cloud\Domain\Environment\DTO;
46

@@ -23,4 +25,9 @@ public function getValue()
2325
{
2426
return $this->value;
2527
}
28+
29+
public function setValue(string $value)
30+
{
31+
$this->value = $value;
32+
}
2633
}

Diff for: src/Domain/Environment/DTO/EnvironmentVariable.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace Kiboko\Cloud\Domain\Environment\DTO;
46

Diff for: src/Domain/Environment/DTO/EnvironmentVariableInterface.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace Kiboko\Cloud\Domain\Environment\DTO;
46

Diff for: src/Domain/Environment/DTO/Expression.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace Kiboko\Cloud\Domain\Environment\DTO;
46

Diff for: src/Domain/Environment/DTO/ExpressionParser.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace Kiboko\Cloud\Domain\Environment\DTO;
46

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

2123
if (isset($matches[3])) {
2224
$elements[] = new Variable($matches[3]);
23-
} else if (isset($matches[2])) {
25+
} elseif (isset($matches[2])) {
2426
$elements[] = new Variable($matches[2]);
2527
}
2628

Diff for: src/Domain/Environment/DTO/SecretValueEnvironmentVariable.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace Kiboko\Cloud\Domain\Environment\DTO;
46

@@ -22,4 +24,9 @@ public function getSecret(): string
2224
{
2325
return $this->secret;
2426
}
27+
28+
public function setSecret(string $secret)
29+
{
30+
$this->secret = $secret;
31+
}
2532
}

Diff for: src/Domain/Environment/DTO/Server.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace Kiboko\Cloud\Domain\Environment\DTO;
46

Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace Kiboko\Cloud\Domain\Environment\DTO;
46

57
interface ValuedEnvironmentVariableInterface extends EnvironmentVariableInterface
68
{
7-
/** @return int|string|Expression|Variable */
9+
/**
10+
* @return int|string|Expression|Variable
11+
*/
812
public function getValue();
13+
14+
public function setValue(string $value);
915
}

Diff for: src/Domain/Environment/DTO/Variable.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace Kiboko\Cloud\Domain\Environment\DTO;
46

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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

Comments
 (0)