From 524ed9f87ddaae1d825374d0e063e74bf8e4ce9d Mon Sep 17 00:00:00 2001 From: Daniel Winning Date: Sat, 12 Apr 2025 10:47:55 +0100 Subject: [PATCH] Validate project path and confirm Docker is running --- bin/spinner | 2 +- composer.json | 5 ++- src/Command/AbstractSpinnerCommand.php | 9 ++++++ src/Command/SpinCommand.php | 23 ++++++++++++++ src/Helper/System.php | 42 ++++++++++++++++++++++++++ 5 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 src/Helper/System.php diff --git a/bin/spinner b/bin/spinner index 4ce2717..d80180e 100644 --- a/bin/spinner +++ b/bin/spinner @@ -13,5 +13,5 @@ $application->add(new SpinCommand()); try { $application->run(); } catch (\Exception $exception) { - die(); + die($exception->getMessage()); } \ No newline at end of file diff --git a/composer.json b/composer.json index ed1bcf7..be6f07a 100644 --- a/composer.json +++ b/composer.json @@ -17,5 +17,8 @@ }, "require-dev": { "phpunit/phpunit": "^12.1" - } + }, + "bin": [ + "bin/spinner" + ] } diff --git a/src/Command/AbstractSpinnerCommand.php b/src/Command/AbstractSpinnerCommand.php index b1863b6..178b4dc 100644 --- a/src/Command/AbstractSpinnerCommand.php +++ b/src/Command/AbstractSpinnerCommand.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Loom\Spinner\Command; +use Loom\Spinner\Helper\System; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -12,6 +13,14 @@ use Symfony\Component\Console\Style\SymfonyStyle; class AbstractSpinnerCommand extends Command { protected SymfonyStyle $style; + protected System $system; + + public function __construct() + { + $this->system = new System(); + + parent::__construct(); + } protected function setStyle(InputInterface $input, OutputInterface $output): void { diff --git a/src/Command/SpinCommand.php b/src/Command/SpinCommand.php index eb761ed..cf5142e 100644 --- a/src/Command/SpinCommand.php +++ b/src/Command/SpinCommand.php @@ -4,18 +4,41 @@ declare(strict_types=1); namespace Loom\Spinner\Command; +use Loom\Utility\FilePath\FilePath; 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; #[AsCommand(name: 'spin', description: 'Spin up a new development environment')] class SpinCommand extends AbstractSpinnerCommand { + protected function configure(): void + { + $this + ->addArgument('name', InputArgument::REQUIRED, 'The name for your Docker container.') + ->addArgument('path', InputArgument::REQUIRED, 'The absolute path to your projects root directory.'); + } + protected function execute(InputInterface $input, OutputInterface $output): int { $this->setStyle($input, $output); + $projectDirectory = new FilePath($input->getArgument('path')); + + if (!$projectDirectory->exists() || !$projectDirectory->isDirectory()) { + $this->style->error('The provided path is not a valid directory.'); + + return Command::FAILURE; + } + + if (!$this->system->isDockerEngineRunning()) { + $this->style->error('It looks like the Docker Engine is not running. Please start it and try again.'); + + return Command::FAILURE; + } + return Command::SUCCESS; } } \ No newline at end of file diff --git a/src/Helper/System.php b/src/Helper/System.php new file mode 100644 index 0000000..9595733 --- /dev/null +++ b/src/Helper/System.php @@ -0,0 +1,42 @@ +operatingSystem = PHP_OS_FAMILY; + } + + public function getName(): string + { + return $this->operatingSystem; + } + + public function isWindows(): bool + { + return $this->operatingSystem === 'Windows'; + } + + public function isMacOS(): bool + { + return $this->operatingSystem === 'Darwin'; + } + + public function isLinux(): bool + { + return $this->operatingSystem === 'Linux'; + } + + public function isDockerEngineRunning(): bool + { + exec('docker info 2>&1', $output, $dockerInfoError); + + return $dockerInfoError === 0; + } +} \ No newline at end of file