Skip to content

Commit 88659e5

Browse files
committed
feat: add environment:variables:upload command
1 parent cb6309c commit 88659e5

1 file changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Ymir command-line tool.
7+
*
8+
* (c) Carl Alexander <support@ymirapp.com>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Ymir\Cli\Command\Environment;
15+
16+
use Symfony\Component\Console\Exception\RuntimeException;
17+
use Symfony\Component\Console\Input\InputArgument;
18+
use Symfony\Component\Console\Input\InputInterface;
19+
use Symfony\Component\Filesystem\Filesystem;
20+
use Ymir\Cli\ApiClient;
21+
use Ymir\Cli\CliConfiguration;
22+
use Ymir\Cli\Command\AbstractProjectCommand;
23+
use Ymir\Cli\Command\Project\DeployProjectCommand;
24+
use Ymir\Cli\Command\Project\RedeployProjectCommand;
25+
use Ymir\Cli\Console\ConsoleOutput;
26+
use Ymir\Cli\ProjectConfiguration;
27+
28+
class UploadEnvironmentVariablesCommand extends AbstractProjectCommand
29+
{
30+
/**
31+
* The name of the command.
32+
*
33+
* @var string
34+
*/
35+
public const NAME = 'environment:variables:upload';
36+
37+
/**
38+
* The file system.
39+
*
40+
* @var Filesystem
41+
*/
42+
private $filesystem;
43+
44+
/**
45+
* The project directory where the project files are copied from.
46+
*
47+
* @var string
48+
*/
49+
private $projectDirectory;
50+
51+
/**
52+
* Constructor.
53+
*/
54+
public function __construct(ApiClient $apiClient, CliConfiguration $cliConfiguration, Filesystem $filesystem, ProjectConfiguration $projectConfiguration, string $projectDirectory)
55+
{
56+
parent::__construct($apiClient, $cliConfiguration, $projectConfiguration);
57+
58+
$this->filesystem = $filesystem;
59+
$this->projectDirectory = rtrim($projectDirectory, '/');
60+
}
61+
62+
/**
63+
* {@inheritdoc}
64+
*/
65+
protected function configure()
66+
{
67+
$this
68+
->setName(self::NAME)
69+
->setDescription('Download an environment\'s environment variables into an environment file')
70+
->addArgument('environment', InputArgument::OPTIONAL, 'The environment name', 'staging');
71+
}
72+
73+
/**
74+
* {@inheritdoc}
75+
*/
76+
protected function perform(InputInterface $input, ConsoleOutput $output)
77+
{
78+
$environment = $this->getStringArgument($input, 'environment');
79+
$filePath = sprintf($this->projectDirectory.'/.env.%s', $environment);
80+
81+
if (!$this->filesystem->exists($filePath)) {
82+
throw new RuntimeException(sprintf('No environment file found for the "%s" environment. Please download it using the "%s" command.', $environment, DownloadEnvironmentVariablesCommand::NAME));
83+
} elseif (!$output->confirm('Uploading the environment file will overwrite all environment variables. Are you sure you want to proceed?', false)) {
84+
return;
85+
}
86+
87+
$this->apiClient->changeEnvironmentVariables($this->projectConfiguration->getProjectId(), $environment, collect(explode(PHP_EOL, (string) file_get_contents($filePath)))->mapWithKeys(function (string $line) {
88+
$matches = [];
89+
preg_match('/([^=]*)=(.*)/', $line, $matches);
90+
91+
return isset($matches[1], $matches[2]) ? [$matches[1] => $matches[2]] : [];
92+
})->all(), true);
93+
94+
$output->info('Environment variables uploaded');
95+
$output->newLine();
96+
$output->writeln(sprintf('<comment>Note:</comment> You need to redeploy the project to the "<comment>%s</comment>" environment using either the "<comment>%s</comment>" or "<comment>%s</comment>" commands for the change to take effect.', $environment, DeployProjectCommand::ALIAS, RedeployProjectCommand::ALIAS));
97+
98+
if ($output->confirm('Do you want to delete the environment file?')) {
99+
$this->filesystem->remove($filePath);
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)