Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add categories on json list #149

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion app/Commands/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Commands;

use App\InitializesCommands;
use App\Services\Category;
use App\Shell\Docker;
use LaravelZero\Framework\Commands\Command;

Expand All @@ -20,7 +21,10 @@ public function handle(): void
$containersCollection = app(Docker::class)->takeoutContainers();

if ($this->option('json')) {
$this->line($containersCollection->toJson());
$this->line($containersCollection->map(function ($item) {
$item['category'] = Category::fromContainerName($item['names']);
return $item;
})->toJson());
return;
}

Expand Down
9 changes: 9 additions & 0 deletions app/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ public function all(): array
return $this->services;
}

public function allByCategory(): array
{
return collect((new Services)->all())
->mapWithKeys(function ($fqcn, $shortName) {
return [$shortName => $fqcn::category()];
})
->toArray();
}

public function classesInServicesNamespace(): array
{
return collect(scandir(base_path('app/Services')))->reject(function ($file) {
Expand Down
21 changes: 21 additions & 0 deletions app/Services/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Services;

use App\Services;

abstract class Category
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this has been sitting open for ages so I'm sorry to make one more request, but could you write tests for these two new methods?

Thanks so much!

{
const CACHE = 'Cache';
Expand All @@ -10,4 +12,23 @@ abstract class Category
const SEARCH = 'Search';
const STORAGE = 'Storage';
const TOOLS = 'Tools';

public static function fromServiceName(string $serviceName): string
{
$serviceByCategory = (new Services)->allByCategory();
return array_key_exists($serviceName, $serviceByCategory) ?
strtolower($serviceByCategory[$serviceName]) :
'other';
}

public static function fromContainerName(string $containerName): string
{
$serviceName = array_slice(
explode('--', $containerName),
1,
1
);
$serviceName = reset($serviceName);
return self::fromServiceName($serviceName);
}
}