class SecretKeys::CLI::Encrypt

Public Instance Methods

action_name() click to toggle source
# File lib/secret_keys/cli.rb, line 182
def action_name
  "encrypt"
end
parse_additional_options(opts) click to toggle source
# 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(<<~DOC)) do |value|
        Encryption key used to encrypt strings in the file on output.
        This option can be used to change the encryption key. If set to '-', read from STDIN.
      DOC
        @new_secret_key = get_secret_key(value)
      end

      @in_place = false
      opts.on("-i", "--in-place", "Update the input file instead of writing to stdout.") do |value|
        @in_place = true
      end

      @encrypt_all = false
      opts.on("--encrypt-all", "Encrypt all keys in the file") do |value|
        @encrypt_all = value
      end
    end
run!() click to toggle source
# 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.open(@input, "w") do |file|
      file.write(contents)
    end
  else
    $stdout.write(encrypted_file_contents)
    $stdout.flush
  end
end