From 3f99353a3f29422e284533ca3b7df3f09b5e2d33 Mon Sep 17 00:00:00 2001 From: Philip Vieira Date: Wed, 28 Jan 2015 00:03:27 +0000 Subject: [PATCH 1/2] Let the FIGARO_FILE_PATH environment variable determine location of configuration YAML - Only works in the context of rails - It takes precedence over the default `config/application.yml` - Add README and include other examples of how you can customise your file path --- README.md | 21 +++++++++++++++++++++ lib/figaro/rails/application.rb | 6 +++++- spec/figaro/rails/application_spec.rb | 22 ++++++++++++++++++++-- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c47d56ca..029b4ac0 100644 --- a/README.md +++ b/README.md @@ -143,6 +143,27 @@ Pusher.key = Figaro.env.pusher_key! Pusher.secret = Figaro.env.pusher_secret! ``` +### Custom file location + +Say your deployment process require you to have your configuration at a certain file path or that you want to provide defaults for different platforms in development. +You can achieve this by using an environment variable to tell Figaro where to look for your yaml file. + +```bash +$ docker run -e FIGARO_FILE_PATH=./config/application.docker.yml my-container rails s +``` + +```bash +$ FIGARO_FILE_PATH=/storage_device/configuration/my_app_variables.yml rails s +``` + +If you're not using rails or if you want to determine the file path by other means, set the `Figaro.adapter.path` to your desired value. + +```ruby +require "figaro" +Figaro.adapter.path = "/my/config/file.yml" +Figaro.load +``` + ### Deployment Figaro is written with deployment in mind. In fact, [Heroku](https://www.heroku.com)'s use of `ENV` for application configuration was the original inspiration for Figaro. diff --git a/lib/figaro/rails/application.rb b/lib/figaro/rails/application.rb index 34ff5aba..88d1b385 100644 --- a/lib/figaro/rails/application.rb +++ b/lib/figaro/rails/application.rb @@ -6,7 +6,11 @@ class Application < Figaro::Application def default_path rails_not_initialized! unless ::Rails.root - ::Rails.root.join("config", "application.yml") + if ::ENV["FIGARO_FILE_PATH"] + ::Rails.root.join(::ENV["FIGARO_FILE_PATH"]) + else + ::Rails.root.join("config", "application.yml") + end end def default_environment diff --git a/spec/figaro/rails/application_spec.rb b/spec/figaro/rails/application_spec.rb index fa49df78..d976e7c3 100644 --- a/spec/figaro/rails/application_spec.rb +++ b/spec/figaro/rails/application_spec.rb @@ -4,9 +4,9 @@ module Rails describe "#default_path" do let!(:application) { Application.new } - it "defaults to config/application.yml in Rails.root" do - allow(::Rails).to receive(:root) { Pathname.new("/path/to/app") } + before { allow(::Rails).to receive(:root) { Pathname.new("/path/to/app") } } + it "defaults to config/application.yml in Rails.root" do expect { allow(::Rails).to receive(:root) { Pathname.new("/app") } }.to change { @@ -14,6 +14,24 @@ module Rails }.from("/path/to/app/config/application.yml").to("/app/config/application.yml") end + it "defaults to path relative to Rails.root from the FIGARO_FILE_PATH environment variable" do + expect { + ::ENV["FIGARO_FILE_PATH"] = "./config/application.docker.yml" + }.to change { + application.send(:default_path).to_s + }.from("/path/to/app/config/application.yml").to("/path/to/app/config/application.docker.yml") + ::ENV["FIGARO_FILE_PATH"] = nil + end + + it "defaults to absolute path from the FIGARO_FILE_PATH environment variable" do + expect { + ::ENV["FIGARO_FILE_PATH"] = "/var/config/my-app.yml" + }.to change { + application.send(:default_path).to_s + }.from("/path/to/app/config/application.yml").to("/var/config/my-app.yml") + ::ENV["FIGARO_FILE_PATH"] = nil + end + it "raises an error when Rails.root isn't set yet" do allow(::Rails).to receive(:root) { nil } From 71512a839820c9d84cf200cfc5ab3e17985afef5 Mon Sep 17 00:00:00 2001 From: Philip Vieira Date: Wed, 28 Jan 2015 01:04:59 +0000 Subject: [PATCH 2/2] Use FIGARO_PATH as suggested in #178 https://github.com/laserlemon/figaro/pull/178#issuecomment-71761138 --- README.md | 4 ++-- lib/figaro/rails/application.rb | 4 ++-- spec/figaro/rails/application_spec.rb | 12 ++++++------ 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 029b4ac0..eedd257b 100644 --- a/README.md +++ b/README.md @@ -149,11 +149,11 @@ Say your deployment process require you to have your configuration at a certain You can achieve this by using an environment variable to tell Figaro where to look for your yaml file. ```bash -$ docker run -e FIGARO_FILE_PATH=./config/application.docker.yml my-container rails s +$ docker run -e FIGARO_PATH=./config/application.docker.yml my-container rails s ``` ```bash -$ FIGARO_FILE_PATH=/storage_device/configuration/my_app_variables.yml rails s +$ FIGARO_PATH=/storage_device/configuration/my_app_variables.yml rails s ``` If you're not using rails or if you want to determine the file path by other means, set the `Figaro.adapter.path` to your desired value. diff --git a/lib/figaro/rails/application.rb b/lib/figaro/rails/application.rb index 88d1b385..5326939d 100644 --- a/lib/figaro/rails/application.rb +++ b/lib/figaro/rails/application.rb @@ -6,8 +6,8 @@ class Application < Figaro::Application def default_path rails_not_initialized! unless ::Rails.root - if ::ENV["FIGARO_FILE_PATH"] - ::Rails.root.join(::ENV["FIGARO_FILE_PATH"]) + if ::ENV["FIGARO_PATH"] + ::Rails.root.join(::ENV["FIGARO_PATH"]) else ::Rails.root.join("config", "application.yml") end diff --git a/spec/figaro/rails/application_spec.rb b/spec/figaro/rails/application_spec.rb index d976e7c3..4787202b 100644 --- a/spec/figaro/rails/application_spec.rb +++ b/spec/figaro/rails/application_spec.rb @@ -14,22 +14,22 @@ module Rails }.from("/path/to/app/config/application.yml").to("/app/config/application.yml") end - it "defaults to path relative to Rails.root from the FIGARO_FILE_PATH environment variable" do + it "defaults to path relative to Rails.root from the FIGARO_PATH environment variable" do expect { - ::ENV["FIGARO_FILE_PATH"] = "./config/application.docker.yml" + ::ENV["FIGARO_PATH"] = "./config/application.docker.yml" }.to change { application.send(:default_path).to_s }.from("/path/to/app/config/application.yml").to("/path/to/app/config/application.docker.yml") - ::ENV["FIGARO_FILE_PATH"] = nil + ::ENV["FIGARO_PATH"] = nil end - it "defaults to absolute path from the FIGARO_FILE_PATH environment variable" do + it "defaults to absolute path from the FIGARO_PATH environment variable" do expect { - ::ENV["FIGARO_FILE_PATH"] = "/var/config/my-app.yml" + ::ENV["FIGARO_PATH"] = "/var/config/my-app.yml" }.to change { application.send(:default_path).to_s }.from("/path/to/app/config/application.yml").to("/var/config/my-app.yml") - ::ENV["FIGARO_FILE_PATH"] = nil + ::ENV["FIGARO_PATH"] = nil end it "raises an error when Rails.root isn't set yet" do