Skip to content

Commit

Permalink
Env. gitignore (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
msamgan authored Oct 7, 2024
1 parent 3588148 commit bf4bfdf
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 7 deletions.
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'],
];
44 changes: 44 additions & 0 deletions src/Commands/EnvInGitIgnoreCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Msamgan\LaravelEnvKeysChecker\Commands;

use Illuminate\Console\Command;

class EnvInGitIgnoreCommand extends Command
{
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->error('!! .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->info('=> All files are present in .gitignore file.');

return self::SUCCESS;
}

$this->error('!! ' . $missingFiles->implode(', ') . ' file(s) not found in .gitignore file.');

return self::FAILURE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Msamgan\LaravelEnvKeysChecker\Actions\CheckKeys;
use Msamgan\LaravelEnvKeysChecker\Actions\GetKeys;

class LaravelEnvKeysCheckerCommand extends Command
class KeysCheckerCommand extends Command
{
public $signature = 'env:keys-check {--auto-add=}';

Expand Down
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);
}
}

0 comments on commit bf4bfdf

Please sign in to comment.