Skip to content

Commit a32cf20

Browse files
committed
Add check for running services before switching projects
1 parent 23cd5e0 commit a32cf20

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/Commands/Projects/SwitchProjectCommand.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,24 @@
22

33
namespace Somnambulist\ProjectManager\Commands\Projects;
44

5+
use Exception;
56
use Somnambulist\ProjectManager\Commands\AbstractCommand;
7+
use Somnambulist\ProjectManager\Commands\Behaviours\DockerAwareCommand;
8+
use Somnambulist\ProjectManager\Commands\Behaviours\GetCurrentActiveProject;
69
use Somnambulist\ProjectManager\Commands\Behaviours\GetProjectFromInput;
710
use Somnambulist\ProjectManager\Commands\Behaviours\ProjectConfigAwareCommand;
811
use Somnambulist\ProjectManager\Commands\Behaviours\UseEnvironmentTemplate;
12+
use Somnambulist\ProjectManager\Contracts\DockerAwareInterface;
913
use Somnambulist\ProjectManager\Contracts\ProjectConfigAwareInterface;
1014
use Somnambulist\ProjectManager\Models\Project;
15+
use Somnambulist\ProjectManager\Models\Service;
1116
use Symfony\Component\Console\Input\InputArgument;
1217
use Symfony\Component\Console\Input\InputInterface;
1318
use Symfony\Component\Console\Output\OutputInterface;
1419
use function array_filter;
1520
use function file_put_contents;
1621
use function implode;
22+
use function strtolower;
1723
use const DIRECTORY_SEPARATOR;
1824

1925
/**
@@ -22,10 +28,12 @@
2228
* @package Somnambulist\ProjectManager\Commands\Projects
2329
* @subpackage Somnambulist\ProjectManager\Commands\Projects\SwitchProjectCommand
2430
*/
25-
class SwitchProjectCommand extends AbstractCommand implements ProjectConfigAwareInterface
31+
class SwitchProjectCommand extends AbstractCommand implements DockerAwareInterface, ProjectConfigAwareInterface
2632
{
2733

2834
use UseEnvironmentTemplate;
35+
use DockerAwareCommand;
36+
use GetCurrentActiveProject;
2937
use GetProjectFromInput;
3038
use ProjectConfigAwareCommand;
3139

@@ -43,6 +51,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
4351
{
4452
$this->setupConsoleHelper($input, $output);
4553

54+
$this->checkCurrentProjectIsRunning();
55+
4656
$project = $this->getProjectFrom($input);
4757

4858
$this->tools()->warning('switching active project to <info>%s</info>', $project->name());
@@ -73,4 +83,29 @@ private function makePath(?string ...$args): string
7383
{
7484
return implode(DIRECTORY_SEPARATOR, array_filter($args));
7585
}
86+
87+
private function checkCurrentProjectIsRunning(): void
88+
{
89+
try {
90+
$project = $this->getActiveProject();
91+
$cnt = $project->services()->list()
92+
->each(function (Service $s) {
93+
$this->docker->resolve($s);
94+
})
95+
->filter(function (Service $s) {
96+
return $s->isRunning();
97+
})
98+
->count()
99+
;
100+
101+
if ($cnt > 0) {
102+
if ('y' == strtolower($this->tools()->ask('Project <info>%s</info> has running services. Should these be stopped? [y/n] ', false, $project->name()))) {
103+
$this->tools()->info('stopping running services in <info>%s</info>', $project->name());
104+
$this->tools()->run('spm stop all');
105+
}
106+
}
107+
} catch (Exception $e) {
108+
109+
}
110+
}
76111
}

0 commit comments

Comments
 (0)