config_to_env 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +22 -0
  3. data/README.md +65 -0
  4. data/lib/config_to_env.rb +56 -0
  5. metadata +47 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7346e2fd481e19c8234578a0b38da73935a99a6a
4
+ data.tar.gz: 5663274aff15ef27fe8335bc6c181c1ec5637187
5
+ SHA512:
6
+ metadata.gz: 3ad6e5615fc335649c3ddc68eabe972e2a4a82aa82ec2e95be2fe414b43b3b4c0cd33e38fa0df708016b36b40ffb7685aa38794b5823c3ab585030a722adb913
7
+ data.tar.gz: 7addf427e9a49d06efa033d73fdbe19c691767d73b1dfc1b5c371b82749a3afe2c1559a1075172db0141552cbbc71b4db19cfb29cd076a6b8da589acdc7b5239
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 BBC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,65 @@
1
+ # config_to_env
2
+
3
+ Take a json configuration file, and load it into the running
4
+ process environment variables before anything
5
+ in your program runs.
6
+
7
+ ## Why would you want to use this?
8
+
9
+ We have a number of applications that use environment variable
10
+ configuration overrides, but our deployment system uses json
11
+ file configs. It's useful for us to simply surface the
12
+ configuration as environment variables as our app loads.
13
+
14
+ ## How to use
15
+
16
+ The api is extremely simple, and mainly works at require time.
17
+
18
+ You just need to set a couple of constants to enable the config:
19
+
20
+ JSON_CONFIG = '/path/to/your/file.json'
21
+ require 'config_to_env'
22
+
23
+ This will load the contents of your json file into the environment
24
+ of the currently running process. A file like this:
25
+
26
+ {
27
+ "environment": "test",
28
+ "configuration": {
29
+ "aa": "one",
30
+ "bb": "two"
31
+ },
32
+ "secondary_configuration": {
33
+ "cc": "three"
34
+ }
35
+ }
36
+
37
+ Will be loaded into the environment as the following:
38
+
39
+ ENVIRONMENT: 'test'
40
+ CONFIGURATION_AA: 'one'
41
+ CONFIGURATION_BB: 'two'
42
+ SECONDARY_CONFIGURATION_CC: 'three'
43
+
44
+ You can optionally specify particular nodes of the json that
45
+ you want to include:
46
+
47
+ JSON_CONFIG_NODES = ['environment', 'configuration']
48
+
49
+ Which would give you:
50
+
51
+ ENVIRONMENT: 'test'
52
+ AA: 'one'
53
+ BB: 'two'
54
+
55
+ The secondary configuration is dropped entirely.
56
+
57
+ ## License
58
+
59
+ config_to_env is available to everyone under the terms of the
60
+ MIT open source licence. Take a look at the LICENSE file in the
61
+ code.
62
+
63
+ Copyright (c) 2015 BBC
64
+
65
+
@@ -0,0 +1,56 @@
1
+ require 'json'
2
+
3
+ module ConfigToEnv
4
+
5
+ # Take a hash, and flatten it down to a simple key/value pair hash
6
+ def self.flatten_hash(hash)
7
+
8
+ if ConfigToEnv.flat_hash?(hash)
9
+ return hash
10
+ else
11
+ # Perform single level flatten
12
+ processed_hash = {}
13
+ hash.each do |k,v|
14
+ if ConfigToEnv.is_value?(v)
15
+ processed_hash[k] = v
16
+ else
17
+ v.each do |l,w|
18
+ processed_hash[k + '_' + l] = w
19
+ end
20
+ end
21
+ end
22
+ return ConfigToEnv.flatten_hash(processed_hash)
23
+ end
24
+ end
25
+
26
+ def self.is_value?(node)
27
+ !node.respond_to?(:each)
28
+ end
29
+
30
+ def self.flat_hash?(hash)
31
+ hash.collect do |k,v|
32
+ return false if !ConfigToEnv.is_value?(v)
33
+ end
34
+ end
35
+
36
+
37
+ if JSON_CONFIG
38
+ config = JSON.parse( File.read(JSON_CONFIG) )
39
+ if JSON_CONFIG_NODES
40
+ hash = {}
41
+ JSON_CONFIG_NODES.each do |node|
42
+ if ConfigToEnv.is_value?(config[node])
43
+ hash[node] = config[node]
44
+ else
45
+ hash.merge!(config[node])
46
+ end
47
+ end
48
+ config = hash
49
+ end
50
+ config = ConfigToEnv.flatten_hash(config)
51
+ config.each do |k,v|
52
+ ENV[k.upcase] = v
53
+ end
54
+ end
55
+
56
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: config_to_env
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - David Buckhurst
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Simple lib for loading json config files into running process environment
14
+ email: david.buckhurst@bbc.co.uk
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - LICENSE
20
+ - README.md
21
+ - lib/config_to_env.rb
22
+ homepage: https://github.com/bbc/config_to_env.rb
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.4.5
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Load config file into process env
46
+ test_files: []
47
+ has_rdoc: