From 311bca9188a1a622ab52157764b055b9b098e0cb Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Wed, 23 Jun 2021 16:05:45 +0100 Subject: [PATCH] Adds possibility of run again commands (#134) * Adds possibility of re-run commands * Refactors to "command:again" * Wording --- src/Commands/CommandAgainCommand.php | 56 ++++++++++++ src/Commands/CommandCommand.php | 98 +-------------------- src/Commands/Output/CommandResult.php | 117 ++++++++++++++++++++++++++ src/ConsoleVaporClient.php | 28 ++++++ vapor | 1 + 5 files changed, 204 insertions(+), 96 deletions(-) create mode 100644 src/Commands/CommandAgainCommand.php create mode 100644 src/Commands/Output/CommandResult.php diff --git a/src/Commands/CommandAgainCommand.php b/src/Commands/CommandAgainCommand.php new file mode 100644 index 00000000..050a628b --- /dev/null +++ b/src/Commands/CommandAgainCommand.php @@ -0,0 +1,56 @@ +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('Vapor Command: php artisan '.$command['command']); + Helpers::line('Vapor Command ID: '.$command['id']); + Helpers::line('Vapor Environment: '.$environment['name']); + Helpers::line('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); + } +} diff --git a/src/Commands/CommandCommand.php b/src/Commands/CommandCommand.php index b7d528ec..53459b59 100644 --- a/src/Commands/CommandCommand.php +++ b/src/Commands/CommandCommand.php @@ -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; @@ -41,22 +42,7 @@ public function handle() $this->getCommand() ); - Helpers::step('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('Vapor Command ID: '.$command['id']); - Helpers::line('AWS Request ID: '.$command['request_id']); - Helpers::line('AWS Log Group Name: '.$command['log_group']); - Helpers::line('AWS Log Stream Name: '.$command['log_stream']); + (new CommandResult)->render($command); } /** @@ -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('Status Code: 0') - : Helpers::line('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'])); - } - } } diff --git a/src/Commands/Output/CommandResult.php b/src/Commands/Output/CommandResult.php new file mode 100644 index 00000000..7007c7b7 --- /dev/null +++ b/src/Commands/Output/CommandResult.php @@ -0,0 +1,117 @@ +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('Vapor Command ID: '.$command['id']); + Helpers::line('AWS Request ID: '.$command['request_id']); + Helpers::line('AWS Log Group Name: '.$command['log_group']); + Helpers::line('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('Status Code: 0') + : Helpers::line('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'])); + } + } +} diff --git a/src/ConsoleVaporClient.php b/src/ConsoleVaporClient.php index f98628ac..d1840efd 100644 --- a/src/ConsoleVaporClient.php +++ b/src/ConsoleVaporClient.php @@ -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. * @@ -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. * diff --git a/vapor b/vapor index 3a6d7d24..89f32788 100755 --- a/vapor +++ b/vapor @@ -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...