Skip to content

V1.4.0 #13

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

Merged
merged 3 commits into from
Oct 8, 2024
Merged
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
52 changes: 50 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

![image](https://github.com/user-attachments/assets/ad617e05-5d45-4b2c-a6b9-5cd095719fa3)


[![Latest Version on Packagist](https://img.shields.io/packagist/v/msamgan/laravel-env-keys-checker.svg?style=flat-square)](https://packagist.org/packages/msamgan/laravel-env-keys-checker)
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/msamgan/laravel-env-keys-checker/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/msamgan/laravel-env-keys-checker/actions?query=workflow%3Arun-tests+branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/msamgan/laravel-env-keys-checker.svg?style=flat-square)](https://packagist.org/packages/msamgan/laravel-env-keys-checker)
Expand All @@ -14,10 +13,30 @@ and you want to make sure that all the keys are available across all the .env fi
With a team of developers, it is possible that some developers might forget to add the keys they used in their .env file
to the .env.example file or the other way around.

## Table of Contents

- [Features](#features)
- [Installation](#installation)
- [Usage](#usage)
- [To check if all the keys are available across all the .env files.](#to-check-if-all-the-keys-are-available-across-all-the-env-files)
- [To check if the .env and other provided files are present in .gitignore.](#to-check-if-the-env-and-other-provided-files-are-present-in-gitignore)
- [In Test](#in-test)
- [To check if all the keys are available across all the .env files.](#to-check-if-all-the-keys-are-available-across-all-the-env-files-1)
- [To check if the .env and other provided files are present in .gitignore.](#to-check-if-the-env-and-other-provided-files-are-present-in-gitignore-1)
- [Configuration](#configuration)
- [Testing](#testing)
- [Changelog](#changelog)
- [Contributing](#contributing)
- [Security Vulnerabilities](#security-vulnerabilities)
- [Credits](#credits)
- [License](#license)

## Features

- Check if all the keys are available across all the .env files.
- Add the missing keys to the .env files automatically (configurable) considering the line numbers and empty lines.
- Check if the .env and other provided files are present in .gitignore, so that they are not committed to git by
mistake.

## Installation

Expand All @@ -35,13 +54,24 @@ php artisan vendor:publish --tag="env-keys-checker-config"

## Usage

### To check if all the keys are available across all the .env files.

```bash
php artisan env:keys-check
```

### To check if the .env and other provided files are present in .gitignore.

```bash
php artisan env:in-git-ignore
```

## In Test

You can also use this package in your test cases to make sure that all the keys are available across all the .env files.
You can also use this package in your test cases to make sure the required feature is working as expected.

### To check if all the keys are available across all the .env files.

Add the following code to your test case.

Make sure to add ``--auto-add=none`` to override the default configuration.
Expand All @@ -52,6 +82,16 @@ it('tests that the .env key are same across all .env files.', function () {
});
```

### To check if the .env and other provided files are present in .gitignore.

Add the following code to your test case.

```php
it('tests that the .env and other provided files are present in .gitignore.', function () {
$this->artisan('env:in-git-ignore')->assertExitCode(0);
});
```

## Configuration

You can configure the package by publishing the configuration file.
Expand Down Expand Up @@ -80,6 +120,14 @@ You can configure the package by publishing the configuration file.
'auto_add' => 'ask',
```

```php
# config/env-keys-checker.php
# List of all the .env.* files to be checked if they
# are present in the .gitignore file.

'gitignore_files' => ['.env'],
```

## Testing

```bash
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"extra": {
"laravel": {
"providers": [
"Msamgan\\LaravelEnvKeysChecker\\LaravelEnvKeysCheckerServiceProvider"
"Msamgan\\LaravelEnvKeysChecker\\LaravelEnvKeysCheckerServiceProvider"
],
"aliases": {
"LaravelEnvKeysChecker": "Msamgan\\LaravelEnvKeysChecker\\Facades\\LaravelEnvKeysChecker"
Expand All @@ -81,4 +81,4 @@
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
4 changes: 4 additions & 0 deletions config/env-keys-checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@
// auto: will add the missing keys automatically
// none: will not add the missing keys
'auto_add' => 'ask',

// List of all the .env.* files to be checked if they
// are present in the .gitignore file
'gitignore_files' => ['.env'],
];
47 changes: 47 additions & 0 deletions src/Commands/EnvInGitIgnoreCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Msamgan\LaravelEnvKeysChecker\Commands;

use Illuminate\Console\Command;
use Msamgan\LaravelEnvKeysChecker\Concerns\HelperFunctions;

class EnvInGitIgnoreCommand extends Command
{
use HelperFunctions;

public $signature = 'env:in-git-ignore';

public $description = 'Check if .env file is in .gitignore file.';

public function handle(): int
{
$gitIgnoreFile = base_path('.gitignore');

if (! file_exists($gitIgnoreFile)) {
$this->showFailureInfo(message: '.gitignore file not found.');

return self::FAILURE;
}

$gitIgnoreContent = array_map('trim', file($gitIgnoreFile));

$filesToCheck = config('env-keys-checker.gitignore_files', ['.env']);

$missingFiles = collect();
collect($filesToCheck)->each(function ($file) use ($gitIgnoreContent, $missingFiles) {
if (! in_array($file, $gitIgnoreContent)) {
$missingFiles->push($file);
}
});

if ($missingFiles->isEmpty()) {
$this->showSuccessInfo(message: 'All files are present in .gitignore file.');

return self::SUCCESS;
}

$this->showFailureInfo(message: $missingFiles->implode(', ') . ' file(s) not found in .gitignore file.');

return self::FAILURE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
namespace Msamgan\LaravelEnvKeysChecker\Commands;

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

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

use Msamgan\LaravelEnvKeysChecker\Actions\AddKeys;
use Msamgan\LaravelEnvKeysChecker\Actions\CheckKeys;
use Msamgan\LaravelEnvKeysChecker\Actions\GetKeys;
use Msamgan\LaravelEnvKeysChecker\Concerns\HelperFunctions;

class LaravelEnvKeysCheckerCommand extends Command
class KeysCheckerCommand extends Command
{
use HelperFunctions;

public $signature = 'env:keys-check {--auto-add=}';

public $description = 'Check if all keys in .env file are present across all .env files. Like .env, .env.example, .env.testing, etc.';
Expand All @@ -28,13 +33,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 +55,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 +92,20 @@ public function handle(GetKeys $getKeys, CheckKeys $checkKeys, AddKeys $addKeys)
}

return self::FAILURE;

}

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()
);
}
}
19 changes: 19 additions & 0 deletions src/Concerns/HelperFunctions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Msamgan\LaravelEnvKeysChecker\Concerns;

use function Laravel\Prompts\error;
use function Laravel\Prompts\info;

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

private function showFailureInfo(string $message): void
{
error(' !! ' . $message);
}
}
6 changes: 4 additions & 2 deletions src/LaravelEnvKeysCheckerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Msamgan\LaravelEnvKeysChecker;

use Msamgan\LaravelEnvKeysChecker\Commands\LaravelEnvKeysCheckerCommand;
use Msamgan\LaravelEnvKeysChecker\Commands\EnvInGitIgnoreCommand;
use Msamgan\LaravelEnvKeysChecker\Commands\KeysCheckerCommand;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

Expand All @@ -18,6 +19,7 @@ public function configurePackage(Package $package): void
$package
->name('laravel-env-keys-checker')
->hasConfigFile()
->hasCommand(LaravelEnvKeysCheckerCommand::class);
->hasCommand(KeysCheckerCommand::class)
->hasCommand(EnvInGitIgnoreCommand::class);
}
}