Skip to content

Commit

Permalink
Merge pull request #2 from MarcinOrlowski/dev
Browse files Browse the repository at this point in the history
Release 1.0.5
  • Loading branch information
MarcinOrlowski authored Jan 27, 2021
2 parents 8ea13e6 + 85a575e commit d9a2672
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 28 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
v1.0.5 (2021-01-01)
===================
* Updated documentation
* Code cleanup

v1.0.4 (2018-02-25)
===================
* Removed `minimum-stability` from `composer.json`
Expand Down
45 changes: 23 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,39 @@
[![Latest Unstable Version](https://poser.pugx.org/marcin-orlowski/process-dotenv/v/unstable)](https://packagist.org/packages/marcin-orlowski/process-dotenv)
[![License](https://poser.pugx.org/marcin-orlowski/process-dotenv/license)](https://packagist.org/packages/marcin-orlowski/process-dotenv)

DotEnv file (`.env`) are often used as runtime configuration files (i.e. [Laravel](https://laravel.com/)
based PHP projects) and are not stored in your repository, so if you use Continuous Integration (CI)
tools like TeamCity or Travis-CI, you need to create that `.env` file before tests can be started.
`Process-DotEnv` is a tool wass created to help you with this task.
`.env` (AKA DotEnv) files are often used to store project configuration (i.e. for [Laravel](https://laravel.com/)
based PHP projects). As they usually contain sensitive information as API keys or DB credentials, `.env` files should
never be versioned. This also means that if you need to use/run your project in automated pipeline, i.e. with
Continuous Integration (CI) tools like TeamCity or Travis-CI, you need to create proper `.env` file before using the
code. As `.env` file usually contains all the project configuration, there will be much more fields than said i.e.
API key. Additionally, as project develops, new entries can be added and existing entries altered or tweaked.
It's a developers' common practice to create `.env.dist` file, fill as much as possible (ommiting sensitive information)
and put it into VCS.

So if we treat said `.env.dist` as **template file**, then with the right tool in hand we'd be able to
create corresponding `.env` file easily. And this is where `process-dotenv` steps in. The goal of this
small tool is pretty simple - generate `.env` file based on the template `.env.dist`, filling/replacing
specified template keys with provided values (taken either from env vars, for supplied as invocation
arguments).

**NOTE:** Whenever I say `.env` or `.env.dist` I only mean **file format**, not file name. Your
file names can be anything you like as long its content follows dot-env file format!

The main assumption is that you usually have file name `.env.dist` in your repository so people
using your code can easily figure out how to create production ready `.env` file. So if we'd
tread your `.env.dist` as **template**, then with the right tool you'd be able to easily create
production ready `.env` file. and this is where `process-dotenv` steps in. The goal (and code :)
is pretty simple - generate `.env` file based on the template `.env.dist` but with all neccessary
changes applied. So ths tool reads your `.env.dist` file and spits it out **replacing** all values
that you wanted it to change either by setting enviromental variables or as command line arguments.

**NOTE:** To avoid accidental overwrites `process-dotenv` does not create any files but echoes the final
content, so to create physical `.env` file for your code you need to redirect output to file with regular
redirection: ` ... > .env`.
**NOTE:** To avoid accidental overwrites `process-dotenv` outputs processed content to standard output. To to
create physical `.env` file need to redirect stdout to a file. Please see examples for more details.

## Env variable subsitution ##

Let's assume our `.env.dist` file looks like this:
**NOTE:** all samples mimics shell session, so ommit `$` line for use in scripts.:

Let's assume our `.env.dist` template file looks like this:

KEY=val
BAR=zen
FOO=

Now, knowing your app requires `KEY` to be valid i.e. API key for tests to pass we can have it replaced with
`process-dotenv` (sample mimics shell session, for scripts get rid of `$`):
Now, knowing your app requires `KEY` to be valid API key for tests to pass we can have it replaced with
`process-dotenv`:

$ KEY=barbar
$ vendor/bin/process-dotenv .env.dist > .env
Expand All @@ -58,7 +61,6 @@ achieve the same results:
**IMPORTANT:** first argument always refers to source dot-env file, followed by (optional) `KEY=VAL` pairs.
You can pass as many pairs as you need and file names can be whatever you like.


## Combined substitution ##

Both substitution methods can be used together. When key is provided as argument and
Expand All @@ -76,7 +78,7 @@ would produce:

## Requirements ##

* PHP 5+ (cli)
* PHP 5+ (CLI)

## Installation ##

Expand Down Expand Up @@ -115,6 +117,5 @@ it will simply use its value. That's why you must override it via command line p

## License ##

* Copyright © 2016-2018 by Marcin Orlowski
* Copyright © 2016-2021 by Marcin Orlowski
* Process Dotenv tool is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)

10 changes: 5 additions & 5 deletions bin/process-dotenv
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Little tool to help build DotEnv files from templates
*
* @author Marcin Orlowski <mail (#) marcinorlowski (.) com>
* @copyright 2016-2018 Marcin Orlowski
* @copyright 2016-2021 Marcin Orlowski
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link https://github.com/MarcinOrlowski/process-dotenv
*/
Expand All @@ -19,14 +19,14 @@
*/
function abort($msg, $code = 1)
{
echo $msg;
echo "{$msg}\n";
exit($code);
}

/******************************************************/

if ($_SERVER['argc'] < 2) {
abort(sprintf("Usage: %s .env.dist [KEY=VAL KEY2=VAL2...]\n", $_SERVER['argv'][0]));
abort(sprintf("Usage: %s .env.dist [KEY=VAL KEY2=VAL2...]", $_SERVER['argv'][0]));
}

$file_in = $_SERVER['argv'][1];
Expand Down Expand Up @@ -91,10 +91,10 @@ if (file_exists($file_in)) {
// output content of processed .env
echo implode("\n", $out);
} else {
abort("Failed processing '{$file_in}' source file\n");
abort("Failed processing '{$file_in}' source file");
}
} else {
abort("Source file '{$file_in}' not found\n");
abort("Source file '{$file_in}' not found");
}

exit(0);
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "marcin-orlowski/process-dotenv",
"version": "1.0.4",
"version": "1.0.5",
"description": "Little tool to help build DotEnv (.env) files from templates",
"license": "MIT",
"keywords": [
Expand Down

0 comments on commit d9a2672

Please sign in to comment.