Class: Envault::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/envault/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(args = [], options = {}, config = {}) ⇒ CLI

Returns a new instance of CLI.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/envault/cli.rb', line 16

def initialize(args = [], options = {}, config = {})
  super(args, options, config)
  @class_options = config[:shell].base.options
  current_command = config[:current_command].name
  unless SKIP_INITIALIZE_COMMANDS.include?(current_command)
    @core = Core.new(
      config: @class_options[:config],
      profile: @class_options[:profile],
      prefix: @class_options[:prefix],
      debug: @class_options[:debug]
    )
    @logger = @core.logger
  end
end

Instance Method Details

#decryptObject



39
40
41
# File 'lib/envault/cli.rb', line 39

def decrypt
  puts @core.cryptor.decrypt_and_verify(options[:source])
end

#decrypt_fileObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/envault/cli.rb', line 96

def decrypt_file
  result = {}
  options[:plain_text].each do |plain_text_path|
    result = result.merge(YAML.load(ERB.new(File.read(plain_text_path)).result))
  end
  options[:source].each do |encrypt_yaml_path|
    result = result.merge(@core.decrypt_yaml(encrypt_yaml_path))
  end
  if options[:output]
    Formatter.write_escape_yaml(options[:output], result)
  else
    puts Formatter.escape_yaml(result)
  end
end

#encryptObject



33
34
35
# File 'lib/envault/cli.rb', line 33

def encrypt
  puts @core.cryptor.encrypt_and_sign(options[:source])
end

#encrypt_fileObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/envault/cli.rb', line 77

def encrypt_file
  result = {}
  options[:plain_text].each do |plain_text_path|
    result = result.merge(YAML.load(ERB.new(File.read(plain_text_path)).result))
  end
  options[:source].each do |secret_yaml_path|
    result = result.merge(@core.encrypt_yaml(secret_yaml_path, options[:keys]))
  end
  if options[:output]
    Formatter.write_escape_yaml(options[:output], result)
  else
    puts Formatter.escape_yaml(result)
  end
end

#loadObject



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/envault/cli.rb', line 114

def load
  options[:sources].each do |source|
    begin
      @core.load(source)
    rescue => e
      raise "error => [#{source}]\n#{e.message}\n#{e.backtrace.join("\n")}"
    end
  end
  @logger.debug(ENV)
  exec(options[:command]) if options[:command]
end

#reencrypt_fileObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/envault/cli.rb', line 48

def reencrypt_file
  yaml = YAML.load_file(options[:source])
  from = Core.new(
    config: @class_options[:config],
    profile: options[:from_profile],
    prefix: @class_options[:prefix],
    debug: @class_options[:debug]
  )
  cipher_keys = from.get_cipher_keys(yaml).map{ |cipher_key| cipher_key.gsub(/^#{from.prefix}/, '') }
  decrypted = from.decrypt_yaml(options[:source])
  to = Core.new(
    config: @class_options[:config],
    profile: options[:to_profile],
    prefix: @class_options[:prefix],
    debug: @class_options[:debug]
  )
  output = to.encrypt_process(decrypted, cipher_keys)
  if options[:overwrite]
    Formatter.write_escape_yaml(options[:source], output)
  else
    puts Formatter.escape_yaml(output)
  end
end