All Projects → luckyframework → habitat

luckyframework / habitat

Licence: MIT license
Easily configure settings for Crystal projects

Programming Languages

crystal
512 projects

Projects that are alternatives of or similar to habitat

JsonSettings
This library simplifies creating configuration for your C# app/service by utilizing the serialization capabilities of Json.NET to serialize nested (custom) objects, dictionaries and lists as simply as by creating a POCO and inheriting JsonSettings class.
Stars: ✭ 59 (-19.18%)
Mutual labels:  configuration
dotfiles
🔧 .files - different setups separated in branches
Stars: ✭ 168 (+130.14%)
Mutual labels:  configuration
envkey-python
EnvKey's python library. Protect API keys and credentials. Keep configuration in sync.
Stars: ✭ 24 (-67.12%)
Mutual labels:  configuration
gl-ionic2-env-configuration
An Ionic2 Service to load an environment specific configuration before everything else
Stars: ✭ 23 (-68.49%)
Mutual labels:  configuration
PropertiesFile4Delphi
Library for managing configuration files with key-value syntax
Stars: ✭ 17 (-76.71%)
Mutual labels:  configuration
Configuration
Hierarchical configuration manager for Swift applications
Stars: ✭ 72 (-1.37%)
Mutual labels:  configuration
swiss-army
Ansible-driven configuration management for maintaining a preferred environment (base system and app dotfiles / configurations)
Stars: ✭ 44 (-39.73%)
Mutual labels:  configuration
open-scd
A substation configuration description editor for projects using SCL IEC 61850-6 Edition 2 or greater
Stars: ✭ 35 (-52.05%)
Mutual labels:  configuration
emacs-config
My personal emacs configuration based on use-package.
Stars: ✭ 34 (-53.42%)
Mutual labels:  configuration
scalecube-config
ScaleCube Config is a configuration access management library for JVM based distributed applications
Stars: ✭ 15 (-79.45%)
Mutual labels:  configuration
circe-config
Yet another Typesafe config Scala wrapper powered by circe
Stars: ✭ 18 (-75.34%)
Mutual labels:  configuration
github-enterprise-cloud-configuration
Guideline of best practices to follow to configure Github Enterprise Cloud in a secure way.
Stars: ✭ 30 (-58.9%)
Mutual labels:  configuration
wcm-io-caconfig
Context-Aware Configuration for AEM applications.
Stars: ✭ 16 (-78.08%)
Mutual labels:  configuration
django-confy
A comfy config for Django
Stars: ✭ 25 (-65.75%)
Mutual labels:  configuration
configuro
An opinionated configuration loading framework for Containerized and Cloud-Native applications.
Stars: ✭ 81 (+10.96%)
Mutual labels:  configuration
dot-emacs
Emacs configuration
Stars: ✭ 23 (-68.49%)
Mutual labels:  configuration
go-chassis-config
pull and push configs in distributed configuration management service. migrated to go-archaius https://github.com/go-chassis/go-archaius/pull/87
Stars: ✭ 23 (-68.49%)
Mutual labels:  configuration
CoSky
High-performance, low-cost microservice governance platform. Service Discovery and Configuration Service | 高性能、低成本微服务治理平台
Stars: ✭ 57 (-21.92%)
Mutual labels:  configuration
unity-auto-preset
Auto Preset enables scriptable configuration of assets from the Unity Editor without requiring any additional code.
Stars: ✭ 19 (-73.97%)
Mutual labels:  configuration
frontend-platform
A framework for Open edX micro-frontend applications.
Stars: ✭ 17 (-76.71%)
Mutual labels:  configuration

Habitat

API Documentation Website

Easily configure settings for Crystal projects

Installation

Add this to your application's shard.yml:

dependencies:
  habitat:
    github: luckyframework/habitat

Usage

require "habitat"
class MyServer
  Habitat.create do
    setting port : Int32
    setting debug_errors : Bool = true

    # Optionally add examples to settings that appear in error messages
    # when the value is not set.
    #
    # Use `String#dump` when you want the example to be wrapped in quotes
    setting host : String, example: "127.0.0.1".dump
    setting logger : Logger, example: "Logger.new(STDOUT)"

    # If you need the value to match a specific format, you can create
    # your own validation.
    setting protocol : String, validation: :validate_protocol
  end

  # Read more on validations below
  def self.validate_protocol(value : String)
    value.match(/^http(?:s)*:$/) || Habitat.raise_validation_error("The protocol must be `http:` or `https:`.")
  end

  # Access them with the `settings` method like this.
  def start
    start_server_on port: settings.port
  end
end

# Configure your settings
MyServer.configure do |settings|
  settings.port = 8080
end

# At the very end of your program use this
# It will raise if you forgot to set any settings
Habitat.raise_if_missing_settings!

Settings can also be accessed from outside the class:

port = MyServer.settings.port
puts "The server is starting on port #{port}"

Setting validations

The validation option takes a Symbol which matches a class method that will run your custom validation. This can be useful if your setting needs to be in a specific format like maybe a 4 digit code that can start with a 0.

class Secret
  Habitat.create do
    setting code : String, validation: :validate_code
  end

  # The validation method will take an argument of the same type.
  # If your setting is `Int32`, then this argument will also be `Int32`.
  #
  # Use any method of validation you'd like here. (i.e. regex, other custom methods, etc...)
  # If your validation fails, you can call `Habitat.raise_validation_error` with your custom error
  # message
  def self.validate_code(value : String)
    value.match(/^\d{4}$/) || Habitat.raise_validation_error("Be sure the code is only 4 digits")
  end
end

Secret.configure do |settings|

  # Even though the code is the correct type, this will still
  # raise an error for us.
  settings.code = "ABCD"

  # This value will pass our validation
  settings.code = "0123"
end

Temp Config

There are some cases in which you may want to temporarily change a setting value. (i.e. specs, one off jobs, etc...)

Habitat comes with a built-in method temp_config that allows you to do this:

class Server
  Habitat.create do
    setting hostname : String
  end
end

Server.configure do |settings|
  settings.hostname = "localhost"
end

Server.settings.hostname #=> "localhost"

Server.temp_config(hostname: "fancyhost.com") do
  # This seting affects the value globally while inside this block
  Server.settings.hostname #=> "fancyhost.com"
end

# Once the block exits, the original value is returned
Server.settings.hostname #=> "localhost"

Contributing

  1. Fork it ( https://github.com/luckyframework/habitat/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

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].