Add Destroy command

This commit is contained in:
Daniel Winning 2025-04-23 23:57:11 +01:00
parent 9db6a5f623
commit 02625fb0d2
3 changed files with 37 additions and 5 deletions

View file

@ -41,12 +41,12 @@ class AbstractSpinnerCommand extends Command implements ConsoleCommandInterface
return Command::SUCCESS;
}
protected function buildDockerComposeCommand(string $command, bool $daemon = true): string
protected function buildDockerComposeCommand(string $command, bool $withEnv = false, bool $daemon = true): string
{
return sprintf(
'cd %s && docker compose --env-file=%s %s%s',
'cd %s && docker compose%s %s%s',
$this->config->getDataDirectory(),
$this->config->getDataDirectory() . '/.env',
$withEnv ? ' --env-file=' . $this->config->getDataDirectory() . '/.env' : '',
$command,
$daemon ? ' -d' : ''
);

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Loom\Spinner\Command;
use Loom\Spinner\Classes\Config\Config;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
@ -28,6 +29,37 @@ class DestroyCommand extends AbstractSpinnerCommand
return Command::FAILURE;
}
$this->config = new Config($input->getArgument('name'));
if (!file_exists($this->config->getDataDirectory())) {
$this->style->error('No project found with the provided name.');
return Command::FAILURE;
}
try {
passthru($this->buildDockerComposeCommand('down', false, false));
recursive_rmdir($this->config->getDataDirectory());
} catch (\Exception $exception) {
$this->style->error('An error occurred while destroying the project: ' . $exception->getMessage());
return Command::FAILURE;
}
return Command::SUCCESS;
}
}
function recursive_rmdir(string $dir): void
{
if (!is_dir($dir)) return;
$items = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($items as $item) {
$item->isDir() ? rmdir($item->getPathname()) : unlink($item->getPathname());
}
rmdir($dir);
}

View file

@ -97,7 +97,7 @@ class SpinCommand extends AbstractSpinnerCommand
$this->config = new Config($input->getArgument('name'), $this->projectWorkPath);
if ($this->config->projectDataExists($input->getArgument('name'))) {
if (file_exists($this->config->getDataDirectory())) {
$this->style->error('A project with the same name already exists.');
return Command::FAILURE;