Skip to content

Commit

Permalink
Add bref cloud command
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Nov 23, 2024
1 parent e9cd060 commit b99b0bd
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function __construct()
$this->add(new Commands\Command);
$this->add(new Commands\Connect);
$this->add(new Commands\PreviousLogs);
$this->add(new Commands\Cloud);
}

public function doRun(InputInterface $input, OutputInterface $output): int
Expand Down
15 changes: 15 additions & 0 deletions src/BrefCloudClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,21 @@ public function markDeploymentFinished(
]);
}

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

/**
* @return array{success: bool, output: string}
*
Expand Down
23 changes: 23 additions & 0 deletions src/Cli/OpenUrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);

namespace Bref\Cli\Cli;

class OpenUrl
{
public static function open(string $url): void
{
switch (php_uname('s')) {
case 'Darwin':
exec('open ' . escapeshellarg($url));
break;
case 'Windows':
exec('start ' . escapeshellarg($url));
break;
default:
if (exec('which xdg-open')) {
exec('xdg-open ' . escapeshellarg($url));
}
break;
}
}
}
54 changes: 54 additions & 0 deletions src/Commands/Cloud.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php declare(strict_types=1);

namespace Bref\Cli\Commands;

use Bref\Cli\BrefCloudClient;
use Bref\Cli\Cli\IO;
use Bref\Cli\Cli\OpenUrl;
use Bref\Cli\Cli\Styles;
use Bref\Cli\Config;
use JsonException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class Cloud extends \Symfony\Component\Console\Command\Command
{
protected function configure(): void
{
$this
->setName('cloud')
->setDescription('Open the Bref Cloud dashboard')
->addOption('config', 'c', InputOption::VALUE_REQUIRED, 'The location of the configuration file to use')
->addOption('env', 'e', InputOption::VALUE_REQUIRED, 'The environment', 'dev');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
/** @var string|null $configFileName */
$configFileName = $input->getOption('config');
/** @var string $environment */
$environment = $input->getOption('env');

$config = Config::loadConfig($configFileName, $environment, null);
$appName = $config['name'];
$teamSlug = $config['team'];

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

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

IO::writeln([Styles::brefHeader(), '']);
IO::writeln([
'Opening the Bref Cloud dashboard for the environment',
Styles::gray(Styles::underline($url)),
'',
]);

OpenUrl::open($url);

return 0;
}
}
20 changes: 2 additions & 18 deletions src/Commands/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Bref\Cli\BrefCloudClient;
use Bref\Cli\Cli\IO;
use Bref\Cli\Cli\OpenUrl;
use Bref\Cli\Cli\Styles;
use Bref\Cli\Token;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -33,7 +34,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
'Once that is done, please paste your Bref Cloud token below.',
'',
]);
$this->open($url);
OpenUrl::open($url);

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

return 0;
}

private function open(string $url): void
{
switch (php_uname('s')) {
case 'Darwin':
exec('open ' . escapeshellarg($url));
break;
case 'Windows':
exec('start ' . escapeshellarg($url));
break;
default:
if (exec('which xdg-open')) {
exec('xdg-open ' . escapeshellarg($url));
}
break;
}
}
}
2 changes: 1 addition & 1 deletion src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static function loadConfig(?string $fileName, ?string $environment, ?stri
if ($fileName) {
$fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
if ($fileExtension === 'yml' || $fileExtension === 'yaml') {
return self::loadServerlessConfig($fileName);
return self::loadServerlessConfig($fileName, $overrideTeam);
}
return self::loadBrefConfig($fileName, $environment, $overrideTeam);
}
Expand Down

0 comments on commit b99b0bd

Please sign in to comment.