add summary command
This commit is contained in:
6 files changed
+207
No files matched your search
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
namespace MeghdadFadaee\LaravelFtpDeploy\Console\Commands;
|
||||
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Console\Attributes\Description;
|
||||
use Illuminate\Console\Attributes\Signature;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Http\Client\RequestException;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Number;
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
use function Laravel\Prompts\error;
|
||||
use function Laravel\Prompts\info;
|
||||
use function Laravel\Prompts\intro;
|
||||
use function Laravel\Prompts\outro;
|
||||
use function Laravel\Prompts\table;
|
||||
|
||||
#[Signature('ftp:api-summary
|
||||
{url? : Full URL for the FTP deploy sync-state JSON API}
|
||||
{--token= : API token for the Authorization bearer header}
|
||||
{--timeout= : Request timeout in seconds}')]
|
||||
#[Description('Call the FTP deploy sync-state API and print a summary of the result')]
|
||||
class CallFtpDeployApiCommand extends Command
|
||||
{
|
||||
public function handle(): int
|
||||
{
|
||||
try {
|
||||
$url = $this->apiUrl();
|
||||
$token = $this->apiToken();
|
||||
$timeout = $this->timeout();
|
||||
|
||||
intro('Calling FTP deploy API');
|
||||
info("Endpoint: {$url}");
|
||||
|
||||
$response = Http::acceptJson()
|
||||
->withToken($token)
|
||||
->timeout($timeout)
|
||||
->get($url)
|
||||
->throw();
|
||||
|
||||
$payload = $response->json();
|
||||
|
||||
if (! is_array($payload)) {
|
||||
throw new RuntimeException('FTP deploy API did not return a JSON object.');
|
||||
}
|
||||
|
||||
if (Arr::get($payload, 'success') !== true) {
|
||||
throw new RuntimeException('FTP deploy API response did not report success.');
|
||||
}
|
||||
|
||||
$this->renderSummary($payload);
|
||||
|
||||
outro('FTP deploy API summary complete.');
|
||||
|
||||
return self::SUCCESS;
|
||||
} catch (RequestException $exception) {
|
||||
error("FTP deploy API request failed with status {$exception->response->status()}.");
|
||||
|
||||
return self::FAILURE;
|
||||
} catch (Throwable $exception) {
|
||||
error($exception->getMessage());
|
||||
|
||||
if ($this->output->isVerbose()) {
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
private function apiUrl(): string
|
||||
{
|
||||
$url = $this->argumentString('url') ?: config('services.ftp_deploy.api_url');
|
||||
|
||||
if (! is_string($url) || trim($url) === '') {
|
||||
throw new RuntimeException('No FTP deploy API URL is configured. Pass {url} or set FTP_DEPLOY_API_URL.');
|
||||
}
|
||||
|
||||
return trim($url);
|
||||
}
|
||||
|
||||
private function apiToken(): string
|
||||
{
|
||||
$token = $this->optionString('token') ?: config('services.ftp_deploy.api_token');
|
||||
|
||||
if (! is_string($token) || $token === '') {
|
||||
throw new RuntimeException('No FTP deploy API token is configured. Pass --token or set FTP_DEPLOY_API_TOKEN.');
|
||||
}
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
private function timeout(): int
|
||||
{
|
||||
$timeout = $this->optionString('timeout') ?: config('services.ftp_deploy.api_timeout', 30);
|
||||
|
||||
if (! is_numeric($timeout) || (int) $timeout < 1) {
|
||||
throw new RuntimeException('FTP deploy API timeout must be at least 1 second.');
|
||||
}
|
||||
|
||||
return (int) $timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $payload
|
||||
*/
|
||||
private function renderSummary(array $payload): void
|
||||
{
|
||||
$generatedTime = (int) (Arr::get($payload, 'generated_time') ?: Arr::get($payload, 'state.generatedTime', 0));
|
||||
|
||||
table(
|
||||
headers: ['Metric', 'Value'],
|
||||
rows: [
|
||||
['Path', (string) Arr::get($payload, 'path', 'unknown')],
|
||||
['Files', Number::format((int) Arr::get($payload, 'files', 0))],
|
||||
['Folders', Number::format((int) Arr::get($payload, 'folders', 0))],
|
||||
['Generated time', $this->formatGeneratedTime($generatedTime)],
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
private function formatGeneratedTime(int $generatedTime): string
|
||||
{
|
||||
if ($generatedTime < 1) {
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
return CarbonImmutable::createFromTimestampMs($generatedTime)->toDateTimeString();
|
||||
}
|
||||
|
||||
private function argumentString(string $key): ?string
|
||||
{
|
||||
$value = $this->argument($key);
|
||||
|
||||
return is_string($value) && trim($value) !== '' ? $value : null;
|
||||
}
|
||||
|
||||
private function optionString(string $key): ?string
|
||||
{
|
||||
$value = $this->option($key);
|
||||
|
||||
return is_string($value) && trim($value) !== '' ? $value : null;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace MeghdadFadaee\LaravelFtpDeploy;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use MeghdadFadaee\LaravelFtpDeploy\Console\Commands\CallFtpDeployApiCommand;
|
||||
use MeghdadFadaee\LaravelFtpDeploy\Console\Commands\DeployFtpChangedFilesCommand;
|
||||
use MeghdadFadaee\LaravelFtpDeploy\Console\Commands\GenerateFtpSyncStateCommand;
|
||||
use MeghdadFadaee\LaravelFtpDeploy\Services\ExcludeMatcher;
|
||||
@@ -25,6 +26,7 @@ class LaravelFtpDeployServiceProvider extends ServiceProvider
|
||||
{
|
||||
if ($this->app->runningInConsole()) {
|
||||
$this->commands([
|
||||
CallFtpDeployApiCommand::class,
|
||||
DeployFtpChangedFilesCommand::class,
|
||||
GenerateFtpSyncStateCommand::class,
|
||||
]);
|
||||
|
||||
Reference in new issue
Block a user