Skip to content

Commit

Permalink
Add bref remove command
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Dec 3, 2024
1 parent 3993b1a commit 56948ff
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function __construct()
$this->add(new Commands\Login);
$this->add(new Commands\Deploy);
$this->add(new Commands\Info);
$this->add(new Commands\Remove);
$this->add(new Commands\Command);
$this->add(new Commands\Connect);
$this->add(new Commands\PreviousLogs);
Expand Down
25 changes: 24 additions & 1 deletion src/BrefCloudClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,25 @@ public function markDeploymentFinished(
* @throws HttpExceptionInterface
* @throws ExceptionInterface
*/
public function getEnvironment(string $teamSlug, string $appName, string $environment): array
public function getEnvironment(int $id): array
{
return $this->client->request('GET', '/api/v1/environments/' . $id)->toArray();
}

/**
* @return array{
* id: int,
* name: string,
* region: string|null,
* url: string|null,
* outputs: array<string, string>,
* app: array{id: int, name: string},
* }
*
* @throws HttpExceptionInterface
* @throws ExceptionInterface
*/
public function findEnvironment(string $teamSlug, string $appName, string $environment): array
{
return $this->client->request('GET', '/api/v1/environments/find?' . http_build_query([
'teamSlug' => $teamSlug,
Expand All @@ -173,6 +191,11 @@ public function getEnvironment(string $teamSlug, string $appName, string $enviro
]))->toArray();
}

public function removeEnvironment(int $environmentId): void
{
$this->client->request('DELETE', '/api/v1/environments/' . $environmentId);
}

/**
* @return array{success: bool, output: string}
*
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Cloud.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$teamSlug = $config['team'];

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

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

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
]);

$brefCloud = new BrefCloudClient;
$environment = $brefCloud->getEnvironment($config['team'], $appName, $environmentName);
$environment = $brefCloud->findEnvironment($config['team'], $appName, $environmentName);
$environmentLink = $brefCloud->url . '/environment/' . $environment['id'];
IO::writeln([
"<href=$environmentLink>" . Styles::gray($environmentLink) . '</>',
Expand Down
61 changes: 61 additions & 0 deletions src/Commands/Remove.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php declare(strict_types=1);

namespace Bref\Cli\Commands;

use Bref\Cli\BrefCloudClient;
use Bref\Cli\Cli\IO;
use Bref\Cli\Cli\Styles;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
use function Amp\delay;

class Remove extends ApplicationCommand
{
protected function configure(): void
{
$this
->setName('remove')
->setDescription('Remove an environment of an application');
parent::configure();
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
[
'appName' => $appName,
'environmentName' => $environmentName,
'team' => $team,
] = $this->parseStandardOptions($input);

IO::writeln([
sprintf("Removing environment %s of application %s", Styles::bold($environmentName), Styles::bold($appName)),
'',
]);

IO::spin('removing');

$brefCloud = new BrefCloudClient;
$environment = $brefCloud->findEnvironment($team, $appName, $environmentName);
$environmentId = $environment['id'];

IO::verbose('Triggering asynchronous removal of environment ' . $environmentId);
$brefCloud->removeEnvironment($environmentId);

while (true) {
try {
$brefCloud->getEnvironment($environmentId);
} catch (HttpExceptionInterface $e) {
if ($e->getResponse()->getStatusCode() === 404) {
break;
}
}

delay(1);
}

IO::spinSuccess('removed');

return 0;
}
}

0 comments on commit 56948ff

Please sign in to comment.