poncho
Poncho
A .env parser/loader improved for performance. Poncho Icon by lastspark from Noun Project.
Installation
Add this to your application's shard.yml
:
dependencies:
poncho:
github: icyleaf/poncho
Usage
Add your application configuration to your .env
file in the root of your project:
MYSQL_HOST=localhost
MYSQL_PORT=3306
MYSQL_DATABASE=poncho
DMYSQL_USER=poncho
MYSQL_PASSWORD=74e10b72-33b1-434b-a476-cfee0faa7d75
Now you can parse or load it.
Parse
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.
Rules
Poncho parser currently supports the following rules:
- Skipped the empty line and comment(
#
). - Ignore the comment which after (
#
). NAME=foo
becomes{"NAME" => "foo"}
.- Empty values become empty strings.
- Whirespace is removed from right ends of the value.
- Single and Double quoted values are escaped.
- New lines are expanded if in double quotes.
- Inner quotes are maintained (such like json).
- Overwrite optional (default is non-overwrite).
- Only accpets string type value.
Overrides
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)
.
Examples
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"
Load
Poncho loads the environment file is easy to use. It accepts both single file (or path) and multiple files.
Orders
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 excepttest
..env.development.local
- Local overrides of environment-specific settings.
NO effect with multiple files.
Overrides
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)
.
Examples
# 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, ignore enviroment name.
Poncho.load ".env", ".env.local", env: "test"
# Searching order: .env, .env.local
Donate
Poncho is a open source, collaboratively funded project. If you run a business and are using Poncho in a revenue-generating product, it would make business sense to sponsor Poncho development. Individual users are also welcome to make a one time donation if Totem has helped you in your work or personal projects.
You can donate via Paypal.
How to Contribute
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.
You may also like
- halite - Crystal HTTP Requests Client with a chainable REST API, built-in sessions and loggers.
- 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.
License
MIT License © icyleaf