11 releases
0.3.0 | Mar 5, 2023 |
---|---|
0.2.6 | Mar 5, 2023 |
0.2.5 | Jul 5, 2020 |
0.2.4 | Apr 27, 2020 |
0.1.2 | Apr 26, 2020 |
#431 in Configuration
6KB
ReadEnv
Simple program that reads .env
file and use it to run given command.
Never load environment variables manually or pollute your interpreter profile again.
How it works
- Read current environment variables
- Read
.env
file in current or parent directory - Extend current environment variables with ones read from
.env
file - Spawn
<COMMAND>
with generated environment variables - Replace current process with spawned one
Installation
cargo install readenv
Usage
-
Create
.env
file in with<KEY>=<VALUE>
structure in current or parent directory -
Run the app:
renv <COMMAND>
During the run, the <COMMAND>
acts exactly as executed directly, including environment variables, stdin, stdout, stderr, pipelines support and signals handling.
.env format
Support of .env
file is provided by dotenv library. See its documentation for the format.
Recipes
Django
To run a Django project, a settings file is needed. One approach is to have different settings file per enviroment (e.g. for development and production).
The easiest way to do that is to define environment variable DJANGO_SETTINGS_MODULE
with name of the settings module.
Let's create .env
file (given settings module is local_settings
):
echo 'DJANGO_SETTINGS_MODULE=local_settings' >> .env
Now run Django server with one command:
renv django-admin.py runserver
Virtualenv
Virtualenv is a nice tool to isolate Python dependencies for a project.
To switch to given virtualenv one has to use command . <VENV>/bin/activate
.
Let's create .env
file (given virtualenv directory is .venv
):
echo "VIRTUAL_ENV=$PWD/.venv" >> .env
echo "PATH=$PWD/.venv/bin:\${PATH}" >> .env
Now check Python interpreter:
renv which python
Result should be:
$PWD/.venv/bin/python
Try pip:
renv pip freeze
Result should be the list of dependencies installed in your virtualenv.
Design considerations
12-factor App methodology is great but could be boring. A simple tool that automates work with environment variables would be helpful.
That tool should be a drop-in replacement for any app, so it:
- Must support stdin, stdout and stderr
- Could be used in pipelines
- Must handle signals (e.g.
SIGTERM
orSIGKILL
) identically
Also, it would be nice to:
- Have small application size
- Have low RAM footprint
- Do not depend on shell used
- Be safe
Solution:
- Produce binary program
- Use Rust
- Replace its process with executed command (like
exec
in BASH)
Kudos
Thanks team of dotenv and dotenv library for the most of work ;-)
Dependencies
~72–285KB