a schema for your application's environment variables
MIT License
a schema describing and validating the environment variables needed by an application.
Why?
Add this line to your application's Gemfile:
gem "envy", github: "bkeepers/envy"
And then execute:
$ bundle
Create an Envfile
in the root of your application.
desc "Support email shown on the contact page."
string :support_email, default: "[email protected]"
desc "The max duration (in seconds) a request can take before it times out."
integer :request_timeout, default: 10
desc "Boolean attribute specifying whether the site is in SSL mode."
boolean :ssl, default: Rails.env.production?
Envy reads the schema when the application bootstraps and:
$ENV
with reader methods for each variable that fetch values from ENV
:default
valuesYou can access the environment through $ENV
.
# For example, in config/application.rb
config.action_mailer.default_options = {
from: $ENV.support_email
}
config.force_ssl = $ENV.ssl?
Variables can be set in the environment at runtime using the variable name in SCREAMING_SNAKE_CASE
:
$ REQUEST_TIMEOUT=30 rails server
Setup happens automatically in a Rails app. To use Envy outside of Rails, run:
require 'envy'
$ENV = Envy.environment.setup('Envfile')
Envy supports type casting of variable values.
string
string :support_email, default: "[email protected]"
# >> $ENV.support_email
# => "[email protected]"
boolean
boolean :ssl, default: true
# >> $ENV.ssl?
# => true
integer
integer :request_timeout, default: 10
# >> $ENV.request_timeout
# => 10
decimal
decimal :sample_rate, default: 0.1
# >> $ENV.sample_rate
# => 0.1
uri
uri :app_host, default: "https://example.com
# >> $ENV.app_host
# => #<Addressable::URI:0x1040 URI:https://example.com>
Declare your own types by extending Envy::Type::Variable
and implementing #cast
.
class MyType < Envy::Type::Variable
def cast(value)
# return cast value here
end
end
type :my_type, MyType
desc "My custom variable type"
my_type :var_name
:required
All defined variables without a default value are required. When the Envfile
is loaded, it will raise an exception if the required keys are not set.
Missing environment variables: ENCRYPTION_KEY
The :required
option can be set to false
for optional values.
string :encryption_key, required: false
:default
The :default
can be used to set a default value when the variable is not defined in ENV
.
integer :request_timeout, default: 10
If logic is required to set a default, a lambda can be passed and it will be called if a variable is not set in ENV
.
boolean :check_for_internet_connection?, default: false
boolean :online, default: -> { check_for_internet_connection? ? Site.google_reachable? : true }
end
Pass a block to a variable definition and it will be called to transform the value.
string :admin_ids do |value|
value.split(",").map(&:to_i)
end
# >> ENV["ADMIN_IDS"] = "1,2,3"
# >> $ENVY.admin_ids
# => [1, 2, 3]
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)