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 class Kamal::Configuration::Secrets
attr_reader :secret_files attr_reader :secret_file, :destination
def initialize(destination: nil) def initialize(destination: nil)
@secret_files = \ @destination = destination
(destination ? [ ".kamal/secrets.#{destination}", ".kamal/secrets" ] : [ ".kamal/secrets" ]) @secret_file = (destination ? [ ".kamal/secrets.#{destination}", ".kamal/secrets" ] : [ ".kamal/secrets" ])
.find { |file| File.exist?(file) }
end end
def [](key) def [](key)
@secrets ||= load @secrets ||= load
@secrets.fetch(key) @secrets.fetch(key)
rescue KeyError rescue KeyError
if secret_files.any? if secret_file
raise Kamal::ConfigurationError, "Secret '#{key}' not found in #{secret_files.join(', ')}" raise Kamal::ConfigurationError, "Secret '#{key}' not found in #{secret_file}"
else 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
end end
private private
def load 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
end end