Skip to content

Commit 56948ff

Browse files
committed
Add bref remove command
1 parent 3993b1a commit 56948ff

File tree

5 files changed

+88
-3
lines changed

5 files changed

+88
-3
lines changed

src/Application.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function __construct()
2323
$this->add(new Commands\Login);
2424
$this->add(new Commands\Deploy);
2525
$this->add(new Commands\Info);
26+
$this->add(new Commands\Remove);
2627
$this->add(new Commands\Command);
2728
$this->add(new Commands\Connect);
2829
$this->add(new Commands\PreviousLogs);

src/BrefCloudClient.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,25 @@ public function markDeploymentFinished(
164164
* @throws HttpExceptionInterface
165165
* @throws ExceptionInterface
166166
*/
167-
public function getEnvironment(string $teamSlug, string $appName, string $environment): array
167+
public function getEnvironment(int $id): array
168+
{
169+
return $this->client->request('GET', '/api/v1/environments/' . $id)->toArray();
170+
}
171+
172+
/**
173+
* @return array{
174+
* id: int,
175+
* name: string,
176+
* region: string|null,
177+
* url: string|null,
178+
* outputs: array<string, string>,
179+
* app: array{id: int, name: string},
180+
* }
181+
*
182+
* @throws HttpExceptionInterface
183+
* @throws ExceptionInterface
184+
*/
185+
public function findEnvironment(string $teamSlug, string $appName, string $environment): array
168186
{
169187
return $this->client->request('GET', '/api/v1/environments/find?' . http_build_query([
170188
'teamSlug' => $teamSlug,
@@ -173,6 +191,11 @@ public function getEnvironment(string $teamSlug, string $appName, string $enviro
173191
]))->toArray();
174192
}
175193

194+
public function removeEnvironment(int $environmentId): void
195+
{
196+
$this->client->request('DELETE', '/api/v1/environments/' . $environmentId);
197+
}
198+
176199
/**
177200
* @return array{success: bool, output: string}
178201
*

src/Commands/Cloud.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3636
$teamSlug = $config['team'];
3737

3838
$brefCloud = new BrefCloudClient;
39-
$environment = $brefCloud->getEnvironment($teamSlug, $appName, $environment);
39+
$environment = $brefCloud->findEnvironment($teamSlug, $appName, $environment);
4040

4141
$url = $brefCloud->url . '/environment/' . $environment['id'];
4242

src/Commands/Info.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3232
]);
3333

3434
$brefCloud = new BrefCloudClient;
35-
$environment = $brefCloud->getEnvironment($config['team'], $appName, $environmentName);
35+
$environment = $brefCloud->findEnvironment($config['team'], $appName, $environmentName);
3636
$environmentLink = $brefCloud->url . '/environment/' . $environment['id'];
3737
IO::writeln([
3838
"<href=$environmentLink>" . Styles::gray($environmentLink) . '</>',

src/Commands/Remove.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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\Styles;
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
11+
use function Amp\delay;
12+
13+
class Remove extends ApplicationCommand
14+
{
15+
protected function configure(): void
16+
{
17+
$this
18+
->setName('remove')
19+
->setDescription('Remove an environment of an application');
20+
parent::configure();
21+
}
22+
23+
protected function execute(InputInterface $input, OutputInterface $output): int
24+
{
25+
[
26+
'appName' => $appName,
27+
'environmentName' => $environmentName,
28+
'team' => $team,
29+
] = $this->parseStandardOptions($input);
30+
31+
IO::writeln([
32+
sprintf("Removing environment %s of application %s", Styles::bold($environmentName), Styles::bold($appName)),
33+
'',
34+
]);
35+
36+
IO::spin('removing');
37+
38+
$brefCloud = new BrefCloudClient;
39+
$environment = $brefCloud->findEnvironment($team, $appName, $environmentName);
40+
$environmentId = $environment['id'];
41+
42+
IO::verbose('Triggering asynchronous removal of environment ' . $environmentId);
43+
$brefCloud->removeEnvironment($environmentId);
44+
45+
while (true) {
46+
try {
47+
$brefCloud->getEnvironment($environmentId);
48+
} catch (HttpExceptionInterface $e) {
49+
if ($e->getResponse()->getStatusCode() === 404) {
50+
break;
51+
}
52+
}
53+
54+
delay(1);
55+
}
56+
57+
IO::spinSuccess('removed');
58+
59+
return 0;
60+
}
61+
}

0 commit comments

Comments
 (0)