env_loader 0.1.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c79eedfc26cd79c97063ec9c90f2499f84c84f01
4
- data.tar.gz: e0e6405bf21904e512513e56e3ab27d7426fbcd5
3
+ metadata.gz: 073794c2413a0207c67b431f6168a026a742d340
4
+ data.tar.gz: 17efae9f198ae06171c30e2944acbbe4fa4b438b
5
5
  SHA512:
6
- metadata.gz: ce8cf474d9b68bcf2bb016cc3a250ee076113e9e5e89ab6148e0aa4a30e9107b83ae988d1cd02a7ee21a03d44e5e77225b140b5e27acf07f5ae0b98829995dc9
7
- data.tar.gz: 5a7a05fa3e475284f8ffa8536f1a58a52f831cf89913eb83344a0cb11632d7b0a9e4c448df4a113f54d6a8c250c55be13717535d63e658031e78003aecd1b62a
6
+ metadata.gz: 81af0b62308846e32d38998c26ea26836422a76f7472ffe1f8ab75bc4bc947379d2924f08d4d32d2e08ea481772c00d6604e005d762d5eb0aecae7ce1ecc0dfa
7
+ data.tar.gz: e2f11343c85b0019ba1a970dd6b8626ce3faaa7ca9f43b2a500c127965c364fd812ccae4a970bd50a2906a5551892f0914e901407ebcbcbf775b3b1236820763
data/.gitignore CHANGED
@@ -1,17 +1,9 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
- Gemfile.lock
7
- InstalledFiles
8
- _yardoc
9
- coverage
10
- doc/
11
- lib/bundler/man
12
- pkg
13
- rdoc
14
- spec/reports
15
- test/tmp
16
- test/version_tmp
17
- tmp
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013 Endri Gjiri
1
+ Copyright (c) 2015 Endri Gjiri
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  By: Endri Gjiri *www.name-reaction.com*
4
4
 
5
- **EnvLoader** is a utility module with a single `read` method which can read a .yml file and store its data in environment variables. This is useful when dealing with sensitive data that should not be directly entered in the code and stored in version control. Passwords and other information can now simply be stored in a yaml file which is added to .gitignore and then can be accessed through the env_loader gem via ENV variables
5
+ **EnvLoader** is a utility module with methods to load a .yml file and store its data in environment variables. This is useful when dealing with sensitive data that should not be directly entered in the code and stored in version control. Passwords and other information can now simply be stored in a yaml file which should be added to .gitignore and then can be accessed through the env_loader gem via ENV variables
6
6
 
7
7
  ## Installation
8
8
 
@@ -19,16 +19,29 @@ Or install it yourself as:
19
19
  $ gem install env_loader
20
20
 
21
21
  ## Usage
22
+ ##### TODO: Better Document this section
23
+ * `EnvLoader.setup_monolith`
24
+ * `EnvLoader.setup_microservice`
25
+
26
+ ---
27
+
28
+ ## DEPRECATED Usage
22
29
 
23
30
  1. Include the env_loader gem directly or through bundler
24
31
  ```ruby
25
32
  require 'env_loader'
26
33
  ```
27
34
 
28
- 2. Create a .yml file with all the desired information. For example create a file called credentials.yml with the following content:
35
+ 2. Create a .yml file with all the desired information. For example:
29
36
  ```yml
30
- username: endri
31
- password: secret
37
+ default: &default
38
+ username: 'endri'
39
+ development:
40
+ <<: *default
41
+ password: 'password'
42
+ production:
43
+ <<: *default
44
+ password: 'secret'
32
45
  ```
33
46
 
34
47
  3. Read the .yml file through the env_loader gem as follows:
@@ -42,7 +55,7 @@ ENV['USERNAME'] # => "endri"
42
55
  ENV['PASSWORD'] # => "secret"
43
56
  ```
44
57
 
45
- ## Usage with Rails
58
+ ## DEPRECATED Usage with Rails
46
59
 
47
60
  1. Add the env_loader gem to the Gemfile and run `Bundle install`
48
61
 
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'env_loader'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/env_loader.gemspec CHANGED
@@ -4,20 +4,24 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'env_loader/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "env_loader"
7
+ spec.name = 'env_loader'
8
8
  spec.version = EnvLoader::VERSION
9
- spec.authors = ["Endri Gjiri"]
10
- spec.email = ["egjiri@gmail.com"]
11
- spec.description = %q{Reads a YAML file and creates ENV variables from it}
12
- spec.summary = %q{Reads a YAML file and creates ENV variables from it}
13
- spec.homepage = ""
14
- spec.license = "MIT"
9
+ spec.authors = ['Endri Gjiri']
10
+ spec.email = ['egjiri@gmail.com']
15
11
 
16
- spec.files = `git ls-files`.split($/)
17
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
12
+ spec.summary = 'Reads a YAML file and creates ENV variables from it'
13
+ spec.description = 'Reads a YAML file and creates ENV variables from it'
14
+ # spec.homepage = "TODO: Put your gem's website or public repo URL here."
15
+ spec.license = 'MIT'
20
16
 
21
- spec.add_development_dependency "bundler", "~> 1.3"
22
- spec.add_development_dependency "rake"
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'uiux'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.10'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec'
23
27
  end
@@ -0,0 +1,50 @@
1
+ require 'yaml'
2
+ require 'json'
3
+
4
+ module EnvLoader
5
+ class Configurator
6
+ attr_reader :raw_config
7
+
8
+ def initialize(config_file: nil, config_content: nil, config_subtree_key: nil)
9
+ if config_file
10
+ @raw_config = config_from_file(config_file, config_subtree_key)
11
+ elsif config_content
12
+ @raw_config = config_from_content(config_content, config_subtree_key)
13
+ end
14
+ end
15
+
16
+ def config
17
+ raw_config.each_with_object({}) do |hash, new_hash|
18
+ key, value = hash
19
+ value = value.is_a?(Hash) ? value.to_json : value.to_s unless value.nil?
20
+ new_hash[key.upcase] = value
21
+ end
22
+ end
23
+
24
+ def expose_values_as_environment_variables
25
+ config.each { |key, value| ENV[key] = value }
26
+ end
27
+
28
+ private
29
+
30
+ def config_from_file(config_file, config_subtree_key = nil)
31
+ return {} unless File.exist?(config_file)
32
+ config_content = File.read(config_file)
33
+ config_from_content(config_content, config_subtree_key)
34
+ end
35
+
36
+ def config_from_content(config_content, config_subtree_key = nil)
37
+ global_config = YAML.load(config_content)
38
+ return global_config unless config_subtree_key
39
+ extract_subhash_from_hash_and_keys(global_config, config_subtree_key.to_s.split('.'))
40
+ end
41
+
42
+ def extract_subhash_from_hash_and_keys(hash, subhash_keys)
43
+ key = subhash_keys.shift
44
+ return unless key
45
+ return hash['default'] || {} unless hash.key?(key)
46
+ return hash[key] if subhash_keys.empty?
47
+ extract_subhash_from_hash_and_keys(hash[key], subhash_keys)
48
+ end
49
+ end
50
+ end
@@ -1,3 +1,3 @@
1
1
  module EnvLoader
2
- VERSION = "0.1.0"
2
+ VERSION = '0.3.0'
3
3
  end
data/lib/env_loader.rb CHANGED
@@ -1,38 +1,34 @@
1
1
  require 'env_loader/version'
2
- require 'yaml'
2
+ require 'env_loader/configurator'
3
+ require 'uiux'
3
4
 
4
5
  module EnvLoader
6
+ # A .monolith-config.yml file must be placed at the root of the monolith
7
+ def self.setup_monolith
8
+ config(config_file: File.join('.monolith-config.yml'), config_subtree_key: environment)
9
+ end
5
10
 
6
- def self.read(env_yml_file = File.join('config', 'env_variables.yml'))
7
- if File.exists? env_yml_file
8
- if hash = YAML.load_file(env_yml_file)
9
- hash.each do |key, value|
10
- if value
11
- ENV[key.upcase] = format_value(value)
12
- end
13
- end
14
- end
15
- end
11
+ # A .microservices-config.yml file must be placed at the parent directory of the microservice
12
+ def self.setup_microservice
13
+ subtree_key = environment
14
+ subtree_key += ".#{Rails.application.class.parent_name.underscore}" if defined?(Rails)
15
+ config(config_file: File.join('..', '.microservices-config.yml'), config_subtree_key: subtree_key)
16
16
  end
17
17
 
18
- def self.get(key)
19
- if value = ENV[key.to_s.upcase]
20
- if value.match(/^\{.*\}$/)
21
- JSON.parse(value).with_indifferent_access
22
- else
23
- value
24
- end
25
- end
18
+ # DEPRECATED (<= 0.2.0):
19
+ # Legacy setup which will be replaced by the above
20
+ def self.read(config_file = File.join('config', 'env_variables.yml'))
21
+ config(config_file: config_file)
26
22
  end
27
23
 
28
24
  private
29
25
 
30
- def self.format_value(value)
31
- if value.is_a? Hash
32
- value = value.to_json
33
- else
34
- value = value.to_s
35
- end
26
+ def self.environment
27
+ Rails.env if defined?(Rails)
36
28
  end
37
- end
38
29
 
30
+ def self.config(config_file: nil, config_content: nil, config_subtree_key: nil)
31
+ configurator = Configurator.new(config_file: config_file, config_content: config_content, config_subtree_key: config_subtree_key)
32
+ configurator.expose_values_as_environment_variables if configurator
33
+ end
34
+ end
metadata CHANGED
@@ -1,41 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: env_loader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Endri Gjiri
8
8
  autorequire:
9
- bindir: bin
9
+ bindir: exe
10
10
  cert_chain: []
11
- date: 2014-04-14 00:00:00.000000000 Z
11
+ date: 2015-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: uiux
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
- - - ~>
31
+ - - "~>"
18
32
  - !ruby/object:Gem::Version
19
- version: '1.3'
33
+ version: '1.10'
20
34
  type: :development
21
35
  prerelease: false
22
36
  version_requirements: !ruby/object:Gem::Requirement
23
37
  requirements:
24
- - - ~>
38
+ - - "~>"
25
39
  - !ruby/object:Gem::Version
26
- version: '1.3'
40
+ version: '1.10'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rake
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
- - - '>='
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
32
60
  - !ruby/object:Gem::Version
33
61
  version: '0'
34
62
  type: :development
35
63
  prerelease: false
36
64
  version_requirements: !ruby/object:Gem::Requirement
37
65
  requirements:
38
- - - '>='
66
+ - - ">="
39
67
  - !ruby/object:Gem::Version
40
68
  version: '0'
41
69
  description: Reads a YAML file and creates ENV variables from it
@@ -45,15 +73,20 @@ executables: []
45
73
  extensions: []
46
74
  extra_rdoc_files: []
47
75
  files:
48
- - .gitignore
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
49
79
  - Gemfile
50
80
  - LICENSE.txt
51
81
  - README.md
52
82
  - Rakefile
83
+ - bin/console
84
+ - bin/setup
53
85
  - env_loader.gemspec
54
86
  - lib/env_loader.rb
87
+ - lib/env_loader/configurator.rb
55
88
  - lib/env_loader/version.rb
56
- homepage: ''
89
+ homepage:
57
90
  licenses:
58
91
  - MIT
59
92
  metadata: {}
@@ -63,17 +96,17 @@ require_paths:
63
96
  - lib
64
97
  required_ruby_version: !ruby/object:Gem::Requirement
65
98
  requirements:
66
- - - '>='
99
+ - - ">="
67
100
  - !ruby/object:Gem::Version
68
101
  version: '0'
69
102
  required_rubygems_version: !ruby/object:Gem::Requirement
70
103
  requirements:
71
- - - '>='
104
+ - - ">="
72
105
  - !ruby/object:Gem::Version
73
106
  version: '0'
74
107
  requirements: []
75
108
  rubyforge_project:
76
- rubygems_version: 2.0.14
109
+ rubygems_version: 2.4.5.1
77
110
  signing_key:
78
111
  specification_version: 4
79
112
  summary: Reads a YAML file and creates ENV variables from it