Validate project path and confirm Docker is running

This commit is contained in:
Daniel Winning 2025-04-12 10:47:55 +01:00
parent c04af3b41c
commit 524ed9f87d
5 changed files with 79 additions and 2 deletions

View file

@ -13,5 +13,5 @@ $application->add(new SpinCommand());
try { try {
$application->run(); $application->run();
} catch (\Exception $exception) { } catch (\Exception $exception) {
die(); die($exception->getMessage());
} }

View file

@ -17,5 +17,8 @@
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^12.1" "phpunit/phpunit": "^12.1"
} },
"bin": [
"bin/spinner"
]
} }

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Loom\Spinner\Command; namespace Loom\Spinner\Command;
use Loom\Spinner\Helper\System;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
@ -12,6 +13,14 @@ use Symfony\Component\Console\Style\SymfonyStyle;
class AbstractSpinnerCommand extends Command class AbstractSpinnerCommand extends Command
{ {
protected SymfonyStyle $style; protected SymfonyStyle $style;
protected System $system;
public function __construct()
{
$this->system = new System();
parent::__construct();
}
protected function setStyle(InputInterface $input, OutputInterface $output): void protected function setStyle(InputInterface $input, OutputInterface $output): void
{ {

View file

@ -4,18 +4,41 @@ declare(strict_types=1);
namespace Loom\Spinner\Command; namespace Loom\Spinner\Command;
use Loom\Utility\FilePath\FilePath;
use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(name: 'spin', description: 'Spin up a new development environment')] #[AsCommand(name: 'spin', description: 'Spin up a new development environment')]
class SpinCommand extends AbstractSpinnerCommand 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 protected function execute(InputInterface $input, OutputInterface $output): int
{ {
$this->setStyle($input, $output); $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; return Command::SUCCESS;
} }
} }

42
src/Helper/System.php Normal file
View file

@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace Loom\Spinner\Helper;
class System
{
private string $operatingSystem;
public function __construct()
{
$this->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;
}
}