Skip to content

Commit 59de8c6

Browse files
committed
Add commands, update readme
1 parent 58d3fbe commit 59de8c6

12 files changed

+335
-28
lines changed

README.md

+120-3
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,133 @@
66
[![Coverage Status][ico-scrutinizer]][link-scrutinizer]
77
[![Quality Score][ico-code-quality]][link-code-quality]
88

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

11-
# Usage
11+
# Installation
1212

13-
composer Scripts composer event
13+
```
14+
composer install tekill/env-diff
15+
```
16+
17+
## Manual running
18+
### Actualize variables
19+
Compare `.env` with `.env.dist` and add missing variables to `.env` file
20+
```
21+
php ./vendor/bin/env-diff actualize
22+
```
23+
24+
Compare `.env` with `.env.example` and add missing variables to `.env` file
25+
```
26+
php ./vendor/bin/env-diff actualize .env.example
27+
```
28+
29+
Compare `.env-target` with `.env.example` and add missing variables to `.env-target` file
30+
```
31+
php ./vendor/bin/env-diff actualize .env.example .env-target
32+
```
33+
34+
If you want to delete outdated values just run command with `-k=false` option
35+
36+
```
37+
php ./vendor/bin/env-diff actualize -k=false
38+
```
39+
40+
### Show differences
41+
Command has same interface, arguments and options
42+
43+
Compare `.env` with `.env.dist` and show differences between them
44+
```
45+
php ./vendor/bin/env-diff diff
46+
```
47+
48+
## Composer script
49+
50+
Add code block in `composer.json`:
1451
```$json
1552
"scripts": {
1653
"post-update-cmd": "Lf\\EnvDiff\\Composer\\ScriptHandler::actualizeEnv"
1754
}
1855
```
56+
57+
The `.env` will then be created or updated by the composer script, to match the structure of the dist
58+
file `.env.dist` by asking you the missing variables.
59+
60+
By default, the dist file is assumed to be in the same place than the target `.env`
61+
file, suffixed by `.dist`. This can be changed in the configuration:
62+
63+
```json
64+
{
65+
"extra": {
66+
"lf-env-diff": [
67+
{
68+
"dist": "path/to/env.dist",
69+
"target": "path/to/.env"
70+
}
71+
]
72+
}
73+
}
74+
```
75+
76+
The script handler will ask you interactively for variables which are missing
77+
in the target env file, using the value of the dist file as default value.
78+
If composer is run in a non-interactive mode `--no-interaction`, the values of the dist file
79+
will be used for missing variables.
80+
81+
**Warning:** This handler will overwrite any comments or spaces into your target `.env` file so handle with care.
82+
83+
### Managing multiple ignored files
84+
85+
The handler can manage multiple ignored files. To use this feature, the `lf-env-diff` extra should contain a
86+
JSON array with multiple configurations inside it instead of a configuration object:
87+
88+
```json
89+
{
90+
"extra": {
91+
"lf-env-diff": [
92+
{
93+
"dist": "path/to/.env.dist",
94+
"target": "path/to/.env"
95+
},
96+
{
97+
"dist": "path/to/.env.dist",
98+
"target": "path/to/.env-test",
99+
"keep-outdated": false
100+
}
101+
]
102+
}
103+
}
104+
```
105+
106+
### Show difference
107+
108+
Add code block in `composer.json`:
109+
```$json
110+
"scripts": {
111+
"post-update-cmd": "Lf\\EnvDiff\\Composer\\ScriptHandler::showDifference"
112+
}
113+
```
114+
115+
This handler has same behavior as described before.
116+
117+
## Git hooks
118+
119+
You can use Git hook that gets triggered after any 'git pull' whenever one of the files specified has changed.
120+
Useful to update any web application dependency or sync configuration.
121+
122+
Create `post-merge` hook in `.git/hooks` directory of your project:
123+
```
124+
#/usr/bin/env bash
125+
126+
changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"
127+
128+
check_run() {
129+
echo "$changed_files" | grep -E --quiet "$1" && eval "$2"
130+
}
131+
132+
# Aclualize env files if the `env.dist` file gets changed
133+
check_run env.dist "php ./vendor/bin/env-diff aclualize"
134+
```
135+
19136
[ico-version]: https://img.shields.io/packagist/v/Tekill/env-diff.svg?style=flat-square
20137
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
21138
[ico-travis]: https://img.shields.io/travis/Tekill/env-diff/master.svg?style=flat-square

env-diff

+5-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/env php
22
<?php
33

4+
use LF\EnvDiff\Console\Application;
5+
46
$autoloadPaths = [
57
__DIR__ . '/../../autoload.php',
68
__DIR__ . '/../vendor/autoload.php',
@@ -9,21 +11,11 @@ $autoloadPaths = [
911

1012
foreach ($autoloadPaths as $path) {
1113
if (file_exists($path)) {
12-
require_once $path;
13-
14-
$config = [];
1514

16-
$processor = new LF\EnvDiff\Processor(
17-
new \LF\EnvDiff\IO\ConsoleIO(
18-
new \Symfony\Component\Console\Input\ArgvInput(),
19-
new \Symfony\Component\Console\Output\ConsoleOutput()
20-
)
21-
);
22-
23-
$processor->actualizeEnv(LF\EnvDiff\Config::createFormArray($config));
24-
$processor->showDifference(LF\EnvDiff\Config::createFormArray($config));
15+
require_once $path;
2516

26-
exit(0);
17+
$application = new Application();
18+
$application->run();
2719
}
2820
}
2921

src/Console/Application.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace LF\EnvDiff\Console;
4+
5+
use LF\EnvDiff\Console\Command\ActualizeCommand;
6+
use LF\EnvDiff\Console\Command\DiffCommand;
7+
use Symfony\Component\Console\Application as BaseApplication;
8+
9+
class Application extends BaseApplication
10+
{
11+
/**
12+
* {@inheritdoc}
13+
*/
14+
public function __construct()
15+
{
16+
parent::__construct('Env diff', '1.0.0');
17+
18+
$this->setAutoExit(true);
19+
$this->add(new DiffCommand('diff'));
20+
$this->add(new ActualizeCommand('actualize'));
21+
}
22+
}
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace LF\EnvDiff\Console\Command;
4+
5+
use LF\EnvDiff\Config;
6+
use LF\EnvDiff\IO\ConsoleIO;
7+
use LF\EnvDiff\Processor;
8+
use Symfony\Component\Console\Command\Command;
9+
use Symfony\Component\Console\Input\InputInterface;
10+
use Symfony\Component\Console\Input\InputOption;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
13+
abstract class AbstractCommand extends Command
14+
{
15+
/**
16+
* {@inheritdoc}
17+
*/
18+
protected function configure()
19+
{
20+
$this
21+
->addArgument('dist', InputOption::VALUE_REQUIRED, 'From file', Config::DEFAULT_DIST)
22+
->addArgument('target', InputOption::VALUE_REQUIRED, 'To file', Config::DEFAULT_TARGET)
23+
->addOption('keep-outdated', 'k', InputOption::VALUE_OPTIONAL, 'Keep old env variables', true);
24+
}
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
protected function execute(InputInterface $input, OutputInterface $output)
30+
{
31+
$config = $this->createConfig($input);
32+
$processor = $this->createProcessor($input, $output);
33+
34+
$this->doExecute($processor, $config);
35+
36+
return 0;
37+
}
38+
39+
/**
40+
* @param InputInterface $input
41+
*
42+
* @return Config
43+
*/
44+
private function createConfig(InputInterface $input)
45+
{
46+
$dist = $input->getArgument('dist');
47+
$target = $input->getArgument('target');
48+
$keepOutdated = $input->getOption('keep-outdated');
49+
50+
return new Config($dist, $target, $keepOutdated);
51+
}
52+
53+
/**
54+
* @param InputInterface $input
55+
* @param OutputInterface $output
56+
*
57+
* @return Processor
58+
*/
59+
private function createProcessor(InputInterface $input, OutputInterface $output)
60+
{
61+
return new Processor(new ConsoleIO($input, $output));
62+
}
63+
64+
/**
65+
* @param Processor $processor
66+
* @param Config $config
67+
*/
68+
abstract protected function doExecute(Processor $processor, Config $config);
69+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace LF\EnvDiff\Console\Command;
4+
5+
use LF\EnvDiff\Config;
6+
use LF\EnvDiff\Processor;
7+
8+
class ActualizeCommand extends AbstractCommand
9+
{
10+
/**
11+
* @param Processor $processor
12+
* @param Config $config
13+
*/
14+
protected function doExecute(Processor $processor, Config $config)
15+
{
16+
$processor->actualizeEnv($config);
17+
}
18+
}

src/Console/Command/DiffCommand.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace LF\EnvDiff\Console\Command;
4+
5+
use LF\EnvDiff\Config;
6+
use LF\EnvDiff\Processor;
7+
8+
class DiffCommand extends AbstractCommand
9+
{
10+
/**
11+
* @param Processor $processor
12+
* @param Config $config
13+
*/
14+
protected function doExecute(Processor $processor, Config $config)
15+
{
16+
$processor->showDifference($config);
17+
}
18+
}

src/IO/ConsoleIO.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function write($message)
4747
*/
4848
public function isInteractive()
4949
{
50-
$this->input->isInteractive();
50+
return $this->input->isInteractive();
5151
}
5252

5353
/**

tests/Composer/ScriptHandlerTest.php

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
<?php
22

3-
namespace LF\EnvHandler\Tests\Composer;
3+
namespace LF\EnvDiff\Tests\Composer;
44

5+
use Composer\Composer;
6+
use Composer\IO\IOInterface;
7+
use Composer\Package\PackageInterface;
58
use Composer\Script\Event;
9+
use InvalidArgumentException;
610
use LF\EnvDiff\Composer\ScriptHandler;
711
use PHPUnit\Framework\TestCase;
812
use PHPUnit_Framework_MockObject_MockObject;
@@ -34,7 +38,7 @@ public function provideInvalidConfiguration()
3438
*/
3539
public function testActualizeEnvInvalidConfiguration(array $extras, $exceptionMessage)
3640
{
37-
$this->expectException('InvalidArgumentException');
41+
$this->expectException(InvalidArgumentException::class);
3842
$this->expectExceptionMessage($exceptionMessage);
3943

4044
$event = $this->createEvent($extras);
@@ -53,7 +57,7 @@ public function testActualizeEnvInvalidConfiguration(array $extras, $exceptionMe
5357
*/
5458
public function testShowDifferenceInvalidConfiguration(array $extras, $exceptionMessage)
5559
{
56-
$this->expectException('InvalidArgumentException');
60+
$this->expectException(InvalidArgumentException::class);
5761
$this->expectExceptionMessage($exceptionMessage);
5862

5963
$event = $this->createEvent($extras);
@@ -94,7 +98,7 @@ public function provideValidConfiguration()
9498
*/
9599
public function testActualizeEnvValidConfiguration(array $extras)
96100
{
97-
$io = $this->createMock('Composer\IO\IOInterface');
101+
$io = $this->createMock(IOInterface::class);
98102

99103
$event = $this->createEvent($extras);
100104
$event
@@ -113,7 +117,7 @@ public function testActualizeEnvValidConfiguration(array $extras)
113117
*/
114118
public function testShowDifferenceValidConfiguration(array $extras)
115119
{
116-
$io = $this->createMock('Composer\IO\IOInterface');
120+
$io = $this->createMock(IOInterface::class);
117121

118122
$event = $this->createEvent($extras);
119123
$event
@@ -132,17 +136,17 @@ public function testShowDifferenceValidConfiguration(array $extras)
132136
*/
133137
private function createEvent(array $extras = [])
134138
{
135-
$package = $this->createMock('Composer\Package\PackageInterface');
139+
$package = $this->createMock(PackageInterface::class);
136140
$package->expects(self::once())
137141
->method('getExtra')
138142
->willReturn($extras);
139143

140-
$composer = $this->createMock('Composer\Composer');
144+
$composer = $this->createMock(Composer::class);
141145
$composer->expects(self::once())
142146
->method('getPackage')
143147
->willReturn($package);
144148

145-
$event = $this->createMock('Composer\Script\Event');
149+
$event = $this->createMock(Event::class);
146150
$event->expects(self::once())
147151
->method('getComposer')
148152
->willReturn($composer);

tests/ConfigTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace LF\EnvHandler\Tests;
3+
namespace LF\EnvDiff\Tests;
44

55
use LF\EnvDiff\Config;
66
use PHPUnit\Framework\TestCase;

0 commit comments

Comments
 (0)