Skip to content

Commit

Permalink
refactors to active containers and adds dropdown to logs cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
gcavanunez committed Dec 18, 2024
1 parent 245c6bc commit adc8b6b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 12 deletions.
58 changes: 54 additions & 4 deletions app/Commands/LogCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@

use App\InitializesCommands;
use App\Shell\Docker;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use LaravelZero\Framework\Commands\Command;

use function Laravel\Prompts\select;

class LogCommand extends Command
{
use InitializesCommands;

const MENU_TITLE = 'Takeout containers logs';

protected $signature = 'logs {containerId?}';
protected $description = 'Display container logs.';
protected $docker;
Expand All @@ -20,17 +25,25 @@ public function handle(Docker $docker): void
$this->docker = $docker;
$this->initializeCommand();

$container = $this->argument('containerId');
$loggableContainers = $this->loggableContainers();


if ($loggableContainers->isEmpty()) {
$this->info("No Takeout containers available.\n");

return;
}

if (! $container) {
$this->error("Please pass a valid container ID.\n");
if (filled($service = $this->argument('containerId'))) {
$this->logsByServiceNameOrContainerId($service, $loggableContainers);

return;
}

$this->logs($container);
$this->logs($this->selectOptions($loggableContainers));
}


public function logs(string $container): void
{
if (Str::contains($container, ' -')) {
Expand All @@ -39,4 +52,41 @@ public function logs(string $container): void

$this->docker->logContainer($container);
}

private function loggableContainers(): Collection
{
return $this->docker->activeTakeoutContainers()->mapWithKeys(function ($container) {
return [$container['container_id'] => str_replace('TO--', '', $container['names'])];
});
}

private function selectOptions($stoppableContainers)
{
return select(
label: self::MENU_TITLE,
options: $stoppableContainers
);
}

private function logsByServiceNameOrContainerId(string $service, Collection $loggableContainers): void
{
$containersByServiceName = $loggableContainers
->filter(function ($containerName, $key) use ($service) {
return Str::startsWith($containerName, $service) || $key === $service;
});

if ($containersByServiceName->isEmpty()) {
$this->info('No containers found for ' . $service);

return;
}

if ($containersByServiceName->count() === 1) {
$this->logs($containersByServiceName->keys()->first());

return;
}

$this->logs($this->selectOptions($containersByServiceName));
}
}
2 changes: 1 addition & 1 deletion app/Commands/StopCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function handle(Docker $docker, Environment $environment): void

private function stoppableContainers(): Collection
{
return $this->docker->stoppableTakeoutContainers()->mapWithKeys(function ($container) {
return $this->docker->activeTakeoutContainers()->mapWithKeys(function ($container) {
return [$container['container_id'] => str_replace('TO--', '', $container['names'])];
});
}
Expand Down
8 changes: 4 additions & 4 deletions app/Shell/Docker.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(

public function removeContainer(string $containerId): void
{
if ($this->stoppableTakeoutContainers()->contains(function ($container) use ($containerId) {
if ($this->activeTakeoutContainers()->contains(function ($container) use ($containerId) {
return $container['container_id'] === $containerId;
})) {
$this->stopContainer($containerId);
Expand All @@ -43,7 +43,7 @@ public function removeContainer(string $containerId): void

public function stopContainer(string $containerId): void
{
if (! $this->stoppableTakeoutContainers()->contains(function ($container) use ($containerId) {
if (! $this->activeTakeoutContainers()->contains(function ($container) use ($containerId) {
return $container['container_id'] === $containerId;
})) {
throw new DockerContainerMissingException($containerId);
Expand All @@ -58,7 +58,7 @@ public function stopContainer(string $containerId): void

public function logContainer(string $containerId): void
{
if (! $this->stoppableTakeoutContainers()->contains(function ($container) use ($containerId) {
if (! $this->activeTakeoutContainers()->contains(function ($container) use ($containerId) {
return $container['container_id'] === $containerId;
})) {
throw new DockerContainerMissingException($containerId);
Expand Down Expand Up @@ -116,7 +116,7 @@ public function startableTakeoutContainers(): Collection
});
}

public function stoppableTakeoutContainers(): Collection
public function activeTakeoutContainers(): Collection
{
return $this->takeoutContainers()->filter(function ($container) {
return Str::contains($container['status'], 'Up');
Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/StopCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function it_can_stop_a_service_from_menu()
$this->mock(Docker::class, function ($mock) use ($services, $containerId) {
$mock->shouldReceive('isInstalled')->andReturn(true);
$mock->shouldReceive('isDockerServiceRunning')->andReturn(true);
$mock->shouldReceive('stoppableTakeoutContainers')->andReturn($services, new Collection);
$mock->shouldReceive('activeTakeoutContainers')->andReturn($services, new Collection);
$mock->shouldReceive('stopContainer')->with($containerId);
});

Expand All @@ -51,7 +51,7 @@ function it_can_stop_containers_by_service_name()
$this->mock(Docker::class, function ($mock) use ($services, $containerId) {
$mock->shouldReceive('isInstalled')->andReturn(true);
$mock->shouldReceive('isDockerServiceRunning')->andReturn(true);
$mock->shouldReceive('stoppableTakeoutContainers')->andReturn($services, new Collection);
$mock->shouldReceive('activeTakeoutContainers')->andReturn($services, new Collection);
$mock->shouldReceive('stopContainer')->with($containerId)->once();
});

Expand Down Expand Up @@ -84,7 +84,7 @@ function it_can_stop_a_service_from_menu_when_there_are_multiple()
$this->mock(Docker::class, function ($mock) use ($services, $secondContainerId) {
$mock->shouldReceive('isInstalled')->andReturn(true);
$mock->shouldReceive('isDockerServiceRunning')->andReturn(true);
$mock->shouldReceive('stoppableTakeoutContainers')->andReturn($services, new Collection);
$mock->shouldReceive('activeTakeoutContainers')->andReturn($services, new Collection);
$mock->shouldReceive('stopContainer')->with($secondContainerId)->once();
});

Expand Down

0 comments on commit adc8b6b

Please sign in to comment.