Skip to content

Commit

Permalink
using prompts on disable command
Browse files Browse the repository at this point in the history
  • Loading branch information
r2luna committed Oct 20, 2023
1 parent e26a1f8 commit 7037427
Showing 1 changed file with 65 additions and 27 deletions.
92 changes: 65 additions & 27 deletions app/Commands/DisableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
)
{

Check failure on line 32 in app/Commands/DisableCommand.php

View workflow job for this annotation

GitHub Actions / PHPCS

The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line
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;
Expand All @@ -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
Expand Down Expand Up @@ -77,7 +109,7 @@ public function disableByServiceName(string $service): void
default: // > 1
$serviceContainerId = $this->selectMenu($this->disableableServices);

if (! $serviceContainerId) {
if (!$serviceContainerId) {

Check failure on line 112 in app/Commands/DisableCommand.php

View workflow job for this annotation

GitHub Actions / PHPCS

Expected 1 space(s) after NOT operator; 0 found
return;
}
}
Expand All @@ -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
Expand All @@ -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());
}
}
}

0 comments on commit 7037427

Please sign in to comment.