Skip to content

Commit adc8b6b

Browse files
committed
refactors to active containers and adds dropdown to logs cmd
1 parent 245c6bc commit adc8b6b

File tree

4 files changed

+62
-12
lines changed

4 files changed

+62
-12
lines changed

app/Commands/LogCommand.php

+54-4
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44

55
use App\InitializesCommands;
66
use App\Shell\Docker;
7+
use Illuminate\Support\Collection;
78
use Illuminate\Support\Str;
89
use LaravelZero\Framework\Commands\Command;
910

11+
use function Laravel\Prompts\select;
12+
1013
class LogCommand extends Command
1114
{
1215
use InitializesCommands;
1316

17+
const MENU_TITLE = 'Takeout containers logs';
18+
1419
protected $signature = 'logs {containerId?}';
1520
protected $description = 'Display container logs.';
1621
protected $docker;
@@ -20,17 +25,25 @@ public function handle(Docker $docker): void
2025
$this->docker = $docker;
2126
$this->initializeCommand();
2227

23-
$container = $this->argument('containerId');
28+
$loggableContainers = $this->loggableContainers();
29+
30+
31+
if ($loggableContainers->isEmpty()) {
32+
$this->info("No Takeout containers available.\n");
33+
34+
return;
35+
}
2436

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

2840
return;
2941
}
3042

31-
$this->logs($container);
43+
$this->logs($this->selectOptions($loggableContainers));
3244
}
3345

46+
3447
public function logs(string $container): void
3548
{
3649
if (Str::contains($container, ' -')) {
@@ -39,4 +52,41 @@ public function logs(string $container): void
3952

4053
$this->docker->logContainer($container);
4154
}
55+
56+
private function loggableContainers(): Collection
57+
{
58+
return $this->docker->activeTakeoutContainers()->mapWithKeys(function ($container) {
59+
return [$container['container_id'] => str_replace('TO--', '', $container['names'])];
60+
});
61+
}
62+
63+
private function selectOptions($stoppableContainers)
64+
{
65+
return select(
66+
label: self::MENU_TITLE,
67+
options: $stoppableContainers
68+
);
69+
}
70+
71+
private function logsByServiceNameOrContainerId(string $service, Collection $loggableContainers): void
72+
{
73+
$containersByServiceName = $loggableContainers
74+
->filter(function ($containerName, $key) use ($service) {
75+
return Str::startsWith($containerName, $service) || $key === $service;
76+
});
77+
78+
if ($containersByServiceName->isEmpty()) {
79+
$this->info('No containers found for ' . $service);
80+
81+
return;
82+
}
83+
84+
if ($containersByServiceName->count() === 1) {
85+
$this->logs($containersByServiceName->keys()->first());
86+
87+
return;
88+
}
89+
90+
$this->logs($this->selectOptions($containersByServiceName));
91+
}
4292
}

app/Commands/StopCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function handle(Docker $docker, Environment $environment): void
5757

5858
private function stoppableContainers(): Collection
5959
{
60-
return $this->docker->stoppableTakeoutContainers()->mapWithKeys(function ($container) {
60+
return $this->docker->activeTakeoutContainers()->mapWithKeys(function ($container) {
6161
return [$container['container_id'] => str_replace('TO--', '', $container['names'])];
6262
});
6363
}

app/Shell/Docker.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(
2828

2929
public function removeContainer(string $containerId): void
3030
{
31-
if ($this->stoppableTakeoutContainers()->contains(function ($container) use ($containerId) {
31+
if ($this->activeTakeoutContainers()->contains(function ($container) use ($containerId) {
3232
return $container['container_id'] === $containerId;
3333
})) {
3434
$this->stopContainer($containerId);
@@ -43,7 +43,7 @@ public function removeContainer(string $containerId): void
4343

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

5959
public function logContainer(string $containerId): void
6060
{
61-
if (! $this->stoppableTakeoutContainers()->contains(function ($container) use ($containerId) {
61+
if (! $this->activeTakeoutContainers()->contains(function ($container) use ($containerId) {
6262
return $container['container_id'] === $containerId;
6363
})) {
6464
throw new DockerContainerMissingException($containerId);
@@ -116,7 +116,7 @@ public function startableTakeoutContainers(): Collection
116116
});
117117
}
118118

119-
public function stoppableTakeoutContainers(): Collection
119+
public function activeTakeoutContainers(): Collection
120120
{
121121
return $this->takeoutContainers()->filter(function ($container) {
122122
return Str::contains($container['status'], 'Up');

tests/Feature/StopCommandTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function it_can_stop_a_service_from_menu()
2525
$this->mock(Docker::class, function ($mock) use ($services, $containerId) {
2626
$mock->shouldReceive('isInstalled')->andReturn(true);
2727
$mock->shouldReceive('isDockerServiceRunning')->andReturn(true);
28-
$mock->shouldReceive('stoppableTakeoutContainers')->andReturn($services, new Collection);
28+
$mock->shouldReceive('activeTakeoutContainers')->andReturn($services, new Collection);
2929
$mock->shouldReceive('stopContainer')->with($containerId);
3030
});
3131

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

@@ -84,7 +84,7 @@ function it_can_stop_a_service_from_menu_when_there_are_multiple()
8484
$this->mock(Docker::class, function ($mock) use ($services, $secondContainerId) {
8585
$mock->shouldReceive('isInstalled')->andReturn(true);
8686
$mock->shouldReceive('isDockerServiceRunning')->andReturn(true);
87-
$mock->shouldReceive('stoppableTakeoutContainers')->andReturn($services, new Collection);
87+
$mock->shouldReceive('activeTakeoutContainers')->andReturn($services, new Collection);
8888
$mock->shouldReceive('stopContainer')->with($secondContainerId)->once();
8989
});
9090

0 commit comments

Comments
 (0)