Module: Ironclad::Diff

Defined in:
lib/ironclad/diff.rb

Constant Summary collapse

ENV_KEY =
'IRONCLAD_DIFF_KEY'

Class Method Summary collapse

Class Method Details

.call(path, out = $stdout) ⇒ Object



12
13
14
# File 'lib/ironclad/diff.rb', line 12

def call(path, out = $stdout)
  out.write(decrypt(path) || File.read(path))
end

.decrypt(path) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ironclad/diff.rb', line 16

def decrypt(path)
  key = Ironclad.key(environment_for(path))
  return unless key

  ENV[ENV_KEY] = key
  ActiveSupport::EncryptedFile.new(
    content_path: path, key_path: File::NULL,
    env_key: ENV_KEY, raise_if_missing_key: true
  ).read
rescue Ironclad::Error, ActiveSupport::MessageEncryptor::InvalidMessage
  nil
end

.environment_for(path) ⇒ Object



29
30
31
32
# File 'lib/ironclad/diff.rb', line 29

def environment_for(path)
  env = File.basename(path).delete_suffix('.yml.enc')
  Ironclad.config.key?(env) ? env : 'default'
end