Skip to content

Commit

Permalink
Add commands, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Tekill committed May 13, 2017
1 parent 58d3fbe commit 59de8c6
Show file tree
Hide file tree
Showing 12 changed files with 335 additions and 28 deletions.
123 changes: 120 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,133 @@
[![Coverage Status][ico-scrutinizer]][link-scrutinizer]
[![Quality Score][ico-code-quality]][link-code-quality]

EnvDiff is tool to compare environment keys to find the difference between .env files and actualize them
EnvDiff is tool to compare environment keys to find the difference between .env files and actualize them.

# Usage
# Installation

composer Scripts composer event
```
composer install tekill/env-diff
```

## Manual running
### Actualize variables
Compare `.env` with `.env.dist` and add missing variables to `.env` file
```
php ./vendor/bin/env-diff actualize
```

Compare `.env` with `.env.example` and add missing variables to `.env` file
```
php ./vendor/bin/env-diff actualize .env.example
```

Compare `.env-target` with `.env.example` and add missing variables to `.env-target` file
```
php ./vendor/bin/env-diff actualize .env.example .env-target
```

If you want to delete outdated values just run command with `-k=false` option

```
php ./vendor/bin/env-diff actualize -k=false
```

### Show differences
Command has same interface, arguments and options

Compare `.env` with `.env.dist` and show differences between them
```
php ./vendor/bin/env-diff diff
```

## Composer script

Add code block in `composer.json`:
```$json
"scripts": {
"post-update-cmd": "Lf\\EnvDiff\\Composer\\ScriptHandler::actualizeEnv"
}
```

The `.env` will then be created or updated by the composer script, to match the structure of the dist
file `.env.dist` by asking you the missing variables.

By default, the dist file is assumed to be in the same place than the target `.env`
file, suffixed by `.dist`. This can be changed in the configuration:

```json
{
"extra": {
"lf-env-diff": [
{
"dist": "path/to/env.dist",
"target": "path/to/.env"
}
]
}
}
```

The script handler will ask you interactively for variables which are missing
in the target env file, using the value of the dist file as default value.
If composer is run in a non-interactive mode `--no-interaction`, the values of the dist file
will be used for missing variables.

**Warning:** This handler will overwrite any comments or spaces into your target `.env` file so handle with care.

### Managing multiple ignored files

The handler can manage multiple ignored files. To use this feature, the `lf-env-diff` extra should contain a
JSON array with multiple configurations inside it instead of a configuration object:

```json
{
"extra": {
"lf-env-diff": [
{
"dist": "path/to/.env.dist",
"target": "path/to/.env"
},
{
"dist": "path/to/.env.dist",
"target": "path/to/.env-test",
"keep-outdated": false
}
]
}
}
```

### Show difference

Add code block in `composer.json`:
```$json
"scripts": {
"post-update-cmd": "Lf\\EnvDiff\\Composer\\ScriptHandler::showDifference"
}
```

This handler has same behavior as described before.

## Git hooks

You can use Git hook that gets triggered after any 'git pull' whenever one of the files specified has changed.
Useful to update any web application dependency or sync configuration.

Create `post-merge` hook in `.git/hooks` directory of your project:
```
#/usr/bin/env bash
changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"
check_run() {
echo "$changed_files" | grep -E --quiet "$1" && eval "$2"
}
# Aclualize env files if the `env.dist` file gets changed
check_run env.dist "php ./vendor/bin/env-diff aclualize"
```

[ico-version]: https://img.shields.io/packagist/v/Tekill/env-diff.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
[ico-travis]: https://img.shields.io/travis/Tekill/env-diff/master.svg?style=flat-square
Expand Down
18 changes: 5 additions & 13 deletions env-diff
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env php
<?php

use LF\EnvDiff\Console\Application;

$autoloadPaths = [
__DIR__ . '/../../autoload.php',
__DIR__ . '/../vendor/autoload.php',
Expand All @@ -9,21 +11,11 @@ $autoloadPaths = [

foreach ($autoloadPaths as $path) {
if (file_exists($path)) {
require_once $path;

$config = [];

$processor = new LF\EnvDiff\Processor(
new \LF\EnvDiff\IO\ConsoleIO(
new \Symfony\Component\Console\Input\ArgvInput(),
new \Symfony\Component\Console\Output\ConsoleOutput()
)
);

$processor->actualizeEnv(LF\EnvDiff\Config::createFormArray($config));
$processor->showDifference(LF\EnvDiff\Config::createFormArray($config));
require_once $path;

exit(0);
$application = new Application();
$application->run();
}
}

Expand Down
22 changes: 22 additions & 0 deletions src/Console/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace LF\EnvDiff\Console;

use LF\EnvDiff\Console\Command\ActualizeCommand;
use LF\EnvDiff\Console\Command\DiffCommand;
use Symfony\Component\Console\Application as BaseApplication;

class Application extends BaseApplication
{
/**
* {@inheritdoc}
*/
public function __construct()
{
parent::__construct('Env diff', '1.0.0');

$this->setAutoExit(true);
$this->add(new DiffCommand('diff'));
$this->add(new ActualizeCommand('actualize'));
}
}
69 changes: 69 additions & 0 deletions src/Console/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace LF\EnvDiff\Console\Command;

use LF\EnvDiff\Config;
use LF\EnvDiff\IO\ConsoleIO;
use LF\EnvDiff\Processor;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

abstract class AbstractCommand extends Command
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->addArgument('dist', InputOption::VALUE_REQUIRED, 'From file', Config::DEFAULT_DIST)
->addArgument('target', InputOption::VALUE_REQUIRED, 'To file', Config::DEFAULT_TARGET)
->addOption('keep-outdated', 'k', InputOption::VALUE_OPTIONAL, 'Keep old env variables', true);
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$config = $this->createConfig($input);
$processor = $this->createProcessor($input, $output);

$this->doExecute($processor, $config);

return 0;
}

/**
* @param InputInterface $input
*
* @return Config
*/
private function createConfig(InputInterface $input)
{
$dist = $input->getArgument('dist');
$target = $input->getArgument('target');
$keepOutdated = $input->getOption('keep-outdated');

return new Config($dist, $target, $keepOutdated);
}

/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return Processor
*/
private function createProcessor(InputInterface $input, OutputInterface $output)
{
return new Processor(new ConsoleIO($input, $output));
}

/**
* @param Processor $processor
* @param Config $config
*/
abstract protected function doExecute(Processor $processor, Config $config);
}
18 changes: 18 additions & 0 deletions src/Console/Command/ActualizeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace LF\EnvDiff\Console\Command;

use LF\EnvDiff\Config;
use LF\EnvDiff\Processor;

class ActualizeCommand extends AbstractCommand
{
/**
* @param Processor $processor
* @param Config $config
*/
protected function doExecute(Processor $processor, Config $config)
{
$processor->actualizeEnv($config);
}
}
18 changes: 18 additions & 0 deletions src/Console/Command/DiffCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace LF\EnvDiff\Console\Command;

use LF\EnvDiff\Config;
use LF\EnvDiff\Processor;

class DiffCommand extends AbstractCommand
{
/**
* @param Processor $processor
* @param Config $config
*/
protected function doExecute(Processor $processor, Config $config)
{
$processor->showDifference($config);
}
}
2 changes: 1 addition & 1 deletion src/IO/ConsoleIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function write($message)
*/
public function isInteractive()
{
$this->input->isInteractive();
return $this->input->isInteractive();
}

/**
Expand Down
20 changes: 12 additions & 8 deletions tests/Composer/ScriptHandlerTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<?php

namespace LF\EnvHandler\Tests\Composer;
namespace LF\EnvDiff\Tests\Composer;

use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Package\PackageInterface;
use Composer\Script\Event;
use InvalidArgumentException;
use LF\EnvDiff\Composer\ScriptHandler;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject;
Expand Down Expand Up @@ -34,7 +38,7 @@ public function provideInvalidConfiguration()
*/
public function testActualizeEnvInvalidConfiguration(array $extras, $exceptionMessage)
{
$this->expectException('InvalidArgumentException');
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage($exceptionMessage);

$event = $this->createEvent($extras);
Expand All @@ -53,7 +57,7 @@ public function testActualizeEnvInvalidConfiguration(array $extras, $exceptionMe
*/
public function testShowDifferenceInvalidConfiguration(array $extras, $exceptionMessage)
{
$this->expectException('InvalidArgumentException');
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage($exceptionMessage);

$event = $this->createEvent($extras);
Expand Down Expand Up @@ -94,7 +98,7 @@ public function provideValidConfiguration()
*/
public function testActualizeEnvValidConfiguration(array $extras)
{
$io = $this->createMock('Composer\IO\IOInterface');
$io = $this->createMock(IOInterface::class);

$event = $this->createEvent($extras);
$event
Expand All @@ -113,7 +117,7 @@ public function testActualizeEnvValidConfiguration(array $extras)
*/
public function testShowDifferenceValidConfiguration(array $extras)
{
$io = $this->createMock('Composer\IO\IOInterface');
$io = $this->createMock(IOInterface::class);

$event = $this->createEvent($extras);
$event
Expand All @@ -132,17 +136,17 @@ public function testShowDifferenceValidConfiguration(array $extras)
*/
private function createEvent(array $extras = [])
{
$package = $this->createMock('Composer\Package\PackageInterface');
$package = $this->createMock(PackageInterface::class);
$package->expects(self::once())
->method('getExtra')
->willReturn($extras);

$composer = $this->createMock('Composer\Composer');
$composer = $this->createMock(Composer::class);
$composer->expects(self::once())
->method('getPackage')
->willReturn($package);

$event = $this->createMock('Composer\Script\Event');
$event = $this->createMock(Event::class);
$event->expects(self::once())
->method('getComposer')
->willReturn($composer);
Expand Down
2 changes: 1 addition & 1 deletion tests/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace LF\EnvHandler\Tests;
namespace LF\EnvDiff\Tests;

use LF\EnvDiff\Config;
use PHPUnit\Framework\TestCase;
Expand Down
Loading

0 comments on commit 59de8c6

Please sign in to comment.