Class: SecretKeys::CLI::Encrypt

Inherits:
Base
  • Object
show all
Defined in:
lib/secret_keys/cli.rb

Direct Known Subclasses

Edit

Constant Summary

Constants inherited from Base

Base::MAX_SUMMARY_LENGTH

Instance Attribute Summary

Attributes inherited from Base

#input, #secret_key

Instance Method Summary collapse

Methods inherited from Base

#format, #initialize, #secrets

Constructor Details

This class inherits a constructor from SecretKeys::CLI::Base

Instance Method Details

#action_nameObject



182
183
184
# File 'lib/secret_keys/cli.rb', line 182

def action_name
  "encrypt"
end

#parse_additional_options(opts) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/secret_keys/cli.rb', line 186

def parse_additional_options(opts)
  opts.separator("\nEncrypt options:")

  @new_secret_key = nil
  opts.on("--new-secret-key=NEW_SECRET", String, *split("    Encryption key used to encrypt strings in the file on output.\n    This option can be used to change the encryption key. If set to '-', read from STDIN.\n  DOC\n    @new_secret_key = get_secret_key(value)\n  end\n\n  @in_place = false\n  opts.on(\"-i\", \"--in-place\", \"Update the input file instead of writing to stdout.\") do |value|\n    @in_place = true\n  end\n\n  @encrypt_all = false\n  opts.on(\"--encrypt-all\", \"Encrypt all keys in the file\") do |value|\n    @encrypt_all = value\n  end\nend\n")) do |value|

#run!Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/secret_keys/cli.rb', line 208

def run!
  if @new_secret_key && !@new_secret_key.empty?
    secrets.encryption_key = @new_secret_key
  end

  if @encrypt_all
    secrets.each_key do |key|
      secrets.encrypt!(key)
    end
  end

  if @in_place
    raise ArgumentError, "Cannot perform in place editing on streams" unless @input.is_a?(String)
    # make sure we read the file **before** writing to it.
    contents = encrypted_file_contents
    File.write(@input, contents)
  else
    $stdout.write(encrypted_file_contents)
    $stdout.flush
  end
end