From 4d39fd3a8c2c8085a74617629cb305e81fd7e596 Mon Sep 17 00:00:00 2001 From: Glen Date: Wed, 18 Jun 2014 10:30:07 -0600 Subject: [PATCH 1/7] Add option to write configuration to log. --- .gitignore | 3 +++ lib/conf_conf.rb | 16 +++++++++++++++- spec/conf_conf_spec.rb | 17 ++++++++++++++++- 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7a8d370 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.bundle/ +Gemfile.lock +vendor/ diff --git a/lib/conf_conf.rb b/lib/conf_conf.rb index 53a434c..4a1fb4e 100644 --- a/lib/conf_conf.rb +++ b/lib/conf_conf.rb @@ -6,6 +6,8 @@ module ConfConf class MissingConfigurationValueError < StandardError; end; class << self + attr_accessor :log_config + def configuration(&block) OpenStruct.new(Configuration.new(&block).parsed_values) end @@ -17,6 +19,8 @@ def rails_configuration(&block) configuration.parsed_values.each do |key, value| Rails.configuration.send("#{key}=", value) end + + ConfigLogger.new.dump_config configuration if log_config end end @@ -46,7 +50,7 @@ def config(key, options={}) class Reference < Struct.new(:key, :options) def value environment_value || default_value - end + end private def default_value @@ -65,4 +69,14 @@ def environment_key options[:from] || key.to_s.upcase end end + + class ConfigLogger + def dump_config(configuration) + Rails.logger.info "Application Configuration\n\n#{format(configuration)}" + end + + def format(configuration) + configuration.parsed_values.collect { |pair| "#{pair[0]}: #{pair[1]}" }.join("\n") + end + end end diff --git a/spec/conf_conf_spec.rb b/spec/conf_conf_spec.rb index 5ebf878..faef3d9 100644 --- a/spec/conf_conf_spec.rb +++ b/spec/conf_conf_spec.rb @@ -8,7 +8,7 @@ module Rails; end; ENV["TEST_KEY"] = "hey" configuration = ConfConf.configuration { - config :test_key + config :test_key } expect(configuration.test_key).to eq("hey") @@ -64,5 +64,20 @@ module Rails; end; end end end + + it "logs the configuration if told to" do + logger = double() + allow(logger).to receive(:info) + allow(Rails).to receive(:logger).and_return logger + expect(configuration).to receive(:integer_value=).with(2) + + ConfConf.log_config = true + + ConfConf.rails_configuration do + config :integer_value do |value| + value.to_i + end + end + end end end From 7f5bade3e175a3ae4726f470b0bc297345bcb956 Mon Sep 17 00:00:00 2001 From: James Kassemi Date: Wed, 18 Jun 2014 10:40:05 -0600 Subject: [PATCH 2/7] Rename --- README.md | 4 +- conf_conf.gemspec | 2 +- spec/conf_conf_spec.rb | 90 ++++++++++++++++-------------------------- 3 files changed, 37 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index cc90bf2..adac43d 100644 --- a/README.md +++ b/README.md @@ -40,8 +40,8 @@ config :public_key, default: "XYZ123" You can adjust the value from the environment and typecast it or perform additional validation by passing a block to `config`: ```ruby -config(:admin) { |admin| - admin ? true : false +config(:admin) { |admin| + admin ? true : false } ``` diff --git a/conf_conf.gemspec b/conf_conf.gemspec index d59c73d..5e4e962 100644 --- a/conf_conf.gemspec +++ b/conf_conf.gemspec @@ -6,7 +6,7 @@ Gem::Specification.new do |s| s.name = "conf_conf" s.version = "1.0.2" s.licenses = ["MIT"] - s.authors = ["James Kassemi"] + s.authors = ["James Kassemi"] s.email = ["jkassemi@gmail.com"] s.homepage = "https://github.com/jkassemi/conf_conf" s.description = "Verify correctness of environment variables" diff --git a/spec/conf_conf_spec.rb b/spec/conf_conf_spec.rb index faef3d9..6ed6198 100644 --- a/spec/conf_conf_spec.rb +++ b/spec/conf_conf_spec.rb @@ -3,80 +3,58 @@ module Rails; end; describe ConfConf do - context "#configuration" do - it "sets a value from the environment" do - ENV["TEST_KEY"] = "hey" + let(:configuration){ double() } + before { allow(Rails).to receive(:configuration).and_return(configuration) } - configuration = ConfConf.configuration { - config :test_key - } + it "sets a value from the environment" do + ENV["TEST_KEY"] = "hey" + expect(configuration).to receive(:test_key=).with("hey") - expect(configuration.test_key).to eq("hey") + ConfConf.rails_configuration do + config :test_key end end - context "#rails_configuration" do - let(:configuration){ double() } - before { allow(Rails).to receive(:configuration).and_return(configuration) } + it "sets the default value when key not present" do + expect(configuration).to receive(:key_not_present=).with("hey") - it "sets a value from the environment" do - ENV["TEST_KEY"] = "hey" - expect(configuration).to receive(:test_key=).with("hey") - - ConfConf.rails_configuration do - config :test_key - end + ConfConf.rails_configuration do + config :key_not_present, default: "hey" end + end - it "sets the default value when key not present" do - expect(configuration).to receive(:key_not_present=).with("hey") - - ConfConf.rails_configuration do - config :key_not_present, default: "hey" - end - end - - it "sets value from specified environment key" do - ENV["TEST_KEY"] = "hey" - expect(configuration).to receive(:other_key=).with("hey") - + it "throws an exception when required key not present" do + expect { ConfConf.rails_configuration do - config :other_key, from: "TEST_KEY" + config :key_not_present end - end - - it "throws an exception when required key not present" do - expect { - ConfConf.rails_configuration do - config :key_not_present - end - }.to raise_error(ConfConf::MissingConfigurationValueError) - end + }.to raise_error(ConfConf::MissingConfigurationValueError) + end - it "evaluates a block to establish the actual configuration value" do - ENV["INTEGER_VALUE"] = "2" + it "evaluates a block to establish the actual configuration value" do + ENV["INTEGER_VALUE"] = "2" - expect(configuration).to receive(:integer_value=).with(2) + expect(configuration).to receive(:integer_value=).with(2) - ConfConf.rails_configuration do - config :integer_value do |value| - value.to_i - end + ConfConf.rails_configuration do + config :integer_value do |value| + value.to_i end end + end - it "logs the configuration if told to" do - logger = double() - allow(logger).to receive(:info) - allow(Rails).to receive(:logger).and_return logger - expect(configuration).to receive(:integer_value=).with(2) + it "logs the configuration if told to" do + logger = double() + allow(logger).to receive(:info) + allow(Rails).to receive(:logger).and_return logger + expect(configuration).to receive(:integer_value=).with(2) - ConfConf.log_config = true - ConfConf.rails_configuration do - config :integer_value do |value| - value.to_i - end + ConfConf.log_config = true + + ConfConf.rails_configuration do + config :integer_value do |value| + value.to_i end end end From 5c22dd5425ada8a30b48055837ab2adf11f62d79 Mon Sep 17 00:00:00 2001 From: James Kassemi Date: Wed, 18 Jun 2014 11:05:21 -0600 Subject: [PATCH 3/7] Adds 'from' option to config, which allows varying names between configuration key and environment variable name --- spec/conf_conf_spec.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/spec/conf_conf_spec.rb b/spec/conf_conf_spec.rb index 6ed6198..c4d6f27 100644 --- a/spec/conf_conf_spec.rb +++ b/spec/conf_conf_spec.rb @@ -23,6 +23,15 @@ module Rails; end; end end + it "sets value from specified environment key" do + ENV["TEST_KEY"] = "hey" + expect(configuration).to receive(:other_key=).with("hey") + + ConfConf.rails_configuration do + config :other_key, from: "TEST_KEY" + end + end + it "throws an exception when required key not present" do expect { ConfConf.rails_configuration do From ff1153addd61782e46d18ea34df94f929e06c55e Mon Sep 17 00:00:00 2001 From: James Kassemi Date: Wed, 18 Jun 2014 16:14:01 -0600 Subject: [PATCH 4/7] Adds general configuration object Probably the better way to use the system, since Rails.configuration is _generally_ for Rails-specific application settings. --- README.md | 44 +++++++++++++++++++++++++- spec/conf_conf_spec.rb | 72 +++++++++++++++++++++++++----------------- 2 files changed, 86 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index adac43d..8bb1e69 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,6 @@ A utility for verifying the correctness of environment variables at application ## Add ConfConf to your Rails/Ruby Project - Add `gem 'conf_conf'` to your application's Gemfile and `bundle install` Better yet, go to https://rubygems.org/gems/conf_conf and configure a specific version requirement. @@ -64,3 +63,46 @@ ConfConf.rails_configuration do config :secret_key end ``` + +### Default Values + +```ruby +# Sets $configuration.public_key from ENV["PUBLIC_KEY"], or uses the default if not available in ENV +config :public_key, default: "XYZ123" +``` + +### Casting + +You can adjust the value from the environment and typecast it or perform +additional validation by passing a block to `config`: + +```ruby +# Sets $configuration.admin to a boolean value of true or false based on truthiness of ENV key, app fails to boot if not present +config(:admin) { |admin| + admin ? true : false +} +``` + +### Varying Names + +If you'd like to reference a configuration value with a different name, you can +use the `from` key as an option to `config` and pass it the name to expect from +the environment. + +```ruby +# Sets $configuration.public_key from ENV["PUBLIC_KEY_WITH_ALT_NAME"] +config :public_key, from: "PUBLIC_KEY_WITH_ALT_NAME" +``` + +## Rails.configuration + +To assign directly to Rails.configuration instead of CONFCONF, you can +use `ConfConf.rails_configuration` method. + +```ruby +# config/initializers/conf_conf.rb +ConfConf.rails_configuration do + # Sets Rails.configuration.secret_key + config :secret_key +end +``` diff --git a/spec/conf_conf_spec.rb b/spec/conf_conf_spec.rb index c4d6f27..6820a25 100644 --- a/spec/conf_conf_spec.rb +++ b/spec/conf_conf_spec.rb @@ -3,51 +3,65 @@ module Rails; end; describe ConfConf do - let(:configuration){ double() } - before { allow(Rails).to receive(:configuration).and_return(configuration) } + context "#configuration" do + it "sets a value from the environment" do + ENV["TEST_KEY"] = "hey" - it "sets a value from the environment" do - ENV["TEST_KEY"] = "hey" - expect(configuration).to receive(:test_key=).with("hey") + configuration = ConfConf.configuration { + config :test_key + } - ConfConf.rails_configuration do - config :test_key + expect(configuration.test_key).to eq("hey") end end - it "sets the default value when key not present" do - expect(configuration).to receive(:key_not_present=).with("hey") + context "#rails_configuration" do + let(:configuration){ double() } + before { allow(Rails).to receive(:configuration).and_return(configuration) } - ConfConf.rails_configuration do - config :key_not_present, default: "hey" + it "sets a value from the environment" do + ENV["TEST_KEY"] = "hey" + expect(configuration).to receive(:test_key=).with("hey") + + ConfConf.rails_configuration do + config :test_key + end end - end - it "sets value from specified environment key" do - ENV["TEST_KEY"] = "hey" - expect(configuration).to receive(:other_key=).with("hey") + it "sets the default value when key not present" do + expect(configuration).to receive(:key_not_present=).with("hey") - ConfConf.rails_configuration do - config :other_key, from: "TEST_KEY" + ConfConf.rails_configuration do + config :key_not_present, default: "hey" + end end - end - it "throws an exception when required key not present" do - expect { + it "sets value from specified environment key" do + ENV["TEST_KEY"] = "hey" + expect(configuration).to receive(:other_key=).with("hey") + ConfConf.rails_configuration do - config :key_not_present + config :other_key, from: "TEST_KEY" end - }.to raise_error(ConfConf::MissingConfigurationValueError) - end + end - it "evaluates a block to establish the actual configuration value" do - ENV["INTEGER_VALUE"] = "2" + it "throws an exception when required key not present" do + expect { + ConfConf.rails_configuration do + config :key_not_present + end + }.to raise_error(ConfConf::MissingConfigurationValueError) + end - expect(configuration).to receive(:integer_value=).with(2) + it "evaluates a block to establish the actual configuration value" do + ENV["INTEGER_VALUE"] = "2" - ConfConf.rails_configuration do - config :integer_value do |value| - value.to_i + expect(configuration).to receive(:integer_value=).with(2) + + ConfConf.rails_configuration do + config :integer_value do |value| + value.to_i + end end end end From e103dcbbe05c7dfefe19b45fcfe7ff0a69994c6c Mon Sep 17 00:00:00 2001 From: Glen Holcomb Date: Mon, 30 Jun 2014 14:42:54 -0600 Subject: [PATCH 5/7] Changing name to match new gem name. --- spec/conf_conf_spec.rb | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/spec/conf_conf_spec.rb b/spec/conf_conf_spec.rb index 6820a25..686f211 100644 --- a/spec/conf_conf_spec.rb +++ b/spec/conf_conf_spec.rb @@ -8,7 +8,7 @@ module Rails; end; ENV["TEST_KEY"] = "hey" configuration = ConfConf.configuration { - config :test_key + config :test_key } expect(configuration.test_key).to eq("hey") @@ -64,6 +64,21 @@ module Rails; end; end end end + + it "logs the configuration if told to" do + logger = double() + allow(logger).to receive(:info) + allow(Rails).to receive(:logger).and_return logger + expect(configuration).to receive(:integer_value=).with(2) + + ConfConf.log_config = true + + ConfConf.rails_configuration do + config :integer_value do |value| + value.to_i + end + end + end end it "logs the configuration if told to" do From 3f686b75c72b57737f12c4687cdd48be15de4ecc Mon Sep 17 00:00:00 2001 From: Glen Holcomb Date: Mon, 30 Jun 2014 15:06:17 -0600 Subject: [PATCH 6/7] Merge went wrong --- spec/conf_conf_spec.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/spec/conf_conf_spec.rb b/spec/conf_conf_spec.rb index 686f211..309bdfb 100644 --- a/spec/conf_conf_spec.rb +++ b/spec/conf_conf_spec.rb @@ -79,20 +79,20 @@ module Rails; end; end end end - end - it "logs the configuration if told to" do - logger = double() - allow(logger).to receive(:info) - allow(Rails).to receive(:logger).and_return logger - expect(configuration).to receive(:integer_value=).with(2) + it "logs the configuration if told to" do + logger = double() + allow(logger).to receive(:info) + allow(Rails).to receive(:logger).and_return logger + expect(configuration).to receive(:integer_value=).with(2) - ConfConf.log_config = true + ConfConf.log_config = true - ConfConf.rails_configuration do - config :integer_value do |value| - value.to_i + ConfConf.rails_configuration do + config :integer_value do |value| + value.to_i + end end end end From 2f164e154b99985ac767d72bbab605330e455ac7 Mon Sep 17 00:00:00 2001 From: Glen Holcomb Date: Mon, 30 Jun 2014 15:36:07 -0600 Subject: [PATCH 7/7] We should expect the logging to happen, not just allow it to happen. --- spec/conf_conf_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/conf_conf_spec.rb b/spec/conf_conf_spec.rb index 309bdfb..85c3250 100644 --- a/spec/conf_conf_spec.rb +++ b/spec/conf_conf_spec.rb @@ -82,8 +82,8 @@ module Rails; end; it "logs the configuration if told to" do logger = double() - allow(logger).to receive(:info) - allow(Rails).to receive(:logger).and_return logger + expect(logger).to receive(:info) + expect(Rails).to receive(:logger).and_return logger expect(configuration).to receive(:integer_value=).with(2)