feat(dump): add console commands to list, create and remove dumps
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace AeonDumpManager\Command;
|
||||
|
||||
use AeonDumpManager\Service\DumpService;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'aeon:dump:create',
|
||||
description: 'Create a new SQL dump',
|
||||
)]
|
||||
class DumpCreateCommand extends Command
|
||||
{
|
||||
public function __construct(private readonly DumpService $dumpService)
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
|
||||
try {
|
||||
$dumpFile = $this->dumpService->createSync();
|
||||
} catch (\Throwable $exception) {
|
||||
$io->error($exception->getMessage());
|
||||
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$io->success(sprintf('Dump created: %s', $dumpFile->filename));
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace AeonDumpManager\Command;
|
||||
|
||||
use AeonDumpManager\Service\DumpLister;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'aeon:dump:list',
|
||||
description: 'List the existing SQL dumps',
|
||||
)]
|
||||
class DumpListCommand extends Command
|
||||
{
|
||||
public function __construct(private readonly DumpLister $dumpLister)
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$dumps = $this->dumpLister->list();
|
||||
|
||||
if ($dumps === []) {
|
||||
$io->info('No dumps found.');
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
$io->table(
|
||||
['Filename', 'Datetime', 'Size'],
|
||||
array_map(
|
||||
static fn ($dumpFile) => [
|
||||
$dumpFile->filename,
|
||||
$dumpFile->datetime->format('Y-m-d H:i:s'),
|
||||
self::formatSize($dumpFile->sizeBytes),
|
||||
],
|
||||
$dumps
|
||||
)
|
||||
);
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
private static function formatSize(int $bytes): string
|
||||
{
|
||||
$units = ['B', 'KB', 'MB', 'GB'];
|
||||
$result = (float) $bytes;
|
||||
$i = 0;
|
||||
|
||||
for (; $i < count($units) - 1; $i++) {
|
||||
if ($result / 1024 < 0.9) {
|
||||
break;
|
||||
}
|
||||
$result /= 1024;
|
||||
}
|
||||
|
||||
return number_format($result, 2) . $units[$i];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace AeonDumpManager\Command;
|
||||
|
||||
use AeonDumpManager\Service\DumpService;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'aeon:dump:remove',
|
||||
description: 'Remove an existing SQL dump',
|
||||
)]
|
||||
class DumpRemoveCommand extends Command
|
||||
{
|
||||
public function __construct(private readonly DumpService $dumpService)
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
$this->addArgument('filename', InputArgument::REQUIRED, 'Filename of the dump to remove, as shown by aeon:dump:list');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$filename = (string) $input->getArgument('filename');
|
||||
|
||||
try {
|
||||
$this->dumpService->delete($filename);
|
||||
} catch (\Throwable $exception) {
|
||||
$io->error($exception->getMessage());
|
||||
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$io->success(sprintf('Dump removed: %s', $filename));
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user