3

I'm building a cli tool in ruby, and I need to take config from different sources: environment variable, dotfile, arguments or hardcoded values. (with a precedence system)

In node.js I would have used nconf.js, to do this.

Is there some configuration gem in ruby that enable to do such a thing?

AdrieanKhisbe
  • 3,899
  • 8
  • 37
  • 45
  • 1
    Yes, take a look here: http://awesome-ruby.com/#awesome-ruby-configuration. If you're building a CLI tool, also check: http://awesome-ruby.com/#awesome-ruby-cli-builder – markets Nov 12 '15 at 15:59

1 Answers1

0

The actual answer is this:

updated: 2020-02-26

https://github.com/infochimps-labs/configliere

to quote the author:

Be willing to sit down with the Five Families. Takes settings from (at your option):

  • Pre-defined defaults from constants
  • Simple config files
  • Environment variables
  • Commandline options and git-style command runners
  • Ruby block (called when all other options are in place)

put simply. just like nconf.

require 'configliere'

Settings.use :commandline
Settings({
    :dest_time => '11-05-1955',
    :fluxcapacitor => {
        :speed => 88,
    },
    :delorean => {
        :power_source => 'plutonium',
        :roads_needed => true,
    },
    :username => 'marty',
    :password => '',
})

#set a value to possibly also come from env
Settings.define :dest_time,   :env_var => 'DEST_TIME'

Settings.read "#{__dir__}/config.yml"
Settings.read "#{Dir.pwd()}/config.yml"
Settings.resolve!

old answer:

https://github.com/rubyconfig/config#working-with-environment-variables

it doesn't do argv, but it lets you layer various yaml files and then override with ENV just like nconf lets you.

airtonix
  • 4,772
  • 2
  • 38
  • 36