lib/secret_env.rb in secret_env-0.2.0 vs lib/secret_env.rb in secret_env-0.3.0

- old
+ new

@@ -8,35 +8,57 @@ def self.load(env: 'development') config = YAML.load_file(SECRETS_FILE).fetch(env) storage = Storage.setup(config['storage']) + env_map = {} Array(config.fetch('env')).each do |key, raw_value| - record = Record.new(key: key, raw_value: raw_value, storage: storage) - ENV[record.key] = record.value + record = Record.new(key: key, raw_value: raw_value, storage: storage, dependency: env_map) + env_map[record.key] = record end + + env_map.each do |key, record| + unless ENV.has_key?(record.key) + ENV[record.key] = record.value + end + end end class Record attr_reader :key - def initialize(key:, raw_value:, storage: Storage::Plain.new) + def initialize(key:, raw_value:, storage: Storage::Plain.new, dependency: {}) @key = key @raw_value = raw_value @storage = storage + @dependency = dependency end def value scanner = StringScanner.new(@raw_value) parts = [] while part = scanner.scan_until(/#\{(.*?)\}/) secret_key = scanner.matched[2..-2] # Extract "secret" from "\#{secret}" - secret = @storage.retrieve(secret_key) + + + secret = case + when ENV.has_key?(secret_key) + ENV[secret_key] + when @dependency.has_key?(secret_key) + # FIXME this code may cause infinite loop + @dependency[secret_key].value + else + @storage.retrieve(secret_key) + end + + raise SecretEnv::KeyNotFound unless secret parts << part.gsub(scanner.matched, secret) end parts << scanner.rest parts.join end end + + class KeyNotFound < StandardError; end end require 'secret_env/rails' if defined?(Rails)