add summary command
This commit is contained in:
6 files changed
+207
No files matched your search
@@ -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
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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'),
|
||||
|
||||
@@ -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,
|
||||
]);
|
||||
|
||||
@@ -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');
|
||||
|
||||
Reference in new issue
Block a user