diff --git a/app/Commands/DisableCommand.php b/app/Commands/DisableCommand.php index 956dc93c..e5f70109 100644 --- a/app/Commands/DisableCommand.php +++ b/app/Commands/DisableCommand.php @@ -2,11 +2,18 @@ namespace App\Commands; +use App\Exceptions\DockerMissingException; +use App\Exceptions\DockerNotAvailableException; use App\InitializesCommands; use App\Shell\Docker; use App\Shell\Environment; use LaravelZero\Framework\Commands\Command; use Throwable; +use function App\clear_terminal; +use function Laravel\Prompts\confirm; +use function Laravel\Prompts\error; +use function Laravel\Prompts\select; +use function Laravel\Prompts\spin; class DisableCommand extends Command { @@ -17,19 +24,27 @@ class DisableCommand extends Command protected $signature = 'disable {serviceNames?*} {--all}'; protected $description = 'Disable services.'; protected $disableableServices; - protected $docker; - protected $environment; - public function handle(Docker $docker, Environment $environment) + public function __construct( + protected Docker $docker, + protected Environment $environment + ) + { + parent::__construct(); + } + + /** + * @throws DockerMissingException + * @throws DockerNotAvailableException + */ + public function handle(): void { - $this->docker = $docker; - $this->environment = $environment; $this->initializeCommand(); $this->disableableServices = $this->disableableServices(); if ($this->option('all')) { foreach ($this->disableableServices as $containerId => $name) { - $this->disableByContainerId($containerId); + $this->disableByContainerId($containerId, $name); } return; @@ -49,7 +64,24 @@ public function handle(Docker $docker, Environment $environment) return; } - $this->showDisableServiceMenu(); + do { + $services = $this->disableableServices(); + + $item = $this->defaultMenu($services); + + if ($item === 'Close') { + break; + } + + clear_terminal(4); + + $this->disableByContainerId( + $item, + $services[$item] + ); + } while (true); + + clear_terminal(4); } public function disableableServices(): array @@ -77,7 +109,7 @@ public function disableByServiceName(string $service): void default: // > 1 $serviceContainerId = $this->selectMenu($this->disableableServices); - if (! $serviceContainerId) { + if (!$serviceContainerId) { return; } } @@ -103,10 +135,17 @@ private function selectMenu($disableableServices): ?string private function defaultMenu($disableableServices): ?string { - return $this->menu(self::MENU_TITLE, $disableableServices) - ->addLineBreak('', 1) - ->setPadding(2, 5) - ->open(); + if (count($disableableServices) === 0) { + return 'Close'; + } + + $disableableServices['Close'] = 'Close'; + + return select( + label: self::MENU_TITLE, + options: $disableableServices, + scroll: 10 + ); } private function windowsMenu($disableableServices): ?string @@ -118,37 +157,36 @@ private function windowsMenu($disableableServices): ?string return array_search($choice, $disableableServices); } - public function disableByContainerId(string $containerId): void + public function disableByContainerId(string $containerId, string $containerName): void { try { $volumeName = $this->docker->attachedVolumeName($containerId); - $this->task('Disabling Service...', $this->docker->removeContainer($containerId)); + clear_terminal(1); + spin( + fn() => $this->docker->removeContainer($containerId) && sleep(5), + 'Disabling Service ➡ ' . $containerName + ); + + clear_terminal(1); if ($volumeName) { - $this->info("\nThe disabled service was using a volume named {$volumeName}."); - $this->info('If you would like to remove this data, run:'); - $this->info("\n docker volume rm {$volumeName}"); + info("\nThe disabled service was using a volume named {$volumeName}."); + info('If you would like to remove this data, run:'); + info("\n docker volume rm {$volumeName}"); } if (count($this->docker->allContainers()) === 0) { $question = 'No containers are running. Turn off Docker?'; - if ($this->environment->isWindowsOs()) { - $option = $this->confirm($question); - } else { - $option = $this->menu($question, [ - 'Yes', - 'No', - ])->disableDefaultItems()->open(); - } + $option = confirm($question); - if ($option === 0 || $option === true) { + if ($option) { $this->task('Stopping Docker service ', $this->docker->stopDockerService()); } } } catch (Throwable $e) { - $this->error('Disabling failed! Error: ' . $e->getMessage()); + error('Disabling failed! Error: ' . $e->getMessage()); } } }