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

Progress bar #12

Merged
merged 3 commits into from
Oct 8, 2024
Merged
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
64 changes: 48 additions & 16 deletions src/Commands/KeysCheckerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
namespace Msamgan\LaravelEnvKeysChecker\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Collection;

use function Laravel\Prompts\confirm;
use function Laravel\Prompts\error;
use function Laravel\Prompts\info;
use function Laravel\Prompts\progress;
use function Laravel\Prompts\table;

use Msamgan\LaravelEnvKeysChecker\Actions\AddKeys;
Expand All @@ -28,13 +32,17 @@ public function handle(GetKeys $getKeys, CheckKeys $checkKeys, AddKeys $addKeys)
$autoAddStrategy = $autoAddOption ?: config('env-keys-checker.auto_add', 'ask');

if (! in_array($autoAddStrategy, $autoAddAvailableOptions)) {
$this->error('!! Invalid auto add option provided. Available options are: ' . implode(', ', $autoAddAvailableOptions));
$this->showFailureInfo(
message: 'Invalid auto add option provided. Available options are: ' . implode(', ', $autoAddAvailableOptions)
);

return self::FAILURE;
}

if (empty($envFiles)) {
$this->error('!! No .env files found.');
$this->showFailureInfo(
message: 'No .env files found.'
);

return self::FAILURE;
}
Expand All @@ -46,32 +54,31 @@ public function handle(GetKeys $getKeys, CheckKeys $checkKeys, AddKeys $addKeys)
$keys = $getKeys->handle(files: $envFiles);

$missingKeys = collect();
$keys->each(function ($keyData) use ($envFiles, $missingKeys, $checkKeys) {
$checkKeys->handle(keyData: $keyData, envFiles: $envFiles, missingKeys: $missingKeys);
});

progress(
label: 'Checking keys...',
steps: $keys,
callback: fn ($key) => $checkKeys->handle(keyData: $key, envFiles: $envFiles, missingKeys: $missingKeys),
hint: 'It won\'t take long.'
);

if ($missingKeys->isEmpty()) {
$this->info('=> All keys are present in across all .env files.');
$this->showSuccessInfo(
message: 'All keys are present in all .env files.'
);

return self::SUCCESS;
}

table(
headers: ['Line', 'Key', 'Is missing in'],
rows: $missingKeys->map(function ($missingKey) {
return [
$missingKey['line'],
$missingKey['key'],
$missingKey['envFile'],
];
})->toArray()
);
$this->showMissingKeysTable($missingKeys);

if ($autoAddStrategy === 'ask') {
$confirmation = confirm('Do you want to add the missing keys to the .env files?');

if ($confirmation) {
$addKeys->handle(missingKeys: $missingKeys);

$this->showSuccessInfo('All missing keys have been added to the .env files.');
}

return self::SUCCESS;
Expand All @@ -84,5 +91,30 @@ public function handle(GetKeys $getKeys, CheckKeys $checkKeys, AddKeys $addKeys)
}

return self::FAILURE;

}

private function showSuccessInfo(string $message): void
{
info(' => ' . $message);
}

private function showFailureInfo(string $message): void
{
error(' !! ' . $message);
}

private function showMissingKeysTable(Collection $missingKeys): void
{
table(
headers: ['Line', 'Key', 'Is missing in'],
rows: $missingKeys->map(function ($missingKey) {
return [
$missingKey['line'],
$missingKey['key'],
$missingKey['envFile'],
];
})->toArray()
);
}
}