Skip to content

Commit

Permalink
Adds possibility of run again commands (#134)
Browse files Browse the repository at this point in the history
* Adds possibility of re-run commands

* Refactors to "command:again"

* Wording
  • Loading branch information
nunomaduro authored Jun 23, 2021
1 parent 0fc1361 commit 311bca9
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 96 deletions.
56 changes: 56 additions & 0 deletions src/Commands/CommandAgainCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Laravel\VaporCli\Commands;

use Laravel\VaporCli\Commands\Output\CommandResult;
use Laravel\VaporCli\Helpers;
use Symfony\Component\Console\Input\InputArgument;

class CommandAgainCommand extends Command
{
/**
* Configure the command options.
*
* @return void
*/
protected function configure()
{
$this
->setName('command:again')
->addArgument('id', InputArgument::OPTIONAL, 'The command ID')
->setDescription('Re-execute a CLI command');
}

/**
* Execute the command.
*
* @return void
*/
public function handle()
{
Helpers::ensure_api_token_is_available();

$command = $this->vapor->command(
$this->argument('id') ?? $this->vapor->latestCommand()['id']
);

$environment = $this->vapor->environment(
$command['environment']['project_id'],
$command['environment']['id']
);

Helpers::line();
Helpers::line('<fg=magenta>Vapor Command: </>php artisan '.$command['command']);
Helpers::line('<fg=magenta>Vapor Command ID:</> '.$command['id']);
Helpers::line('<fg=magenta>Vapor Environment:</> '.$environment['name']);
Helpers::line('<fg=magenta>Vapor Project:</> '.$environment['project']['name']);

if (! Helpers::confirm('Are you sure you want to run again this command?', true)) {
return 0;
}

$command = $this->vapor->commandReRun($command['id']);

(new CommandResult)->render($command);
}
}
98 changes: 2 additions & 96 deletions src/Commands/CommandCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Laravel\VaporCli\Commands;

use Laravel\VaporCli\Commands\Output\CommandResult;
use Laravel\VaporCli\Helpers;
use Laravel\VaporCli\Manifest;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -41,22 +42,7 @@ public function handle()
$this->getCommand()
);

Helpers::step('<options=bold>Executing Function...</>'.PHP_EOL);

// We will poll the backend service to get the invocations status and wait until
// it gets done processing. Once it's done we will be able to show the status
// of the invocation and any related logs which will need to get displayed.
$command = $this->waitForCommandToFinish($command);

$this->displayStatusCode($command);

$this->displayOutput($command);

Helpers::line();
Helpers::line('<fg=magenta>Vapor Command ID:</> '.$command['id']);
Helpers::line('<fg=magenta>AWS Request ID:</> '.$command['request_id']);
Helpers::line('<fg=magenta>AWS Log Group Name:</> '.$command['log_group']);
Helpers::line('<fg=magenta>AWS Log Stream Name:</> '.$command['log_stream']);
(new CommandResult)->render($command);
}

/**
Expand All @@ -68,84 +54,4 @@ protected function getCommand()
{
return $this->option('command') ?? Helpers::ask('What command would you like to execute');
}

/**
* Wait for the given command to finish executing.
*
* @param array $command
*
* @return array
*/
protected function waitForCommandToFinish(array $command)
{
while ($command['status'] !== 'finished') {
sleep(1);

$command = $this->vapor->command($command['id']);
}

return $command;
}

/**
* Display the status code of the command.
*
* @param array $command
*
* @return void
*/
protected function displayStatusCode(array $command)
{
if (! isset($command['status_code'])) {
return;
}

$command['status_code'] === 0
? Helpers::line('<finished>Status Code:</> 0')
: Helpers::line('<fg=red>Status Code:</> '.$command['status_code']);
}

/**
* Display the output of the command.
*
* @param array $command
*
* @return void
*/
protected function displayOutput(array $command)
{
Helpers::line();

if (isset($command['output'])) {
Helpers::comment('Output:');

$output = $command['output'];
$output = base64_decode($output);

if ($json = json_decode($output, true)) {
$output = $json['output'];
}

Helpers::write($output);
}
}

/**
* Display the command's log messages.
*
* @param array $command
* @param int $statusCode
*
* @return void
*/
protected function displayLog(array $command, $statusCode)
{
if (! isset($command['output']) || $statusCode !== 0) {
Helpers::line();
Helpers::comment('Function Logs:');
Helpers::line();

Helpers::write(base64_decode($command['log']));
}
}
}
117 changes: 117 additions & 0 deletions src/Commands/Output/CommandResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace Laravel\VaporCli\Commands\Output;

use Laravel\VaporCli\ConsoleVaporClient;
use Laravel\VaporCli\Helpers;

class CommandResult
{
/**
* Render the output.
*
* @param array $output
*
* @return void
*/
public function render($command)
{
Helpers::step('<options=bold>Executing Function...</>'.PHP_EOL);

// We will poll the backend service to get the invocations status and wait until
// it gets done processing. Once it's done we will be able to show the status
// of the invocation and any related logs which will need to get displayed.
$command = $this->waitForCommandToFinish($command);

$this->displayStatusCode($command);

$this->displayOutput($command);

Helpers::line();
Helpers::line('<fg=magenta>Vapor Command ID:</> '.$command['id']);
Helpers::line('<fg=magenta>AWS Request ID:</> '.$command['request_id']);
Helpers::line('<fg=magenta>AWS Log Group Name:</> '.$command['log_group']);
Helpers::line('<fg=magenta>AWS Log Stream Name:</> '.$command['log_stream']);
}

/**
* Wait for the given command to finish executing.
*
* @param array $command
*
* @return array
*/
protected function waitForCommandToFinish($command)
{
while ($command['status'] !== 'finished') {
sleep(1);

$command = Helpers::app(ConsoleVaporClient::class)
->command($command['id']);
}

return $command;
}

/**
* Display the status code of the command.
*
* @param array $command
*
* @return void
*/
protected function displayStatusCode($command)
{
if (! isset($command['status_code'])) {
return;
}

$command['status_code'] === 0
? Helpers::line('<finished>Status Code:</> 0')
: Helpers::line('<fg=red>Status Code:</> '.$command['status_code']);
}

/**
* Display the output of the command.
*
* @param array $command
*
* @return void
*/
protected function displayOutput($command)
{
Helpers::line();

if (isset($command['output'])) {
Helpers::comment('Output:');

$output = $command['output'];
$output = base64_decode($output);

if ($json = json_decode($output, true)) {
$output = $json['output'];
}

Helpers::write($output);
}
}

/**
* Display the command's log messages.
*
* @param array $command
* @param int $statusCode
*
* @return void
*/
protected function displayLog($command, $statusCode)
{
if (! isset($command['output']) || $statusCode !== 0) {
Helpers::line();
Helpers::comment('Function Logs:');
Helpers::line();

Helpers::write(base64_decode($command['log']));
}
}
}
28 changes: 28 additions & 0 deletions src/ConsoleVaporClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,22 @@ public function deleteProject($projectId)
$this->requestWithErrorHandling('delete', '/api/projects/'.$projectId);
}

/**
* Get the environment with the given ID.
*
* @param string $projectId
* @param string $environmentId
*
* @return array
*/
public function environment($projectId, $environmentId)
{
return $this->request(
'get',
'/api/projects/'.$projectId.'/environments/'.$environmentId
);
}

/**
* Create a new environment for the project.
*
Expand Down Expand Up @@ -1277,6 +1293,18 @@ public function cancelDeployment($deploymentId)
);
}

/**
* Re-runs the given command id.
*
* @param string $commandId
*
* @return array
*/
public function commandReRun($commandId)
{
return $this->requestWithErrorHandling('post', '/api/commands/'.$commandId.'/re-run');
}

/**
* Get all of the commands for the given project and environment.
*
Expand Down
1 change: 1 addition & 0 deletions vapor
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ $app->add(new Commands\UpCommand);
// Commands / Invocations...
$app->add(new Commands\CommandCommand);
$app->add(new Commands\CommandLogCommand);
$app->add(new Commands\CommandAgainCommand);
$app->add(new Commands\TinkerCommand);

// Logs...
Expand Down

0 comments on commit 311bca9

Please sign in to comment.