Skip to content

Commit 3993b1a

Browse files
committed
Factorize code
1 parent 6070a50 commit 3993b1a

File tree

3 files changed

+62
-43
lines changed

3 files changed

+62
-43
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bref\Cli\Commands;
4+
5+
use Bref\Cli\Config;
6+
use Symfony\Component\Console\Command\Command;
7+
use Symfony\Component\Console\Input\InputInterface;
8+
use Symfony\Component\Console\Input\InputOption;
9+
10+
class ApplicationCommand extends Command
11+
{
12+
protected function configure(): void
13+
{
14+
$this
15+
->addOption('env', 'e', InputOption::VALUE_REQUIRED, 'The environment', 'dev')
16+
->addOption('config', 'c', InputOption::VALUE_REQUIRED, 'The location of the configuration file to use')
17+
->addOption('team', null, InputOption::VALUE_REQUIRED, 'Override the team');
18+
}
19+
20+
/**
21+
* @return array{
22+
* appName: string,
23+
* environmentName: string,
24+
* team: string,
25+
* config: array{name: string, team: string, type: string},
26+
* }
27+
*/
28+
protected function parseStandardOptions(InputInterface $input): array
29+
{
30+
/** @var string $environment */
31+
$environment = $input->getOption('env');
32+
/** @var string|null $configFileName */
33+
$configFileName = $input->getOption('config');
34+
$config = Config::loadConfig($configFileName, $environment, $input->getOption('team'));
35+
36+
return [
37+
'appName' => $config['name'],
38+
'config' => $config,
39+
'environmentName' => $environment,
40+
'team' => $config['team'],
41+
];
42+
}
43+
}

src/Commands/Deploy.php

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
use Bref\Cli\Cli\IO;
1010
use Bref\Cli\Cli\Styles;
1111
use Bref\Cli\Components\ServerlessFramework;
12-
use Bref\Cli\Config;
1312
use Exception;
14-
use Symfony\Component\Console\Command\Command;
1513
use Symfony\Component\Console\Input\InputInterface;
1614
use Symfony\Component\Console\Input\InputOption;
1715
use Symfony\Component\Console\Output\OutputInterface;
@@ -22,7 +20,7 @@
2220
use function Amp\ByteStream\buffer;
2321
use function Amp\delay;
2422

25-
class Deploy extends Command
23+
class Deploy extends ApplicationCommand
2624
{
2725
protected function configure(): void
2826
{
@@ -31,27 +29,21 @@ protected function configure(): void
3129
$this
3230
->setName('deploy')
3331
->setDescription('Deploy the application')
34-
->addOption('env', 'e', InputOption::VALUE_REQUIRED, 'The environment to deploy to', 'dev')
35-
->addOption('config', 'c', InputOption::VALUE_REQUIRED, 'The location of the configuration file to use')
36-
->addOption('team', null, InputOption::VALUE_REQUIRED, 'Override the team to deploy to')
3732
->addOption('force', null, InputOption::VALUE_NONE, 'Force the deployment');
33+
parent::configure();
3834
}
3935

4036
protected function execute(InputInterface $input, OutputInterface $output): int
4137
{
4238
IO::writeln([Styles::brefHeader(), '']);
4339

44-
$brefCloud = new BrefCloudClient;
45-
46-
/** @var string $environment */
47-
$environment = $input->getOption('env');
48-
49-
/** @var string|null $configFileName */
50-
$configFileName = $input->getOption('config');
40+
[
41+
'appName' => $appName,
42+
'environmentName' => $environment,
43+
'config' => $config,
44+
] = $this->parseStandardOptions($input);
5145

52-
$config = Config::loadConfig($configFileName, $environment, $input->getOption('team'));
53-
54-
$appName = $config['name'];
46+
$brefCloud = new BrefCloudClient;
5547

5648
IO::writeln([
5749
sprintf("Deploying %s to environment %s", Styles::bold($appName), Styles::bold($environment)),

src/Commands/Info.php

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,37 @@
22

33
namespace Bref\Cli\Commands;
44

5-
use Amp\ByteStream\BufferException;
6-
use Amp\Process\Process;
7-
use Amp\Process\ProcessException;
85
use Bref\Cli\BrefCloudClient;
96
use Bref\Cli\Cli\IO;
107
use Bref\Cli\Cli\Styles;
11-
use Bref\Cli\Components\ServerlessFramework;
12-
use Bref\Cli\Config;
13-
use Exception;
14-
use Symfony\Component\Console\Command\Command;
158
use Symfony\Component\Console\Input\InputInterface;
16-
use Symfony\Component\Console\Input\InputOption;
179
use Symfony\Component\Console\Output\OutputInterface;
18-
use Symfony\Component\Console\Question\ChoiceQuestion;
19-
use Symfony\Component\HttpClient\HttpClient;
20-
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
21-
use ZipArchive;
22-
use function Amp\ByteStream\buffer;
23-
use function Amp\delay;
2410

25-
class Info extends Command
11+
class Info extends ApplicationCommand
2612
{
2713
protected function configure(): void
2814
{
2915
$this
3016
->setName('info')
31-
->setDescription('Show information about the application')
32-
->addOption('env', 'e', InputOption::VALUE_REQUIRED, 'The environment', 'dev')
33-
->addOption('config', 'c', InputOption::VALUE_REQUIRED, 'The location of the configuration file to use')
34-
->addOption('team', null, InputOption::VALUE_REQUIRED, 'Override the team');
17+
->setDescription('Show information about the application');
18+
parent::configure();
3519
}
3620

3721
protected function execute(InputInterface $input, OutputInterface $output): int
3822
{
39-
/** @var string $environment */
40-
$environment = $input->getOption('env');
41-
/** @var string|null $configFileName */
42-
$configFileName = $input->getOption('config');
43-
$config = Config::loadConfig($configFileName, $environment, $input->getOption('team'));
44-
$appName = $config['name'];
23+
[
24+
'appName' => $appName,
25+
'environmentName' => $environmentName,
26+
'config' => $config,
27+
] = $this->parseStandardOptions($input);
28+
4529
IO::writeln([
46-
sprintf("%s %s / %s", Styles::brefLogo(), Styles::bold($appName), Styles::bold($environment)),
30+
sprintf("%s %s / %s", Styles::brefLogo(), Styles::bold($appName), Styles::bold($environmentName)),
4731
'',
4832
]);
4933

5034
$brefCloud = new BrefCloudClient;
51-
$environment = $brefCloud->getEnvironment($config['team'], $appName, $environment);
35+
$environment = $brefCloud->getEnvironment($config['team'], $appName, $environmentName);
5236
$environmentLink = $brefCloud->url . '/environment/' . $environment['id'];
5337
IO::writeln([
5438
"<href=$environmentLink>" . Styles::gray($environmentLink) . '</>',

0 commit comments

Comments
 (0)