Allow dynamic accessory files to reference declared ENVs

This commit is contained in:
David Heinemeier Hansson
2023-02-01 14:45:56 +01:00
parent 4c8b1a3e04
commit 3f44e25b63
3 changed files with 12 additions and 3 deletions

View File

@@ -74,12 +74,19 @@ class Mrsk::Configuration::Assessory
def expand_local_file(local_file)
if local_file.end_with?("erb")
read_dynamic_file(local_file)
with_clear_env_loaded { read_dynamic_file(local_file) }
else
Pathname.new(File.expand_path(local_file)).to_s
end
end
def with_clear_env_loaded
(env["clear"] || env).each { |k, v| ENV[k] = v }
yield
ensure
(env["clear"] || env).each { |k, v| ENV.delete(k) }
end
def read_dynamic_file(local_file)
StringIO.new(ERB.new(IO.read(local_file)).result)
end

View File

@@ -98,7 +98,8 @@ class ConfigurationAccessoryTest < ActiveSupport::TestCase
@deploy[:accessories]["mysql"]["files"] << "test/fixtures/files/structure.sql.erb:/docker-entrypoint-initdb.d/structure.sql"
@config = Mrsk::Configuration.new(@deploy)
assert_equal "This was dynamically expanded", @config.accessory(:mysql).files.keys[2].read
assert_match "This was dynamically expanded", @config.accessory(:mysql).files.keys[2].read
assert_match "%", @config.accessory(:mysql).files.keys[2].read
end
test "directories" do

View File

@@ -1 +1,2 @@
<%= "This was dynamically expanded" %>
<%= ENV["MYSQL_ROOT_HOST"] %>