Giter VIP home page Giter VIP logo

bogo-config's Introduction

Bogo Config

Hash based application configuration helpers, because snowflake configurations are evil.

Usage

Direct

Use the Bogo::Config directly to store configuration values:

require 'bogo-config'

config = Bogo::Config.new(
  :bind => {
    :address => '0.0.0.0',
    :port => 8080
  },
  :run_as => 'www-data'
)

puts config.get(:bind, :address)
puts config[:run_as]

Configuration files

A path to a configuration file can also be provided. Lets define the file:

# /tmp/bogo-config.json
{
  "bind": {
    "address": "0.0.0.0",
    "port": 8080
  },
  "run_as": "www-data"
}

and now we can load it:

require 'bogo-config'

config = Bogo::Config.new('/tmp/bogo-config.json')

puts config.get(:bind, :address)
puts config[:run_as]

Subclassing

Subclassing Bogo::Config allows adding some structure to the configuration file. The Bogo::Lazy module is used to provide the #attribute method to describe the configuration:

require 'bogo-config'

class MyConfig < Bogo::Config
  attribute :bind, Smash, :coerce => proc{|v| v.to_smash}
  attribute :run_as, String, :default => 'www-data'
end

config = MyConfig.new('/tmp/bogo-config.json')
puts config.get(:bind, :address)
puts config[:run_as]

Configuration file

Support for AttributeStruct configuration files is builtin. A helper class is provided as a visual nicety. The above JSON example could also be written as:

# /tmp/bogo-config.rb
Configuration.new do
  bind do
    address '0.0.0.0'
    port 8080
  end
  run_as 'www-data'
end

Configuration file support

Currently the following serialization types are supported:

  • JSON
  • YAML
  • XML
  • AttributeStruct

Note on XML configuration files: The configuration must be contained within a <configuration> tag. The above example would then look like this:

<configuration>
  <bind>
    <address>
      0.0.0.0
    </address>
    <port>
      8080
    </port>
  </bind>
  <run_as>
    www-data
  </run_as>
</configuration>

Configuration directory

The path provided on initialization can also be a directory. The contents of the directory will be read in string sorted order and deep merged. Files can be a mix of supported types.

Ruby evaluation

Ruby evaluation is super awesome, except when you don't want it and then it's not super awesome. If a running process needs to disable Ruby based evaluation (a service perhaps), this can be done with an environment variable:

export BOGO_CONFIG_DISABLE_EVAL=true

Info

bogo-config's People

Contributors

chrisroberts avatar imbriaco avatar luckymike avatar grosser avatar

Stargazers

Mark Woods avatar

Watchers

 avatar James Cloos avatar

bogo-config's Issues

Can't use Kernel methods in a Configuration block

Unadorned methods like catch and throw can't currently be used in a Configuration block without causing load errors.

 credentials do
    provider :aws

    catch(:done) do
      [ENV['AWS_CREDENTIALS_PATH'], File.expand_path("~/.aws/config"), File.expand_path('~/.aws/credentials')].each do |file|
        if File.exist?(file.to_s)
          aws_profile_name ENV['AWS_PROFILE'] || 'tokyo'
          aws_credentials_file file
          throw(:done)
        end
      end
   end

One fully-qualify the methods (e.g. ::Kernel.catch) to avoid errors, but this is a pretty baroque workaround, especially for non-skilled Ruby programmers who are used to DSLs like Chef recipes working without them.

config is loaded twice

this is bad because it is slow and does whatever is done in .sfn twice ... which in our case means doing the same work twice which lead to a bug

once when initializing the config:

vendor/bundle/gems/bogo-config-0.2.2/lib/bogo-config/config.rb:297:in `instance_eval'
vendor/bundle/gems/bogo-config-0.2.2/lib/bogo-config/config.rb:297:in `struct_load'
vendor/bundle/gems/bogo-config-0.2.2/lib/bogo-config/config.rb:203:in `load_file'
vendor/bundle/gems/bogo-config-0.2.2/lib/bogo-config/config.rb:178:in `load!'
vendor/bundle/gems/bogo-config-0.2.2/lib/bogo-config/config.rb:138:in `init!'
vendor/bundle/gems/bogo-config-0.2.2/lib/bogo-config/config.rb:114:in `initialize'

and once when actually loading the config after:

vendor/bundle/gems/bogo-config-0.2.2/lib/bogo-config/config.rb:203:in `load_file'
vendor/bundle/gems/bogo-config-0.2.2/lib/bogo-config/config.rb:178:in `load!'
vendor/bundle/gems/bogo-cli-0.2.8/lib/bogo-cli/command.rb:103:in `load_config!'
vendor/bundle/gems/bogo-cli-0.2.8/lib/bogo-cli/command.rb:36:in `initialize'
vendor/bundle/gems/sfn-3.0.18/lib/sfn/command.rb:51:in `initialize'

@chrisroberts

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.