-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds possibility of run again commands (#134)
* Adds possibility of re-run commands * Refactors to "command:again" * Wording
- Loading branch information
1 parent
0fc1361
commit 311bca9
Showing
5 changed files
with
204 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'])); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters