- Notifications
You must be signed in to change notification settings - Fork3
A .env parser/loader improved for performance.
License
icyleaf/poncho
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A .env parser/loader improved for performance. Poncho Icon by lastspark fromNoun Project.
Add this to your application'sshard.yml
:
dependencies:poncho:github:icyleaf/poncho
Add your application configuration to your.env
file in the root of your project:
MYSQL_HOST=localhostMYSQL_PORT=3306MYSQL_DATABASE=ponchoMYSQL_USER=ponchoMYSQL_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 anHash
with the parsed keys and values.
Poncho parser currently supports the following rules:
- Skipped the empty line and comment(
#
). - Ignore the comment which after (
#
). ENV=development
becomes{"ENV" => "development"}
.- Snakecase and upcase the key:
dbName
becomesDB_NAME
,DB_NAME
becomesDB_NAME
- Support variables in value.
$NAME
or${NAME}
. - Whitespace is removed from both ends of the value.
NAME = foo
becomes{"NAME" => "foo"}
- New lines are expanded if in double quotes.
MULTILINE="new\nline"
becomes{"MULTILINE" => "new\nline"}
- Inner quotes are maintained (such like
Hash
/JSON
).JSON={"foo":"bar"}
becomes{"JSON" => "{\"foo\":\"bar\"}"}
- Empty values become empty strings.
- Single and double quoted values are escaped.
- Overwrite optional (default is non-overwrite).
- Support variables in value.
WELCOME="hello $NAME"
becomes{"WELCOME" => "hello foo"}
By default, Poncho won't overwrite existing environment variables as dotenv assumes the deployment environmenthas more knowledge about configuration than the application does.To overwrite existing environment variables you can usePoncho.parse!(string_or_io)
/Poncho.from_file(file, overwrite: true)
andPoncho.parse(string_or_io, overwrite: true)
.
require"poncho"# Or only import parserrequire"poncho/parser"poncho=Poncho.from_file".env"# orponcho=Poncho.parse("ENV=development\nENV=production")poncho["ENV"]# => "development"# Overwrite value with exists keyponcho=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 loadssingle file supports the following order with environment name (default isdevelopment
):
.env
- The Original®.env.development
- Environment-specific settings..env.local
- Local overrides. This file is loaded for all environments excepttest
..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 environmenthas more knowledge about configuration than the application does.To overwrite existing environment variables you can usePoncho.load!(*files)
orPoncho.load(*files, overwrite: true)
.
require"poncho"# Or only import loaderrequire"poncho/loader"# Load singe file# Searching order: .env.development, .env.local, .env.development.localPoncho.load".env"# Load from pathPoncho.load"config/"# Load production file# Searching order: .env, .env.production, .env.local, .env.production.localPoncho.load".env",env:"production"# Load multiple files and overwrite value with exists key# note: ignore enviroment name.# Searching order: .env, .env.localPoncho.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.
AllContributors are on the wall.
- halite - Crystal HTTP Requests Client with a chainable REST API, built-in sessions and middlewares.
- totem - Load and parse a configuration file or string in JSON, YAML, dotenv formats.
- markd - Yet another markdown parser built for speed, Compliant to CommonMark specification.
- popcorn - Easy and Safe casting from one type to another.
- fast-crystal - 💨 Writing Fast Crystal 😍 -- Collect Common Crystal idioms.
MIT License © icyleaf
About
A .env parser/loader improved for performance.