A .env parser/loader improved for performance.
A .env parser/loader improved for performance. Poncho Icon by lastspark from Noun Project.
Add this to your application's shard.yml
:
dependencies:
poncho:
github: icyleaf/poncho
Add your application configuration to your .env
file in the root of your project:
MYSQL_HOST=localhost
MYSQL_PORT=3306
MYSQL_DATABASE=poncho
MYSQL_USER=poncho
MYSQL_PASSWORD=74e10b72-33b1-434b-a476-cfee0faa7d75
Now you can parse or load it.
Poncho parses the contents of your file containing environment variables is available to use.
It accepts a String or IO and will return an Hash
with the parsed keys and values.
Poncho parser currently supports the following rules:
#
).#
).ENV=development
becomes {"ENV" => "development"}
.dbName
becomes DB_NAME
, DB_NAME
becomes DB_NAME
$NAME
or ${NAME}
.NAME = foo
becomes{"NAME" => "foo"}
MULTILINE="new\nline"
becomes {"MULTILINE" => "new\nline"}
Hash
/JSON
). JSON={"foo":"bar"}
becomes {"JSON" => "{\"foo\":\"bar\"}"}
WELCOME="hello $NAME"
becomes {"WELCOME" => "hello foo"}
By default, Poncho won't overwrite existing environment variables as dotenv assumes the deployment environment
has more knowledge about configuration than the application does.
To overwrite existing environment variables you can use Poncho.parse!(string_or_io)
/
Poncho.from_file(file, overwrite: true)
and Poncho.parse(string_or_io, overwrite: true)
.
require "poncho"
# Or only import parser
require "poncho/parser"
poncho = Poncho.from_file ".env"
# or
poncho = Poncho.parse("ENV=development\nENV=production")
poncho["ENV"] # => "development"
# Overwrite value with exists key
poncho = Poncho.parse!("ENV=development\nENV=production")
poncho["ENV"] # => "production"
Poncho loads the environment file is easy to use, based on parser above.
It accepts both single file (or path) and multiple files.
Poncho loads single file supports the following order with environment name (default is development
):
.env
- The Original®.env.development
- Environment-specific settings..env.local
- Local overrides. This file is loaded for all environments except test
..env.development.local
- Local overrides of environment-specific settings.NO effect with multiple files, it only loads the given files.
By default, Poncho won't overwrite existing environment variables as dotenv assumes the deployment environment
has more knowledge about configuration than the application does.
To overwrite existing environment variables you can use Poncho.load!(*files)
or Poncho.load(*files, overwrite: true)
.
require "poncho"
# Or only import loader
require "poncho/loader"
# Load singe file
# Searching order: .env.development, .env.local, .env.development.local
Poncho.load ".env"
# Load from path
Poncho.load "config/"
# Load production file
# Searching order: .env, .env.production, .env.local, .env.production.local
Poncho.load ".env", env: "production"
# Load multiple files and overwrite value with exists key
# note: ignore enviroment name.
# Searching order: .env, .env.local
Poncho.load! ".env", ".env.local", env: "test"
Totem is here to help with that. Poncho was built-in to Totem to better with configuration. Configuration file formats is always the problem, you want to focus on building awesome things.
Your contributions are always welcome! Please submit a pull request or create an issue to add a new question, bug or feature to the list.
All Contributors are on the wall.
MIT License © icyleaf