Skip to content

Commit

Permalink
inherit env from ENV, and prefer ENV to .env
Browse files Browse the repository at this point in the history
  • Loading branch information
NAR8789 committed Dec 7, 2023
1 parent f1b8018 commit dba8a50
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 8 deletions.
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
7 changes: 7 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 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

0 comments on commit dba8a50

Please sign in to comment.