Set KAMAL_DESTINATION for dotenv parsing

This commit is contained in:
Donal McBreen
2024-08-07 14:45:47 +01:00
committed by Donal McBreen
parent 5480b40ba3
commit fcdef5fa06

View File

@@ -1,24 +1,29 @@
class Kamal::Configuration::Secrets
attr_reader :secret_files
attr_reader :secret_file, :destination
def initialize(destination: nil)
@secret_files = \
(destination ? [ ".kamal/secrets.#{destination}", ".kamal/secrets" ] : [ ".kamal/secrets" ])
@destination = destination
@secret_file = (destination ? [ ".kamal/secrets.#{destination}", ".kamal/secrets" ] : [ ".kamal/secrets" ])
.find { |file| File.exist?(file) }
end
def [](key)
@secrets ||= load
@secrets.fetch(key)
rescue KeyError
if secret_files.any?
raise Kamal::ConfigurationError, "Secret '#{key}' not found in #{secret_files.join(', ')}"
if secret_file
raise Kamal::ConfigurationError, "Secret '#{key}' not found in #{secret_file}"
else
raise Kamal::ConfigurationError, "Secret '#{key}' not found, no secret files provided"
raise Kamal::ConfigurationError, "Secret '#{key}' not found, no secret file provided"
end
end
private
def load
secret_files.any? ? Dotenv.parse(*secret_files) : {}
original_env = ENV.to_hash
ENV["KAMAL_DESTINATION"] = destination if destination
secret_file ? Dotenv.parse(*secret_file) : {}
ensure
ENV.replace(original_env)
end
end