config-rc 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +37 -0
- data/Rakefile +10 -0
- data/config-rc.gemspec +22 -0
- data/lib/config-rc.rb +2 -0
- data/lib/config-rc/base.rb +7 -0
- data/lib/config-rc/configuration.rb +32 -0
- data/lib/config-rc/environment.rb +14 -0
- data/lib/config-rc/options.rb +14 -0
- data/lib/config-rc/rc_file.rb +19 -0
- data/lib/config-rc/version.rb +3 -0
- data/test/bin/app +40 -0
- data/test/fixtures/.testconfigrc +1 -0
- data/test/helper.rb +2 -0
- data/test/unit/test_config_rc.rb +42 -0
- metadata +131 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Steffen Uhlig
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# ConfigRC
|
2
|
+
|
3
|
+
Provides simple, hierarchical app configuration:
|
4
|
+
|
5
|
+
* Environment variables take precendence over
|
6
|
+
* Command-line options, which take precendence over
|
7
|
+
* rc-file options.
|
8
|
+
|
9
|
+
Environment variables are expected to be prefixed with the application name and an underscore (e.g. `app_foo` will set `foo`). The rc file is assumed to have the usual name of `.apprc` in the user's home directory. An additional options hash can be passed.
|
10
|
+
|
11
|
+
[](http://travis-ci.org/nerab/config-rc)
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
gem 'config-rc'
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install config-rc
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
See test/bin/app for an example.
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
1. Fork it
|
34
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
35
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
36
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
37
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/config-rc.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/config-rc/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Nicholas E. Rabenau"]
|
6
|
+
gem.email = "nerab@gmx.net"
|
7
|
+
gem.description = %q{Opinionated configuration library. Reads from config file, command line option and environment variable.}
|
8
|
+
gem.summary = %q{Opinionated config library}
|
9
|
+
gem.homepage = "http://github.com/nerab/config-rc"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "config-rc"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = ConfigRC::VERSION
|
17
|
+
|
18
|
+
gem.add_runtime_dependency 'require_all'
|
19
|
+
gem.add_development_dependency 'minitest'
|
20
|
+
gem.add_development_dependency 'pry'
|
21
|
+
gem.add_development_dependency 'rake'
|
22
|
+
end
|
data/lib/config-rc.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
|
3
|
+
module ConfigRC
|
4
|
+
#
|
5
|
+
# Provide hierarchical configuration.
|
6
|
+
#
|
7
|
+
# Environment variables win over
|
8
|
+
# command line options win over
|
9
|
+
# settings from the config file
|
10
|
+
#
|
11
|
+
class Configuration
|
12
|
+
include Enumerable
|
13
|
+
extend Forwardable
|
14
|
+
def_delegators :map, :each, :[]
|
15
|
+
|
16
|
+
def initialize(app_name, options = {})
|
17
|
+
@providers = [RcFileProvider.new(app_name), OptionsProvider.new(options), EnvironmentProvider.new(app_name)]
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
#
|
22
|
+
# Provides the union of each key across all providers
|
23
|
+
#
|
24
|
+
def map
|
25
|
+
result = {}
|
26
|
+
@providers.each do |provider|
|
27
|
+
result.merge!(provider.map)
|
28
|
+
end
|
29
|
+
result
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module ConfigRC
|
2
|
+
#
|
3
|
+
# Configuration stored as environment variable
|
4
|
+
#
|
5
|
+
class EnvironmentProvider < BaseProvider
|
6
|
+
def initialize(prefix)
|
7
|
+
@prefix = "#{prefix}_"
|
8
|
+
end
|
9
|
+
|
10
|
+
def map
|
11
|
+
ENV.select{|k, v| k.start_with?(@prefix)}.each_with_object({}){|(k, v), h| h[k.gsub(@prefix, '')] = v}
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module ConfigRC
|
2
|
+
#
|
3
|
+
# Configuration stored in a file
|
4
|
+
#
|
5
|
+
class RcFileProvider < BaseProvider
|
6
|
+
def initialize(prefix)
|
7
|
+
@path = File.expand_path(File.join('~', ".#{prefix}rc"))
|
8
|
+
end
|
9
|
+
|
10
|
+
def map
|
11
|
+
props = {}
|
12
|
+
File.open(@path).select{|line| not line =~ /^[ \t]*(#.+)*$/}.each do |line|
|
13
|
+
k, v = line.chomp.split('=', 2)
|
14
|
+
props[k] = v
|
15
|
+
end if File.exist?(@path)
|
16
|
+
props
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/test/bin/app
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
#
|
4
|
+
# Sample usage:
|
5
|
+
#
|
6
|
+
# HOME=$(pwd)/test/fixtures test/bin/app.rb
|
7
|
+
#
|
8
|
+
# to see foo being read from test/fixtures/.testconfigrc. Override this
|
9
|
+
# setting with an environment variable:
|
10
|
+
#
|
11
|
+
# HOME=$(pwd)/test/fixtures testconfig_foo=env test/bin/app.rb
|
12
|
+
#
|
13
|
+
require 'bundler'
|
14
|
+
Bundler.require
|
15
|
+
|
16
|
+
require 'config-rc'
|
17
|
+
|
18
|
+
class App
|
19
|
+
attr_reader :config
|
20
|
+
APP_NAME = 'testconfig'
|
21
|
+
|
22
|
+
def initialize
|
23
|
+
@config = ConfigRC::Configuration.new(APP_NAME, 'foo' => 'option')
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_s
|
27
|
+
"Hi, my name is '#{APP_NAME}', and I do " <<
|
28
|
+
if @config.any?
|
29
|
+
"have configuration:"
|
30
|
+
else
|
31
|
+
"not have any configuration."
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
app = App.new
|
37
|
+
puts app.to_s
|
38
|
+
app.config.each do |k, v|
|
39
|
+
puts "#{k}=#{v}"
|
40
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
foo=file
|
data/test/helper.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestConfigRC < MiniTest::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
ENV['HOME'] = File.join(File.dirname(__FILE__), '..', 'fixtures')
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_empty
|
9
|
+
c = ConfigRC::Configuration.new('test_empty')
|
10
|
+
refute(c.any?)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_file
|
14
|
+
ENV['testconfig_foo'] = nil
|
15
|
+
c = ConfigRC::Configuration.new('testconfig')
|
16
|
+
assert_equal('file', c['foo'])
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_file_option
|
20
|
+
ENV['testconfig_foo'] = nil
|
21
|
+
c = ConfigRC::Configuration.new('testconfig', 'foo' => 'option')
|
22
|
+
assert_equal('option', c['foo'])
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_file_option_env
|
26
|
+
ENV['testconfig_foo'] = 'env'
|
27
|
+
c = ConfigRC::Configuration.new('testconfig', 'foo' => 'option')
|
28
|
+
assert_equal('env', c['foo'])
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_file_env
|
32
|
+
ENV['testconfig_foo'] = 'env'
|
33
|
+
c = ConfigRC::Configuration.new('testconfig', 'foo' => 'option')
|
34
|
+
assert_equal('env', c['foo'])
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_option_env
|
38
|
+
ENV['testconfig2_foo2'] = 'envi'
|
39
|
+
c = ConfigRC::Configuration.new('testconfig2', 'foo2' => 'option')
|
40
|
+
assert_equal('envi', c['foo2'])
|
41
|
+
end
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: config-rc
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Nicholas E. Rabenau
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-09 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: require_all
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: minitest
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: pry
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rake
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Opinionated configuration library. Reads from config file, command line
|
79
|
+
option and environment variable.
|
80
|
+
email: nerab@gmx.net
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- .travis.yml
|
87
|
+
- Gemfile
|
88
|
+
- LICENSE
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- config-rc.gemspec
|
92
|
+
- lib/config-rc.rb
|
93
|
+
- lib/config-rc/base.rb
|
94
|
+
- lib/config-rc/configuration.rb
|
95
|
+
- lib/config-rc/environment.rb
|
96
|
+
- lib/config-rc/options.rb
|
97
|
+
- lib/config-rc/rc_file.rb
|
98
|
+
- lib/config-rc/version.rb
|
99
|
+
- test/bin/app
|
100
|
+
- test/fixtures/.testconfigrc
|
101
|
+
- test/helper.rb
|
102
|
+
- test/unit/test_config_rc.rb
|
103
|
+
homepage: http://github.com/nerab/config-rc
|
104
|
+
licenses: []
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
none: false
|
117
|
+
requirements:
|
118
|
+
- - ! '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 1.8.24
|
124
|
+
signing_key:
|
125
|
+
specification_version: 3
|
126
|
+
summary: Opinionated config library
|
127
|
+
test_files:
|
128
|
+
- test/bin/app
|
129
|
+
- test/fixtures/.testconfigrc
|
130
|
+
- test/helper.rb
|
131
|
+
- test/unit/test_config_rc.rb
|