All Projects → fnando → superconfig

fnando / superconfig

Licence: MIT license
Access environment variables. Also includes presence validation, type coercion and default values.

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to superconfig

sitri
Sitri - powerful settings & configs for python
Stars: ✭ 20 (-39.39%)
Mutual labels:  config, configuration, environment-variables, configuration-management
ini
📝 Go INI config management. support multi file load, data override merge. parse ENV variable, parse variable reference. Dotenv file parse and loader. INI配置读取管理,支持多文件加载,数据覆盖合并, 解析ENV变量, 解析变量引用。DotEnv 解析加载
Stars: ✭ 72 (+118.18%)
Mutual labels:  config, dotenv, environment-variables, configuration-management
Dynaconf
Configuration Management for Python ⚙
Stars: ✭ 2,082 (+6209.09%)
Mutual labels:  config, configuration, environment-variables, configuration-management
gconfigs
gConfigs - Config and Secret parser
Stars: ✭ 42 (+27.27%)
Mutual labels:  dotenv, configuration, environment-variables, 12-factor
climatecontrol
Python library for loading settings and config data from files and environment variables
Stars: ✭ 20 (-39.39%)
Mutual labels:  config, configuration, environment-variables, configuration-management
nest-typed-config
Intuitive, type-safe configuration module for Nest framework ✨
Stars: ✭ 47 (+42.42%)
Mutual labels:  config, dotenv, configuration, configuration-management
parse it
A python library for parsing multiple types of config files, envvars & command line arguments that takes the headache out of setting app configurations.
Stars: ✭ 86 (+160.61%)
Mutual labels:  config, configuration, environment-variables, configuration-management
Node Convict
Featureful configuration management library for Node.js
Stars: ✭ 1,855 (+5521.21%)
Mutual labels:  config, configuration, environment-variables, configuration-management
Config Rs
⚙️ Layered configuration system for Rust applications (with strong support for 12-factor applications).
Stars: ✭ 915 (+2672.73%)
Mutual labels:  config, configuration, configuration-management
Configuration
Library for setting values to structs' fields from env, flags, files or default tag
Stars: ✭ 37 (+12.12%)
Mutual labels:  config, configuration, configuration-management
Node No Config
Config and resource loader
Stars: ✭ 45 (+36.36%)
Mutual labels:  config, configuration, configuration-management
Strictyaml
Type-safe YAML parser and validator.
Stars: ✭ 836 (+2433.33%)
Mutual labels:  config, configuration, configuration-management
Config
A lightweight yet powerful config package for Go projects
Stars: ✭ 126 (+281.82%)
Mutual labels:  config, configuration, environment-variables
Ini Parser
Read/Write an INI file the easy way!
Stars: ✭ 643 (+1848.48%)
Mutual labels:  config, configuration, configuration-management
Koanf
Light weight, extensible configuration management library for Go. Built in support for JSON, TOML, YAML, env, command line, file, S3 etc. Alternative to viper.
Stars: ✭ 450 (+1263.64%)
Mutual labels:  config, configuration, configuration-management
Appconfiguration
Questions, feedback and samples for Azure App Configuration service
Stars: ✭ 116 (+251.52%)
Mutual labels:  config, configuration, configuration-management
Environ Config
Python Application Configuration With Environment Variables
Stars: ✭ 210 (+536.36%)
Mutual labels:  config, configuration, environment-variables
Simple Settings
A simple way to manage your project settings.
Stars: ✭ 165 (+400%)
Mutual labels:  config, configuration, configuration-management
Config
Easiest way to add multi-environment yaml settings to Rails, Sinatra, Pandrino and other Ruby projects.
Stars: ✭ 1,821 (+5418.18%)
Mutual labels:  config, configuration, configuration-management
libconfini
Yet another INI parser
Stars: ✭ 106 (+221.21%)
Mutual labels:  config, configuration, configuration-management

SuperConfig: Access environment variables. Also includes presence validation, type coercion and default values.

Github Actions Code Climate Gem Gem

Installation

Add this line to your application's Gemfile:

gem "superconfig"

And then execute:

$ bundle

Or install it yourself as:

$ gem install superconfig

Usage

Config = SuperConfig.new do
  mandatory :database_url, string
  optional  :timeout, int, 10
  optional  :force_ssl, bool, false
  optional  :rails_env, "development", string, aliases: %w[env]
end

Config.database_url
Config.timeout
Config.force_ssl?

You can specify the description for both mandatory and optional methods; this will be used in exceptions.

Config = SuperConfig.new do
  mandatory :missing_var, string, description: "this is important"
end

#=> SuperConfig::MissingEnvironmentVariable: MISSING_VAR (this is important) is not defined

If you're going to use SuperConfig as your main configuration object, you can also set arbitrary properties, like the following:

Config = SuperConfig.new do
  optional :redis_url, string, "redis://127.0.0.1"
  property :redis, -> { Redis.new } # pass an object that responds to #call
  property(:now) { Time.now }       # or pass a block.
end

Config.redis.set("key", "value")
Config.redis.get("key")
#=> "value"

Values are cached by default. If you want to dynamically generate new values, set cache: false.

Config = SuperConfig.new do
  property(:uuid, cache: false) { SecureRandom.uuid }
end

You may want to start a debug session without raising exceptions for missing variables. In this case, just pass raise_exception: false instead to log error messages to $stderr. This is especially great with Rails' credentials command (rails credentials:edit) when already defined the configuration.

Config = SuperConfig.new(raise_exception: false) do
  mandatory :database_url, string, description: "the leader database"
end

#=> [SUPERCONFIG] DATABASE_URL (the leader database) is not defined

I'd like to centralize access to my credentials; there's a handy mechanism for doing that with SuperConfig:

Config = SuperConfig.new do
  credential :api_secret_key
  credential :slack_oauth_credentials do |creds|
    SlackCredentials.new(creds)
  end
end

Config.api_secret_key
Config.slack_oauth_credentials
#=> The value stored under `Rails.application.credentials[:api_secret_key]`

Types

You can coerce values to the following types:

  • string: Is the default. E.g. optional :name, string.
  • int: E.g. optional :timeout, int.
  • float: E.g. optional :wait, float.
  • bigdecimal: E.g. optional :fee, bigdecimal.
  • bool: E.g. optional :force_ssl, bool. Any of yes, true or 1 is considered as true. Any other value will be coerced to false.
  • symbol: E.g. optional :app_name, symbol.
  • array: E.g. optional :chars, array or optional :numbers, array(int). The environment variable must be something like a,b,c.
  • json: E.g. mandatory :keyring, json. The environment variable must be parseable by JSON.parse(content).

Report

Sometimes it gets hard to understand what's set and what's not. In this case, you can get a report by calling SuperConfig::Base#report. If you're using Rails, you can create a rake task like this:

# frozen_string_literal: true

desc "Show SuperConfig report"
task superconfig: [:environment] do
  puts YourAppNamespace::Config.report
end

Then, change your configuration so it doesn't raise an exception.

# frozen_string_literal: true
# file: config/config.rb

module YourAppNamespace
  Config = SuperConfig.new(raise_exception: false) do
    mandatory :database_url, string
    optional :app_name, string
    optional :wait, string
    optional :force_ssl, bool, true
  end
end

Finally, run the following command:

$ rails superconfig
❌ DATABASE_URL is not set (mandatory)
✅ APP_NAME is set (optional)
⚠️ WAIT is not set (optional)
✅ FORCE_SSL is not set, but has default value (optional)

Dotenv integration

If you're using dotenv, you can simply require superconfig/dotenv. This will load environment variables from .env.local.%{environment}, .env.local, .env.%{environment} and .env files, respectively. You must add dotenv to your Gemfile.

require "superconfig/dotenv"

Configuring Rails

If you want to use SuperConfig even on your Rails configuration files like database.yml and secrets.yml, you must load it from config/boot.rb, right after setting up Bundler.

ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", __FILE__)

# Set up gems listed in the Gemfile.
require "bundler/setup"

# Load configuration.
require "superconfig/dotenv"
require File.expand_path("../config", __FILE__)

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/fnando/superconfig. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Icon

Icon made by eucalyp from Flaticon is licensed by Creative Commons BY 3.0.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].