README ¶
envset
envset
run commands in an environment defined using a ini configuration file.
Environment level configuration
Application configuration is (usually) specific to an environment and will change between different build environments- e.g. app secrets for a staging environment are different than your production secrets.
The 12 factor app guidelines suggest you store your application's configuration in the environment.
Environment variables enable us to manage application configuration outside of our application code.
Application configuration usually are small and sensitive data such as API keys or tokens, database credentials, etc. However not all environment configuration have to be secrets, there might be build distribution specific values such as the application's base URL to build OAuth callbacks, a dependent service endpoint or anything that changes between development and production environments.
envset
helps you manage environment variables for multiple build distributions.
Is as simple as calling:
envset development -- node server.js
This will load the variables defined in the [development]
section of a local .envset
in the shell environment and execute the command after the --
, in this instance node server.js
.
See the examples section for more details.
Similar Tools
Inspired by daemontools' tool envdir and tools such as dotenv.
- Distributed as a single binary
- No dependencies in your codebase
- e.g.
dotenv-rails
anddotenv
1 for Node.js require you to use a library
- e.g.
- Support multiple environments in a single file
- Generates an example file with your current env vars to keep documentation updated.
- Interpolation of variable using POSIX variable expansion.
- Command expansion
- (required) Define required variables and exit with error if not set
- (isolated) By default the shell environment is not loaded in the context
Instead of having an .env
file per environment you can have one single .envset
file with one section per environment.
1: You an actually require the library outside of your project with the node -r
flag.
Examples
Executing a command
An .envset file could look like this:
[production]
NODE_AWS_SECRET_ACCESS_KEY=FS40N0QY22p2bpciAh7wuAeHjJURgXIBQ2cGodpJD3FRjw2EyYGjyXpi73Ld8zWO
NODE_AWS_ACCESS_KEY_ID=LSLhv74Q1vH8auQKUt5pFwnix0FUl0Ml
NODE_HONEYBADGER_KEY=LCgZgqsxKfhO
NODE_POSTGRES_ENDPOINT=50.23.54.25
NODE_POSTGRES_DATABASE=myproject
NODE_POSTGRES_PSWD=Pa$sW03d
NODE_POSTGRES_USER=myproject
[development]
NODE_AWS_SECRET_ACCESS_KEY=HN5ITok3lDaA1ASHxtEpy1U9XCjZwThmfgoIYZk8bkOqc5yk6sT7AWd3ooNeRFV9
NODE_AWS_ACCESS_KEY_ID=m35W4PGGVZfj1gOxYvztFxBD5F2605B3
NODE_HONEYBADGER_KEY=f3MNPUhZoki6
NODE_POSTGRES_ENDPOINT=localhost
NODE_POSTGRES_DATABASE=postgres
NODE_POSTGRES_PSWD=postgres
NODE_POSTGRES_USER=postgres
To use it, simply prefix the call to your program with envset
and the name of the environment section. The node app.js
will be running with the environment variables specified in the development section of the .envset file.
$ envset development -- node app.js
Generating an example template
If we run the envset template
command with the previous .envset file we generate a envset.example file:
[development]
NODE_AWS_SECRET_ACCESS_KEY={{NODE_AWS_SECRET_ACCESS_KEY}}
NODE_AWS_ACCESS_KEY_ID={{NODE_AWS_ACCESS_KEY_ID}}
NODE_HONEYBADGER_KEY={{NODE_HONEYBADGER_KEY}}
NODE_POSTGRES_ENDPOINT={{NODE_POSTGRES_ENDPOINT}}
NODE_POSTGRES_DATABASE={{NODE_POSTGRES_DATABASE}}
NODE_POSTGRES_PSWD={{NODE_POSTGRES_PSWD}}
NODE_POSTGRES_USER={{NODE_POSTGRES_USER}}
[production]
NODE_AWS_SECRET_ACCESS_KEY={{NODE_AWS_SECRET_ACCESS_KEY}}
NODE_AWS_ACCESS_KEY_ID={{NODE_AWS_ACCESS_KEY_ID}}
NODE_HONEYBADGER_KEY={{NODE_HONEYBADGER_KEY}}
NODE_POSTGRES_ENDPOINT={{NODE_POSTGRES_ENDPOINT}}
NODE_POSTGRES_DATABASE={{NODE_POSTGRES_DATABASE}}
NODE_POSTGRES_PSWD={{NODE_POSTGRES_PSWD}}
NODE_POSTGRES_USER={{NODE_POSTGRES_USER}}
Support for .env files
You can load other environment files like .env
files:
envset --env-file=.env -- node index.js
Installation
Add tap to brew:
$ brew tap goliatone/homebrew-tap
Install envset
:
$ brew install envset
Documentation
envset
will look for a file defining different environments and make them available as commands.
[development]
APP_BASE_URL=https://localhost:3003
[production]
APP_BASE_URL=https://envset.sh
Commands
The following is a list of the available commands:
- metadata
- template
Variable Expansion
envset
can interpolate variables using POSIX variable expansion in both the loaded environment file and the running command arguments.
[development]
CLIENT_NAME=$(whoami -f)
CLIENT_ID=${CLIENT_NAME}.devices.local
$ envset development -- node cli.js --user '${USER}'
Commands
If you type envset
without arguments it will display help and a list of supported environment names.
.envset file
.envsetrc
You can create an .envsetrc
file with configuration options for envset
.
The default .envsetrc
looks like this:
expand=true
isolated=true
filename=.envset
export_environment=APP_ENV
[metadata]
dir=.meta
file=data.json
[template]
path=.
file=envset.example
[environments]
name=test
name=staging
name=production
name=development
Configuration
Follows rc
standards.
Configuration Syntax
The loaded files need to be valid ini
syntax.
License
Copyright (c) 2015-2021 goliatone
Licensed under the MIT license.