Skip to content

Commit

Permalink
MCLOUD-7072: Add CLI command for generating .magento.env.yaml (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
oshmyheliuk authored Oct 16, 2020
1 parent e4bb6c3 commit eaea526
Show file tree
Hide file tree
Showing 39 changed files with 761 additions and 25 deletions.
2 changes: 2 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ protected function getDefaultCommands()
$this->container->create(Command\CronKill::class),
$this->container->create(Command\CronUnlock::class),
$this->container->create(Command\ConfigShow::class),
$this->container->create(Command\ConfigCreate::class),
$this->container->create(Command\ConfigUpdate::class),
$this->container->create(Command\RunCommand::class),
$this->container->create(Command\GenerateSchema::class),
$this->container->create(Command\ErrorShow::class)
Expand Down
2 changes: 1 addition & 1 deletion src/Command/ApplyPatches.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(Manager $manager)
protected function configure(): void
{
$this->setName(self::NAME)
->setDescription('Applies custom patches');
->setDescription('Applies custom patches.');

parent::configure();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Command/BackupList.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function __construct(
protected function configure()
{
$this->setName(self::NAME)
->setDescription('Shows the list of backup files');
->setDescription('Shows the list of backup files.');

parent::configure();
}
Expand Down
4 changes: 3 additions & 1 deletion src/Command/BackupRestore.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ public function __construct(Restore $restore, LoggerInterface $logger)
protected function configure()
{
$this->setName(self::NAME)
->setDescription('Restore important configuration files. Run backup:list to show the list of backup files');
->setDescription(
'Restore important configuration files. Run backup:list to show the list of backup files.'
);
$this->addOption(
'force',
'f',
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Build extends Command
protected function configure()
{
$this->setName(static::NAME)
->setDescription('Builds application');
->setDescription('Builds application.');

parent::configure();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Build/Generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(Processor $processor)
protected function configure()
{
$this->setName(static::NAME)
->setDescription('Generates all necessary files for build stage');
->setDescription('Generates all necessary files for build stage.');

parent::configure();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Build/Transfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(Processor $processor)
protected function configure()
{
$this->setName(static::NAME)
->setDescription('Transfer generated files into init directory');
->setDescription('Transfers generated files into init directory.');

parent::configure();
}
Expand Down
88 changes: 88 additions & 0 deletions src/Command/ConfigCreate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MagentoCloud\Command;

use Magento\MagentoCloud\Filesystem\ConfigFileList;
use Magento\MagentoCloud\Filesystem\Driver\File;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Yaml;

/**
* Creates .magento.env.yaml
*/
class ConfigCreate extends Command
{
const NAME = 'cloud:config:create';

const ARG_CONFIGURATION = 'configuration';

/**
* @var ConfigFileList
*/
private $configFileList;

/**
* @var File
*/
private $file;

/**
* @param ConfigFileList $configFileList
* @param File $file
*/
public function __construct(ConfigFileList $configFileList, File $file)
{
$this->configFileList = $configFileList;
$this->file = $file;

parent::__construct();
}

/**
* @inheritdoc
*/
protected function configure()
{
$this->setName(static::NAME)
->setDescription(
'Creates a `.magento.env.yaml` file with the specified build, deploy, and post-deploy variable ' .
'configuration. Overwrites any existing `.magento,.env.yaml` file.'
)
->addArgument(
self::ARG_CONFIGURATION,
InputArgument::REQUIRED,
'Configuration in JSON format'
);

parent::configure();
}

/**
* @inheritDoc
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$configuration = $input->getArgument(self::ARG_CONFIGURATION);

$decodedConfig = json_decode($configuration, true);

if (json_last_error() !== JSON_ERROR_NONE) {
throw new \Exception('Wrong JSON format: ' . json_last_error_msg());
}

$yaml = Yaml::dump($decodedConfig, 10, 2);
$filePath = $this->configFileList->getEnvConfig();

$this->file->filePutContents($filePath, $yaml);

$output->writeln(sprintf("Config file %s was created", $filePath));
}
}
2 changes: 1 addition & 1 deletion src/Command/ConfigShow.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function __construct(
protected function configure()
{
$this->setName(static::NAME)
->setDescription('Display encoded cloud configuration environment variables')
->setDescription('Display encoded cloud configuration environment variables.')
->addArgument(
'variable',
InputArgument::IS_ARRAY,
Expand Down
98 changes: 98 additions & 0 deletions src/Command/ConfigUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MagentoCloud\Command;

use Magento\MagentoCloud\Filesystem\ConfigFileList;
use Magento\MagentoCloud\Config\Environment\ReaderInterface;
use Magento\MagentoCloud\Filesystem\Driver\File;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Yaml;

/**
* Updates .magento.env.yaml
*/
class ConfigUpdate extends Command
{
const NAME = 'cloud:config:update';

const ARG_CONFIGURATION = 'configuration';

/**
* @var ConfigFileList
*/
private $configFileList;

/**
* @var File
*/
private $file;

/**
* @var ReaderInterface
*/
private $reader;

/**
* @param ConfigFileList $configFileList
* @param File $file
* @param ReaderInterface $reader
*/
public function __construct(ConfigFileList $configFileList, File $file, ReaderInterface $reader)
{
$this->configFileList = $configFileList;
$this->file = $file;
$this->reader = $reader;

parent::__construct();
}

/**
* @inheritdoc
*/
protected function configure()
{
$this->setName(static::NAME)
->setDescription(
'Updates the existing `.magento.env.yaml` file with the specified configuration. ' .
'Creates `.magento.env.yaml` file if it does not exist.'
)
->addArgument(
self::ARG_CONFIGURATION,
InputArgument::REQUIRED,
'Configuration in JSON format'
);

parent::configure();
}

/**
* @inheritDoc
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$configuration = $input->getArgument(self::ARG_CONFIGURATION);

$decodedConfig = json_decode($configuration, true);

if (json_last_error() !== JSON_ERROR_NONE) {
throw new \Exception('Wrong JSON format: ' . json_last_error_msg());
}

$config = array_replace_recursive($this->reader->read(), $decodedConfig);

$yaml = Yaml::dump($config, 10, 2);
$filePath = $this->configFileList->getEnvConfig();

$this->file->filePutContents($filePath, $yaml);

$output->writeln(sprintf("Config file %s was updated", $filePath));
}
}
2 changes: 1 addition & 1 deletion src/Command/CronDisable.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function __construct(
protected function configure()
{
$this->setName(static::NAME)
->setDescription('Disable all Magento cron processes and kills currently running');
->setDescription('Disable all Magento cron processes and terminates all running processes.');

parent::configure();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Command/CronEnable.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __construct(Switcher $cronSwitcher, LoggerInterface $logger)
protected function configure()
{
$this->setName(static::NAME)
->setDescription('Enable Magento cron processes');
->setDescription('Enables Magento cron processes.');

parent::configure();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Command/CronKill.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(BackgroundProcess $backgroundProcess)
protected function configure()
{
$this->setName(static::NAME)
->setDescription('Kill all Magento cron processes');
->setDescription('Terminates all Magento cron processes.');

parent::configure();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Command/DbDump.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function __construct(
protected function configure()
{
$this->setName(self::NAME)
->setDescription('Creates backups of databases');
->setDescription('Creates database backups.');
$this->addArgument(
self::ARGUMENT_DATABASES,
InputArgument::IS_ARRAY,
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function __construct(Processor $processor, Manager $flagManager)
protected function configure()
{
$this->setName(static::NAME)
->setDescription('Deploys application');
->setDescription('Deploys the application.');

parent::configure();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Dev/GenerateSchemaError.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function __construct(File $file, FileList $fileList)
protected function configure(): void
{
$this->setName(static::NAME)
->setDescription('Generates dist/error-codes.md file from schema.error.yaml');
->setDescription('Generates the dist/error-codes.md file from the schema.error.yaml file.');

parent::configure();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Command/ErrorShow.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct(ErrorInfo $errorInfo, ReaderInterface $reader)
protected function configure()
{
$this->setName(self::NAME)
->setDescription('Display info about error by error id or info about all errors from the last deployment')
->setDescription('Displays info about error by error id or info about all errors from the last deployment.')
->addArgument(
self::ARGUMENT_ERROR_CODE,
InputArgument::OPTIONAL,
Expand Down
2 changes: 1 addition & 1 deletion src/Command/GenerateSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function __construct(
*/
protected function configure(): void
{
$this->setDescription('Generate the schema dist file');
$this->setDescription('Generates the schema *.dist file.');

parent::configure();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Command/ModuleRefresh.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(Module $module)
protected function configure()
{
$this->setName(self::NAME)
->setDescription('Refresh config to enable newly added modules');
->setDescription('Refreshes the configuration to enable newly added modules.');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(Processor $processor)
*/
protected function configure()
{
$this->setDescription('Execute scenario(s)')
$this->setDescription('Execute scenario(s).')
->addArgument(
self::ARG_SCENARIO,
InputArgument::REQUIRED | InputArgument::IS_ARRAY,
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Wizard/IdealState.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function __construct(OutputFormatter $outputFormatter, IdealStateValidato
protected function configure()
{
$this->setName(self::NAME)
->setDescription('Verifies ideal state of configuration');
->setDescription('Verifies ideal state of configuration.');

parent::configure();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Wizard/MasterSlave.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function __construct(OutputFormatter $outputFormatter, DeployInterface $d
protected function configure()
{
$this->setName(self::NAME)
->setDescription('Verifies master-slave configuration');
->setDescription('Verifies master-slave configuration.');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Wizard/ScdOnBuild.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct(
protected function configure()
{
$this->setName(self::NAME)
->setDescription('Verifies SCD on build configuration');
->setDescription('Verifies SCD on build configuration.');

parent::configure();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Wizard/ScdOnDemand.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function __construct(OutputFormatter $outputFormatter, GlobalSection $glo
protected function configure()
{
$this->setName(self::NAME)
->setDescription('Verifies SCD on demand configuration');
->setDescription('Verifies SCD on demand configuration.');

parent::configure();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Wizard/ScdOnDeploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __construct(OutputFormatter $outputFormatter, ScdOnDeployValidat
protected function configure()
{
$this->setName(self::NAME)
->setDescription('Verifies SCD on deploy configuration');
->setDescription('Verifies SCD on deploy configuration.');
}

/**
Expand Down
Loading

0 comments on commit eaea526

Please sign in to comment.