From 9266cc66eb853fb683e956f923051c3a10c09ef5 Mon Sep 17 00:00:00 2001 From: Meghdad <61308304+MeghdadFadaee@users.noreply.github.com> Date: Thu, 25 Jun 2026 14:48:33 +0330 Subject: [PATCH] add summary command --- README.md | 16 ++ composer.json | 1 + config/services.php | 2 + .../Commands/CallFtpDeployApiCommand.php | 148 ++++++++++++++++++ src/LaravelFtpDeployServiceProvider.php | 2 + tests/Feature/LaravelFtpDeployTest.php | 38 +++++ 6 files changed, 207 insertions(+) create mode 100644 src/Console/Commands/CallFtpDeployApiCommand.php diff --git a/README.md b/README.md index 20fd8f4..aa8a480 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,8 @@ Customize deploy behavior in `config/services.php`: 'local_root' => env('FTP_DEPLOY_LOCAL_ROOT', base_path()), 'state_path' => env('FTP_DEPLOY_STATE_PATH', 'storage/app/.ftp-deploy-sync-state.json'), 'api_token' => env('FTP_DEPLOY_API_TOKEN'), + 'api_url' => env('FTP_DEPLOY_API_URL'), + 'api_timeout' => (int) env('FTP_DEPLOY_API_TIMEOUT', 30), 'route' => [ 'enabled' => env('FTP_DEPLOY_ROUTE_ENABLED', true), 'path' => env('FTP_DEPLOY_ROUTE_PATH', 'ftp/generate-sync-state'), @@ -65,6 +67,8 @@ FTP_DEPLOY_TIMEOUT=30 FTP_DEPLOY_DISK=ftp_deploy FTP_DEPLOY_STATE_PATH=storage/app/.ftp-deploy-sync-state.json FTP_DEPLOY_API_TOKEN= +FTP_DEPLOY_API_URL= +FTP_DEPLOY_API_TIMEOUT=30 ``` ## Commands @@ -87,6 +91,18 @@ Generate a sync-state file locally: php artisan ftp:generate-sync-state --output=storage/app/.ftp-deploy-sync-state.json ``` +Call the JSON API and print a summary: + +```bash +php artisan ftp:api-summary https://example.com/ftp/generate-sync-state --token=your-token +``` + +If `FTP_DEPLOY_API_URL` and `FTP_DEPLOY_API_TOKEN` are configured, the command can be run without arguments: + +```bash +php artisan ftp:api-summary +``` + Extra excludes may be passed as repeated options or comma-separated values: ```bash diff --git a/composer.json b/composer.json index ea3458a..4f9157e 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ "illuminate/console": "^13.0", "illuminate/contracts": "^13.0", "illuminate/filesystem": "^13.0", + "illuminate/http": "^13.0", "illuminate/routing": "^13.0", "illuminate/support": "^13.0", "laravel/prompts": "^0.3", diff --git a/config/services.php b/config/services.php index 2b23617..8e8eb00 100644 --- a/config/services.php +++ b/config/services.php @@ -6,6 +6,8 @@ return [ 'local_root' => env('FTP_DEPLOY_LOCAL_ROOT', base_path()), 'state_path' => env('FTP_DEPLOY_STATE_PATH', 'storage/app/.ftp-deploy-sync-state.json'), 'api_token' => env('FTP_DEPLOY_API_TOKEN'), + 'api_url' => env('FTP_DEPLOY_API_URL'), + 'api_timeout' => (int) env('FTP_DEPLOY_API_TIMEOUT', 30), 'route' => [ 'enabled' => env('FTP_DEPLOY_ROUTE_ENABLED', true), 'path' => env('FTP_DEPLOY_ROUTE_PATH', 'ftp/generate-sync-state'), diff --git a/src/Console/Commands/CallFtpDeployApiCommand.php b/src/Console/Commands/CallFtpDeployApiCommand.php new file mode 100644 index 0000000..5bedea1 --- /dev/null +++ b/src/Console/Commands/CallFtpDeployApiCommand.php @@ -0,0 +1,148 @@ +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 $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; + } +} diff --git a/src/LaravelFtpDeployServiceProvider.php b/src/LaravelFtpDeployServiceProvider.php index dc5f9b6..402af7d 100644 --- a/src/LaravelFtpDeployServiceProvider.php +++ b/src/LaravelFtpDeployServiceProvider.php @@ -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, ]); diff --git a/tests/Feature/LaravelFtpDeployTest.php b/tests/Feature/LaravelFtpDeployTest.php index 49f5878..a7c45b1 100644 --- a/tests/Feature/LaravelFtpDeployTest.php +++ b/tests/Feature/LaravelFtpDeployTest.php @@ -2,6 +2,7 @@ use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\File; +use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Storage; @@ -18,6 +19,8 @@ beforeEach(function (): void { 'services.ftp_deploy.local_root' => $this->localRoot, 'services.ftp_deploy.state_path' => 'storage/framework/testing/ftp-deploy-state.json', 'services.ftp_deploy.api_token' => 'secret-token', + 'services.ftp_deploy.api_url' => 'https://example.test/ftp/generate-sync-state', + 'services.ftp_deploy.api_timeout' => 10, 'services.ftp_deploy.exclude' => ['ignored', '*.tmp'], ]); @@ -33,12 +36,47 @@ test('package registers default config commands and route', function () { expect(config('filesystems.disks.ftp_deploy.driver'))->toBe('ftp') ->and(config('services.ftp_deploy.disk'))->toBe('ftp_deploy') ->and(Artisan::all())->toHaveKeys([ + 'ftp:api-summary', 'ftp:deploy', 'ftp:generate-sync-state', ]) ->and(Route::has('ftp-deploy.generate-sync-state'))->toBeTrue(); }); +test('api summary command calls configured api and prints the returned summary', function () { + Http::fake([ + 'https://example.test/ftp/generate-sync-state' => Http::response([ + 'success' => true, + 'path' => '/var/www/storage/app/.ftp-deploy-sync-state.json', + 'files' => 12, + 'folders' => 4, + 'generated_time' => 1782361358583, + 'state' => [ + 'generatedTime' => 1782361358583, + 'data' => [], + ], + ]), + ]); + + $this->artisan('ftp:api-summary') + ->expectsOutputToContain('Files') + ->assertExitCode(0); + + Http::assertSent(fn ($request): bool => $request->url() === 'https://example.test/ftp/generate-sync-state' + && $request->hasHeader('Authorization', 'Bearer secret-token')); +}); + +test('api summary command fails when api returns an error status', function () { + Http::fake([ + 'https://example.test/ftp/generate-sync-state' => Http::response([ + 'message' => 'Invalid FTP deploy API token.', + ], 403), + ]); + + $this->artisan('ftp:api-summary') + ->assertExitCode(1); +}); + test('generate sync state command writes state and honors excludes', function () { ftpDeployWriteLocalFile($this->localRoot, 'keep.txt', 'keep contents'); ftpDeployWriteLocalFile($this->localRoot, 'ignored/file.txt', 'ignored contents');