-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathApplication.php
More file actions
104 lines (84 loc) · 3.55 KB
/
Copy pathApplication.php
File metadata and controls
104 lines (84 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php declare(strict_types=1);
namespace Bref\Cli;
use Aws\Exception\CredentialsException;
use Bref\Cli\Cli\IO;
use Bref\Cli\Cli\Styles;
use ErrorException;
use Exception;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpClient\Exception\ClientException;
use Throwable;
class Application extends \Symfony\Component\Console\Application
{
public function __construct()
{
parent::__construct('bref');
$this->turnWarningsIntoExceptions();
$this->safeAddCommand(new Commands\Login);
$this->safeAddCommand(new Commands\Whoami);
$this->safeAddCommand(new Commands\Teams);
$this->safeAddCommand(new Commands\Deploy);
$this->safeAddCommand(new Commands\Info);
$this->safeAddCommand(new Commands\Remove);
$this->safeAddCommand(new Commands\Command);
$this->safeAddCommand(new Commands\Connect);
$this->safeAddCommand(new Commands\PreviousLogs);
$this->safeAddCommand(new Commands\Cloud);
$this->safeAddCommand(new Commands\Tinker);
$this->safeAddCommand(new Commands\SecretCreate);
}
public function safeAddCommand(Command $command): ?Command
{
// addCommand() exists since Symfony Console 7.4; add() was removed in Symfony 8.
$method = method_exists(parent::class, 'addCommand') ? 'addCommand' : 'add';
/** @var callable(Command): ?Command $register */
$register = [$this, $method];
return $register($command);
}
public function doRun(InputInterface $input, OutputInterface $output): int
{
IO::init($input, $output);
if ($input->hasParameterOption(['--stage'], true)) {
throw new Exception('The "--stage" option does not exist in the "bref" CLI. Use the "--env" option instead.');
}
$result = parent::doRun($input, $output);
IO::stop();
return $result;
}
public function renderThrowable(Throwable $e, OutputInterface $output): void
{
IO::spinClear();
// Prettify Bref Cloud errors
if ($e instanceof ClientException) {
try {
$body = $e->getResponse()->toArray(false);
$message = $body['message'] ?? 'Unknown Bref Cloud error';
$statusCode = $e->getResponse()->getStatusCode();
$message = match ($statusCode) {
401 => 'Unauthenticated. Please log in with `bref login`.',
403 => 'Forbidden. You do not have the required permissions. Do you need to login to a different team?',
default => $message,
};
$e = new Exception("Bref Cloud API error: [$statusCode] $message", $statusCode);
} catch (Throwable) {
}
}
// Prettify AWS credentials errors
if ($e instanceof CredentialsException && str_contains($e->getMessage(), 'not found in credentials file')) {
IO::error(new Exception('AWS profile not found: ' . $e->getMessage()), false);
return;
}
if (! IO::isVerbose()) {
IO::writeln(Styles::gray('verbose logs are available by running `bref previous-logs`'));
}
IO::error($e);
}
private function turnWarningsIntoExceptions(): void
{
set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
});
}
}