Skip to content

Commit eaea526

Browse files
authored
MCLOUD-7072: Add CLI command for generating .magento.env.yaml (#3)
1 parent e4bb6c3 commit eaea526

39 files changed

+761
-25
lines changed

src/Application.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ protected function getDefaultCommands()
7575
$this->container->create(Command\CronKill::class),
7676
$this->container->create(Command\CronUnlock::class),
7777
$this->container->create(Command\ConfigShow::class),
78+
$this->container->create(Command\ConfigCreate::class),
79+
$this->container->create(Command\ConfigUpdate::class),
7880
$this->container->create(Command\RunCommand::class),
7981
$this->container->create(Command\GenerateSchema::class),
8082
$this->container->create(Command\ErrorShow::class)

src/Command/ApplyPatches.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function __construct(Manager $manager)
4343
protected function configure(): void
4444
{
4545
$this->setName(self::NAME)
46-
->setDescription('Applies custom patches');
46+
->setDescription('Applies custom patches.');
4747

4848
parent::configure();
4949
}

src/Command/BackupList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function __construct(
5555
protected function configure()
5656
{
5757
$this->setName(self::NAME)
58-
->setDescription('Shows the list of backup files');
58+
->setDescription('Shows the list of backup files.');
5959

6060
parent::configure();
6161
}

src/Command/BackupRestore.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ public function __construct(Restore $restore, LoggerInterface $logger)
5454
protected function configure()
5555
{
5656
$this->setName(self::NAME)
57-
->setDescription('Restore important configuration files. Run backup:list to show the list of backup files');
57+
->setDescription(
58+
'Restore important configuration files. Run backup:list to show the list of backup files.'
59+
);
5860
$this->addOption(
5961
'force',
6062
'f',

src/Command/Build.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Build extends Command
2828
protected function configure()
2929
{
3030
$this->setName(static::NAME)
31-
->setDescription('Builds application');
31+
->setDescription('Builds application.');
3232

3333
parent::configure();
3434
}

src/Command/Build/Generate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function __construct(Processor $processor)
4444
protected function configure()
4545
{
4646
$this->setName(static::NAME)
47-
->setDescription('Generates all necessary files for build stage');
47+
->setDescription('Generates all necessary files for build stage.');
4848

4949
parent::configure();
5050
}

src/Command/Build/Transfer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function __construct(Processor $processor)
4343
protected function configure()
4444
{
4545
$this->setName(static::NAME)
46-
->setDescription('Transfer generated files into init directory');
46+
->setDescription('Transfers generated files into init directory.');
4747

4848
parent::configure();
4949
}

src/Command/ConfigCreate.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MagentoCloud\Command;
9+
10+
use Magento\MagentoCloud\Filesystem\ConfigFileList;
11+
use Magento\MagentoCloud\Filesystem\Driver\File;
12+
use Symfony\Component\Console\Command\Command;
13+
use Symfony\Component\Console\Input\InputArgument;
14+
use Symfony\Component\Console\Input\InputInterface;
15+
use Symfony\Component\Console\Output\OutputInterface;
16+
use Symfony\Component\Yaml\Yaml;
17+
18+
/**
19+
* Creates .magento.env.yaml
20+
*/
21+
class ConfigCreate extends Command
22+
{
23+
const NAME = 'cloud:config:create';
24+
25+
const ARG_CONFIGURATION = 'configuration';
26+
27+
/**
28+
* @var ConfigFileList
29+
*/
30+
private $configFileList;
31+
32+
/**
33+
* @var File
34+
*/
35+
private $file;
36+
37+
/**
38+
* @param ConfigFileList $configFileList
39+
* @param File $file
40+
*/
41+
public function __construct(ConfigFileList $configFileList, File $file)
42+
{
43+
$this->configFileList = $configFileList;
44+
$this->file = $file;
45+
46+
parent::__construct();
47+
}
48+
49+
/**
50+
* @inheritdoc
51+
*/
52+
protected function configure()
53+
{
54+
$this->setName(static::NAME)
55+
->setDescription(
56+
'Creates a `.magento.env.yaml` file with the specified build, deploy, and post-deploy variable ' .
57+
'configuration. Overwrites any existing `.magento,.env.yaml` file.'
58+
)
59+
->addArgument(
60+
self::ARG_CONFIGURATION,
61+
InputArgument::REQUIRED,
62+
'Configuration in JSON format'
63+
);
64+
65+
parent::configure();
66+
}
67+
68+
/**
69+
* @inheritDoc
70+
*/
71+
public function execute(InputInterface $input, OutputInterface $output)
72+
{
73+
$configuration = $input->getArgument(self::ARG_CONFIGURATION);
74+
75+
$decodedConfig = json_decode($configuration, true);
76+
77+
if (json_last_error() !== JSON_ERROR_NONE) {
78+
throw new \Exception('Wrong JSON format: ' . json_last_error_msg());
79+
}
80+
81+
$yaml = Yaml::dump($decodedConfig, 10, 2);
82+
$filePath = $this->configFileList->getEnvConfig();
83+
84+
$this->file->filePutContents($filePath, $yaml);
85+
86+
$output->writeln(sprintf("Config file %s was created", $filePath));
87+
}
88+
}

src/Command/ConfigShow.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function __construct(
6666
protected function configure()
6767
{
6868
$this->setName(static::NAME)
69-
->setDescription('Display encoded cloud configuration environment variables')
69+
->setDescription('Display encoded cloud configuration environment variables.')
7070
->addArgument(
7171
'variable',
7272
InputArgument::IS_ARRAY,

src/Command/ConfigUpdate.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MagentoCloud\Command;
9+
10+
use Magento\MagentoCloud\Filesystem\ConfigFileList;
11+
use Magento\MagentoCloud\Config\Environment\ReaderInterface;
12+
use Magento\MagentoCloud\Filesystem\Driver\File;
13+
use Symfony\Component\Console\Command\Command;
14+
use Symfony\Component\Console\Input\InputArgument;
15+
use Symfony\Component\Console\Input\InputInterface;
16+
use Symfony\Component\Console\Output\OutputInterface;
17+
use Symfony\Component\Yaml\Yaml;
18+
19+
/**
20+
* Updates .magento.env.yaml
21+
*/
22+
class ConfigUpdate extends Command
23+
{
24+
const NAME = 'cloud:config:update';
25+
26+
const ARG_CONFIGURATION = 'configuration';
27+
28+
/**
29+
* @var ConfigFileList
30+
*/
31+
private $configFileList;
32+
33+
/**
34+
* @var File
35+
*/
36+
private $file;
37+
38+
/**
39+
* @var ReaderInterface
40+
*/
41+
private $reader;
42+
43+
/**
44+
* @param ConfigFileList $configFileList
45+
* @param File $file
46+
* @param ReaderInterface $reader
47+
*/
48+
public function __construct(ConfigFileList $configFileList, File $file, ReaderInterface $reader)
49+
{
50+
$this->configFileList = $configFileList;
51+
$this->file = $file;
52+
$this->reader = $reader;
53+
54+
parent::__construct();
55+
}
56+
57+
/**
58+
* @inheritdoc
59+
*/
60+
protected function configure()
61+
{
62+
$this->setName(static::NAME)
63+
->setDescription(
64+
'Updates the existing `.magento.env.yaml` file with the specified configuration. ' .
65+
'Creates `.magento.env.yaml` file if it does not exist.'
66+
)
67+
->addArgument(
68+
self::ARG_CONFIGURATION,
69+
InputArgument::REQUIRED,
70+
'Configuration in JSON format'
71+
);
72+
73+
parent::configure();
74+
}
75+
76+
/**
77+
* @inheritDoc
78+
*/
79+
public function execute(InputInterface $input, OutputInterface $output)
80+
{
81+
$configuration = $input->getArgument(self::ARG_CONFIGURATION);
82+
83+
$decodedConfig = json_decode($configuration, true);
84+
85+
if (json_last_error() !== JSON_ERROR_NONE) {
86+
throw new \Exception('Wrong JSON format: ' . json_last_error_msg());
87+
}
88+
89+
$config = array_replace_recursive($this->reader->read(), $decodedConfig);
90+
91+
$yaml = Yaml::dump($config, 10, 2);
92+
$filePath = $this->configFileList->getEnvConfig();
93+
94+
$this->file->filePutContents($filePath, $yaml);
95+
96+
$output->writeln(sprintf("Config file %s was updated", $filePath));
97+
}
98+
}

0 commit comments

Comments
 (0)