Skip to content

Commit b99b0bd

Browse files
committed
Add bref cloud command
1 parent e9cd060 commit b99b0bd

File tree

6 files changed

+96
-19
lines changed

6 files changed

+96
-19
lines changed

src/Application.php

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function __construct()
2525
$this->add(new Commands\Command);
2626
$this->add(new Commands\Connect);
2727
$this->add(new Commands\PreviousLogs);
28+
$this->add(new Commands\Cloud);
2829
}
2930

3031
public function doRun(InputInterface $input, OutputInterface $output): int

src/BrefCloudClient.php

+15
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,21 @@ public function markDeploymentFinished(
139139
]);
140140
}
141141

142+
/**
143+
* @return array{id: int, name: string, region: string, url: string, outputs: array<string, string>, app: array{id: int, name: string}}
144+
*
145+
* @throws HttpExceptionInterface
146+
* @throws ExceptionInterface
147+
*/
148+
public function getEnvironment(string $teamSlug, string $appName, string $environment): array
149+
{
150+
return $this->client->request('GET', '/api/v1/environments/find?' . http_build_query([
151+
'teamSlug' => $teamSlug,
152+
'appName' => $appName,
153+
'environmentName' => $environment,
154+
]))->toArray();
155+
}
156+
142157
/**
143158
* @return array{success: bool, output: string}
144159
*

src/Cli/OpenUrl.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bref\Cli\Cli;
4+
5+
class OpenUrl
6+
{
7+
public static function open(string $url): void
8+
{
9+
switch (php_uname('s')) {
10+
case 'Darwin':
11+
exec('open ' . escapeshellarg($url));
12+
break;
13+
case 'Windows':
14+
exec('start ' . escapeshellarg($url));
15+
break;
16+
default:
17+
if (exec('which xdg-open')) {
18+
exec('xdg-open ' . escapeshellarg($url));
19+
}
20+
break;
21+
}
22+
}
23+
}

src/Commands/Cloud.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bref\Cli\Commands;
4+
5+
use Bref\Cli\BrefCloudClient;
6+
use Bref\Cli\Cli\IO;
7+
use Bref\Cli\Cli\OpenUrl;
8+
use Bref\Cli\Cli\Styles;
9+
use Bref\Cli\Config;
10+
use JsonException;
11+
use Symfony\Component\Console\Input\InputArgument;
12+
use Symfony\Component\Console\Input\InputInterface;
13+
use Symfony\Component\Console\Input\InputOption;
14+
use Symfony\Component\Console\Output\OutputInterface;
15+
16+
class Cloud extends \Symfony\Component\Console\Command\Command
17+
{
18+
protected function configure(): void
19+
{
20+
$this
21+
->setName('cloud')
22+
->setDescription('Open the Bref Cloud dashboard')
23+
->addOption('config', 'c', InputOption::VALUE_REQUIRED, 'The location of the configuration file to use')
24+
->addOption('env', 'e', InputOption::VALUE_REQUIRED, 'The environment', 'dev');
25+
}
26+
27+
protected function execute(InputInterface $input, OutputInterface $output): int
28+
{
29+
/** @var string|null $configFileName */
30+
$configFileName = $input->getOption('config');
31+
/** @var string $environment */
32+
$environment = $input->getOption('env');
33+
34+
$config = Config::loadConfig($configFileName, $environment, null);
35+
$appName = $config['name'];
36+
$teamSlug = $config['team'];
37+
38+
$brefCloud = new BrefCloudClient;
39+
$environment = $brefCloud->getEnvironment($teamSlug, $appName, $environment);
40+
41+
$url = $brefCloud->url . '/environment/' . $environment['id'];
42+
43+
IO::writeln([Styles::brefHeader(), '']);
44+
IO::writeln([
45+
'Opening the Bref Cloud dashboard for the environment',
46+
Styles::gray(Styles::underline($url)),
47+
'',
48+
]);
49+
50+
OpenUrl::open($url);
51+
52+
return 0;
53+
}
54+
}

src/Commands/Login.php

+2-18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Bref\Cli\BrefCloudClient;
66
use Bref\Cli\Cli\IO;
7+
use Bref\Cli\Cli\OpenUrl;
78
use Bref\Cli\Cli\Styles;
89
use Bref\Cli\Token;
910
use Symfony\Component\Console\Command\Command;
@@ -33,7 +34,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3334
'Once that is done, please paste your Bref Cloud token below.',
3435
'',
3536
]);
36-
$this->open($url);
37+
OpenUrl::open($url);
3738

3839
$question = new Question('Bref Cloud token:');
3940
$question->setHidden(true)
@@ -63,21 +64,4 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6364

6465
return 0;
6566
}
66-
67-
private function open(string $url): void
68-
{
69-
switch (php_uname('s')) {
70-
case 'Darwin':
71-
exec('open ' . escapeshellarg($url));
72-
break;
73-
case 'Windows':
74-
exec('start ' . escapeshellarg($url));
75-
break;
76-
default:
77-
if (exec('which xdg-open')) {
78-
exec('xdg-open ' . escapeshellarg($url));
79-
}
80-
break;
81-
}
82-
}
8367
}

src/Config.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static function loadConfig(?string $fileName, ?string $environment, ?stri
1818
if ($fileName) {
1919
$fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
2020
if ($fileExtension === 'yml' || $fileExtension === 'yaml') {
21-
return self::loadServerlessConfig($fileName);
21+
return self::loadServerlessConfig($fileName, $overrideTeam);
2222
}
2323
return self::loadBrefConfig($fileName, $environment, $overrideTeam);
2424
}

0 commit comments

Comments
 (0)