env_configuration 0.1.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,47 +1,47 @@
1
-
2
- lib = File.expand_path("../lib", __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "env_configuration/version"
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "env_configuration"
8
- spec.version = EnvConfiguration::VERSION
9
- spec.authors = ["Channa Ly"]
10
- spec.email = ["channa.info@gmail.com"]
11
-
12
- spec.summary = %q{
13
- A gem to config env variable in development in dotenv, yaml, aws ssm parameter store, heroku in ruby and rails project.
14
- }
15
- spec.description = %q{
16
- This gem aims to load config to ENV varaible easily. Currently configuration can be done by a .env, a yaml file or by loading from
17
- AWS System Manager Parameter Store which is recommended for production deployment.
18
-
19
- }
20
- spec.homepage = "https://github.com/channainfo/env_configuration"
21
- spec.license = "MIT"
22
-
23
- # # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
24
- # # to allow pushing to a single host or delete this section to allow pushing to any host.
25
- # if spec.respond_to?(:metadata)
26
- # spec.metadata["allowed_push_host"] = "http://mygemserver.com"
27
- # else
28
- # raise "RubyGems 2.0 or newer is required to protect against " \
29
- # "public gem pushes."
30
- # end
31
-
32
- # Specify which files should be added to the gem when it is released.
33
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
34
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
35
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
36
- end
37
- spec.bindir = "exe"
38
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
39
- spec.require_paths = ["lib"]
40
-
41
- spec.add_development_dependency "bundler", "~> 1.16"
42
- spec.add_development_dependency "rake", "~> 10.0"
43
- spec.add_development_dependency "rspec", "~> 3.0"
44
-
45
- spec.add_dependency "aws-sdk-ssm"
46
- spec.add_dependency "dotenv-rails", "2.7.4"
47
- end
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "env_configuration/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "env_configuration"
8
+ spec.version = EnvConfiguration::VERSION
9
+ spec.authors = ["Channa Ly"]
10
+ spec.email = ["channa.info@gmail.com"]
11
+
12
+ spec.summary = %q{
13
+ A gem to config env variable in development in dotenv, yaml, aws ssm parameter store, heroku in ruby and rails project.
14
+ }
15
+ spec.description = %q{
16
+ This gem aims to load config to ENV varaible easily. Currently configuration can be done by a .env, a yaml file or by loading from
17
+ AWS System Manager Parameter Store which is recommended for production deployment.
18
+
19
+ }
20
+ spec.homepage = "https://github.com/channainfo/env_configuration"
21
+ spec.license = "MIT"
22
+
23
+ # # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
24
+ # # to allow pushing to a single host or delete this section to allow pushing to any host.
25
+ # if spec.respond_to?(:metadata)
26
+ # spec.metadata["allowed_push_host"] = "http://mygemserver.com"
27
+ # else
28
+ # raise "RubyGems 2.0 or newer is required to protect against " \
29
+ # "public gem pushes."
30
+ # end
31
+
32
+ # Specify which files should be added to the gem when it is released.
33
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
34
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
35
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
36
+ end
37
+ spec.bindir = "exe"
38
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
39
+ spec.require_paths = ["lib"]
40
+
41
+ spec.add_development_dependency "bundler", "~> 1.16"
42
+ spec.add_development_dependency "rake", "~> 10.0"
43
+ spec.add_development_dependency "rspec", "~> 3.0"
44
+
45
+ spec.add_dependency "aws-sdk-ssm"
46
+ spec.add_dependency "dotenv-rails", "2.7.4"
47
+ end
@@ -1,27 +1,27 @@
1
- require "env_configuration/version"
2
- require "env_configuration/configurator"
3
- require "env_configuration/configuration"
4
- require "env_configuration/aws_ssm_parameter_store_writer"
5
-
6
- module EnvConfiguration
7
- class Error < StandardError; end
8
-
9
- class << self
10
- attr_accessor :configuration
11
- end
12
-
13
-
14
- def self.configuration
15
- @configuration ||= Configuration.new
16
- end
17
-
18
- def self.configure
19
- yield(self.configuration)
20
- self.configuration
21
- end
22
-
23
- def self.reset_configuration
24
- @configuration = Configuration.new
25
- end
26
-
27
- end
1
+ require "env_configuration/version"
2
+ require "env_configuration/configurator"
3
+ require "env_configuration/configuration"
4
+
5
+ module EnvConfiguration
6
+ class Error < StandardError; end
7
+
8
+ class << self
9
+ attr_accessor :configuration
10
+ end
11
+
12
+
13
+ def self.configuration
14
+ @configuration ||= Configuration.new
15
+ end
16
+
17
+ def self.configure
18
+ yield(self.configuration)
19
+ self.configuration
20
+ end
21
+
22
+ def self.reset_configuration
23
+ @configuration = Configuration.new
24
+ end
25
+
26
+ end
27
+
@@ -1,91 +1,88 @@
1
- module EnvConfiguration
2
- module Adapter
3
- class AwsSsmParameterStoreAdapter < Base
4
-
5
- # if you set ENV['AWS_ACCESS_KEY_ID'],ENV['AWS_SECRET_ACCESS_KEY'], ENV['AWS_REGION']
6
- # you don't need to pass the options
7
- # { access_key_id: ENV['AWS_ACCESS_KEY_ID'], secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'], region: ENV['AWS_REGION']
8
- def initialize(options={})
9
- super(options)
10
-
11
- end
12
-
13
- def client
14
- aws_options = {
15
- access_key_id: access_key_id,
16
- secret_access_key: secret_access_key,
17
- region: region,
18
- }
19
- @client ||= Aws::SSM::Client.new(@options)
20
- end
21
-
22
- def access_key_id
23
- @options[:access_key_id] || EnvConfiguration.configuration.aws_access_key_id
24
- end
25
-
26
- def secret_access_key
27
- @options[:secret_access_key] || EnvConfiguration.configuration.aws_secret_access_key
28
- end
29
-
30
- def region
31
- @options[:region] || EnvConfiguration.configuration.aws_region
32
- end
33
-
34
- def path
35
- path_value = @options[:path] || EnvConfiguration.configuration.aws_path
36
- raise ":path options is required for example /staging" if path_value.nil?
37
- path_value
38
- end
39
-
40
- def load
41
- # fetch_configs do |item|
42
- # ENV["#{item[:name]}"] = item[:value]
43
- # end
44
-
45
- configs = fetch_configs
46
- ENV.update(configs)
47
- configs
48
- end
49
-
50
- def fetch_configs(&block)
51
- next_token = nil
52
- result = {}
53
-
54
- while(true)
55
-
56
- fetch_options = {
57
- path: path,
58
- with_decryption: false,
59
- recursive: true,
60
- max_results: 10
61
- }
62
-
63
- fetch_options[:next_token] = next_token if next_token
64
-
65
- response = client.get_parameters_by_path(fetch_options)
66
-
67
- items = sanitize_configs(response.parameters, &block)
68
- result.merge!(items)
69
-
70
- next_token = response.next_token
71
- break if next_token.nil?
72
- end
73
-
74
- result
75
- end
76
-
77
- def sanitize_configs(parameters, &block)
78
- items = {}
79
- parameters.each do |param|
80
- name = param.name.gsub("#{path}/", '')
81
- value = param.value
82
- item = {name: name, value: value}
83
- items[name] = value
84
- yield(item) if block_given?
85
- end
86
- items
87
- end
88
- end
89
-
90
- end
91
- end
1
+ module EnvConfiguration
2
+ module Adapter
3
+ class AwsSsmParameterStoreAdapter < Base
4
+
5
+ def initialize(options={})
6
+ super(options)
7
+
8
+ end
9
+
10
+ def client
11
+ aws_options = {
12
+ access_key_id: access_key_id,
13
+ secret_access_key: secret_access_key,
14
+ region: region,
15
+ }
16
+ @client ||= Aws::SSM::Client.new(@options)
17
+ end
18
+
19
+ def access_key_id
20
+ @options[:access_key_id] || EnvConfiguration.configuration.aws_access_key_id
21
+ end
22
+
23
+ def secret_access_key
24
+ @options[:secret_access_key] || EnvConfiguration.configuration.aws_secret_access_key
25
+ end
26
+
27
+ def region
28
+ @options[:region] || EnvConfiguration.configuration.aws_region
29
+ end
30
+
31
+ def path
32
+ path_value = @options[:path] || EnvConfiguration.configuration.aws_path
33
+ raise ":path options is required for example /staging" if path_value.nil?
34
+ path_value
35
+ end
36
+
37
+ def load
38
+ # fetch_configs do |item|
39
+ # ENV["#{item[:name]}"] = item[:value]
40
+ # end
41
+
42
+ configs = fetch_configs
43
+ ENV.update(configs)
44
+ configs
45
+ end
46
+
47
+ def fetch_configs(&block)
48
+ next_token = nil
49
+ result = {}
50
+
51
+ while(true)
52
+
53
+ fetch_options = {
54
+ path: path,
55
+ with_decryption: false,
56
+ recursive: true,
57
+ max_results: 10
58
+ }
59
+
60
+ fetch_options[:next_token] = next_token if next_token
61
+
62
+ response = client.get_parameters_by_path(fetch_options)
63
+
64
+ items = sanitize_configs(response.parameters, &block)
65
+ result.merge!(items)
66
+
67
+ next_token = response.next_token
68
+ break if next_token.nil?
69
+ end
70
+
71
+ result
72
+ end
73
+
74
+ def sanitize_configs(parameters, &block)
75
+ items = {}
76
+ parameters.each do |param|
77
+ name = param.name.gsub("#{path}/", '')
78
+ value = param.value
79
+ item = {name: name, value: value}
80
+ items[name] = value
81
+ yield(item) if block_given?
82
+ end
83
+ items
84
+ end
85
+ end
86
+
87
+ end
88
+ end
@@ -1,15 +1,15 @@
1
- module EnvConfiguration
2
- module Adapter
3
- attr_accessor :options
4
-
5
- class Base
6
- def initialize(options={})
7
- @options = options.clone
8
- end
9
-
10
- def update_env_variable_with(configs)
11
- ENV.update(configs)
12
- end
13
- end
14
- end
1
+ module EnvConfiguration
2
+ module Adapter
3
+ attr_accessor :options
4
+
5
+ class Base
6
+ def initialize(options={})
7
+ @options = options.clone
8
+ end
9
+
10
+ def update_env_variable_with(configs)
11
+ ENV.update(configs)
12
+ end
13
+ end
14
+ end
15
15
  end
@@ -1,25 +1,25 @@
1
- require 'dotenv'
2
-
3
- module EnvConfiguration
4
- module Adapter
5
- class DotEnvAdapter < Base
6
-
7
- def initialize(options={})
8
- super(options)
9
- end
10
-
11
- def file
12
- @options[:dot_env_file] || EnvConfiguration.configuration.dot_env_file
13
- end
14
-
15
- def load
16
- p file
17
- if file.nil?
18
- Dotenv.load
19
- else
20
- Dotenv.load(file)
21
- end
22
- end
23
- end
24
- end
25
- end
1
+ require 'dotenv'
2
+
3
+ module EnvConfiguration
4
+ module Adapter
5
+ class DotEnvAdapter < Base
6
+
7
+ def initialize(options={})
8
+ super(options)
9
+ end
10
+
11
+ def file
12
+ @options[:dot_env_file] || EnvConfiguration.configuration.dot_env_file
13
+ end
14
+
15
+ def load
16
+ p file
17
+ if file.nil?
18
+ Dotenv.load
19
+ else
20
+ Dotenv.load(file)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,34 +1,34 @@
1
- require 'yaml'
2
-
3
- module EnvConfiguration
4
- module Adapter
5
- class YamlAdapter < Base
6
-
7
- def initialize(options = {})
8
- super(options)
9
- end
10
-
11
- def section
12
- section_value = @options[:section] || EnvConfiguration.configuration.yaml_section
13
- raise ":section in the options{} is required, for example :staging, :test, :production" if section_value.nil?
14
- section_value
15
- end
16
-
17
- def yaml_file
18
- yaml_file_value = @options[:yaml_file] || EnvConfiguration.configuration.yaml_file
19
- raise ":yaml_file in the options{} is required, for example config/application.yml" if yaml_file_value.nil?
20
- yaml_file_value
21
- end
22
-
23
- def load
24
- configs = fetch_configs
25
- update_env_variable_with(configs)
26
- configs
27
- end
28
-
29
- def fetch_configs
30
- ::YAML.load_file(yaml_file)[section]
31
- end
32
- end
33
- end
34
- end
1
+ require 'yaml'
2
+
3
+ module EnvConfiguration
4
+ module Adapter
5
+ class YamlAdapter < Base
6
+
7
+ def initialize(options = {})
8
+ super(options)
9
+ end
10
+
11
+ def section
12
+ section_value = @options[:section] || EnvConfiguration.configuration.yaml_section
13
+ raise ":section in the options{} is required, for example :staging, :test, :production" if section_value.nil?
14
+ section_value
15
+ end
16
+
17
+ def yaml_file
18
+ yaml_file_value = @options[:yaml_file] || EnvConfiguration.configuration.yaml_file
19
+ raise ":yaml_file in the options{} is required, for example config/application.yml" if yaml_file_value.nil?
20
+ yaml_file_value
21
+ end
22
+
23
+ def load
24
+ configs = fetch_configs
25
+ update_env_variable_with(configs)
26
+ configs
27
+ end
28
+
29
+ def fetch_configs
30
+ ::YAML.load_file(yaml_file)[section]
31
+ end
32
+ end
33
+ end
34
+ end