Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer runtime env values over .env values #711

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -5,6 +5,7 @@ gemspec
gem 'thor', '0.19.4', :require => false

group :test do
gem 'climate_control'
gem 'rake'
gem 'fakefs'
gem 'rspec', '~> 3.5'
Expand Down
6 changes: 6 additions & 0 deletions lib/foreman/cli.rb
Expand Up @@ -37,6 +37,7 @@ def is_thor_reserved_word?(word, type)
def start(process=nil)
check_procfile!
load_environment!
inherit_environment!
engine.load_procfile(procfile)
engine.options[:formation] = "#{process}=1" if process
engine.start
Expand Down Expand Up @@ -80,6 +81,7 @@ def check

def run(*args)
load_environment!
inherit_environment!

if File.file?(procfile)
engine.load_procfile(procfile)
Expand Down Expand Up @@ -134,6 +136,10 @@ def check_procfile!
error("#{procfile} does not exist.") unless File.file?(procfile)
end

def inherit_environment!
engine.inherit_env
end

def load_environment!
if options[:env]
options[:env].split(",").each do |file|
Expand Down
16 changes: 15 additions & 1 deletion lib/foreman/engine.rb
Expand Up @@ -172,12 +172,26 @@ def load_procfile(filename)
self
end

# Load ENV into the +env+ for this +Engine+
#
def inherit_env
merge_env(ENV)
end

# Load a .env file into the +env+ for this +Engine+
#
# @param [String] filename A .env file to load into the environment
#
def load_env(filename)
Foreman::Env.new(filename).entries do |name, value|
merge_env(Foreman::Env.new(filename).entries)
end

# Load a hash or entries list into the +env+ for this +Engine+
#
# @param [Hash] entries A Hash or an entries list to load into the environment
#
def merge_env(entries)
entries.each do |name, value|
@env[name] = value
end
end
Expand Down
7 changes: 0 additions & 7 deletions lib/foreman/env.rb
Expand Up @@ -19,11 +19,4 @@ def initialize(filename)
ax
end
end

def entries
@entries.each do |key, value|
yield key, value
end
end

end
6 changes: 6 additions & 0 deletions spec/foreman/cli_spec.rb
Expand Up @@ -88,6 +88,12 @@
expect(forked_foreman("run -e #{resource_path(".env")} #{resource_path("bin/env FOO")}")).to eq("bar\n")
end

it "prefers ENV to .env" do
ClimateControl.modify FOO: 'qux' do
expect(forked_foreman("run -e #{resource_path(".env")} #{resource_path("bin/env FOO")}")).to eq("qux\n")
end
end

it "can run a command from the Procfile" do
expect(forked_foreman("run -f #{resource_path("Procfile")} test")).to eq("testing\n")
end
Expand Down
15 changes: 15 additions & 0 deletions spec/foreman/engine_spec.rb
Expand Up @@ -61,6 +61,13 @@ def shutdown
end

describe "environment" do
it "should read ENV" do
ClimateControl.modify FOO: 'baz' do
subject.inherit_env
expect(subject.env["FOO"]).to eq("baz")
end
end

it "should read env files" do
write_file("/tmp/env") { |f| f.puts("FOO=baz") }
subject.load_env("/tmp/env")
Expand All @@ -76,6 +83,14 @@ def shutdown
expect(subject.env["BAZ"]).to eq("qux")
end

it "should prefer later versions of values" do
write_file("/tmp/env1") { |f| f.puts("FOO=bar") }
write_file("/tmp/env2") { |f| f.puts("FOO=qux") }
subject.load_env "/tmp/env1"
subject.load_env "/tmp/env2"
expect(subject.env["FOO"]).to eq("qux")
end

it "should handle quoted values" do
write_file("/tmp/env") do |f|
f.puts 'FOO=bar'
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -8,6 +8,7 @@
require "pp"
require "fakefs/safe"
require "fakefs/spec_helpers"
require 'climate_control'

$:.unshift File.expand_path("../../lib", __FILE__)

Expand Down