|
| 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