From 02625fb0d26ccb65f39011d041c7c3442ff1c8c3 Mon Sep 17 00:00:00 2001 From: Danny Date: Wed, 23 Apr 2025 23:57:11 +0100 Subject: [PATCH] Add Destroy command --- src/Command/AbstractSpinnerCommand.php | 6 ++--- src/Command/DestroyCommand.php | 34 +++++++++++++++++++++++++- src/Command/SpinCommand.php | 2 +- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/Command/AbstractSpinnerCommand.php b/src/Command/AbstractSpinnerCommand.php index 1c031dc..ae125eb 100644 --- a/src/Command/AbstractSpinnerCommand.php +++ b/src/Command/AbstractSpinnerCommand.php @@ -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' : '' ); diff --git a/src/Command/DestroyCommand.php b/src/Command/DestroyCommand.php index 250d894..1ec6cb8 100644 --- a/src/Command/DestroyCommand.php +++ b/src/Command/DestroyCommand.php @@ -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; } -} \ No newline at end of file +} + +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); +} diff --git a/src/Command/SpinCommand.php b/src/Command/SpinCommand.php index bc97dde..92f356a 100644 --- a/src/Command/SpinCommand.php +++ b/src/Command/SpinCommand.php @@ -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;