class SecretKeys::CLI::Edit

Attributes

actions[R]

Public Instance Methods

action_name() click to toggle source
# File lib/secret_keys/cli.rb, line 274
def action_name
  "edit"
end
parse_additional_options(opts) click to toggle source
# File lib/secret_keys/cli.rb, line 278
    def parse_additional_options(opts)
      opts.separator("\nEdit options:")

      @actions = []
      set_encrypted_docs = split(<<~HELP)
        Set an encrypted value in the file. You can use dot notation to set a nested value.
        If no VALUE is specified, the key will be moved to the encrypted keys while keeping any existing value.
      HELP
      opts.on("-e", "--set-encrypted KEY[=VALUE]", String, *set_encrypted_docs) do |value|
        key, val = value.split("=", 2)
        @actions << [:encrypt, key, val]
      end

      set_decrypted_docs = split(<<~HELP)
        Set a plain text value in the file. You can use dot notation to set a nested value. If no VALUE is specified,
        the key will be moved to the plain text keys while keeping any existing value.
      HELP
      opts.on("-d", "--set-decrypted KEY[=VALUE]", String, *set_decrypted_docs) do |value|
        key, val = value.split("=", 2)
        @actions << [:decrypt, key, val]
      end

      opts.on("-r", "--remove KEY", String, "Remove a key from the file. You can use dot notation to remove a nested value.") do |value|
        @actions << [:remove, value, nil]
      end

      super
    end
run!() click to toggle source
Calls superclass method SecretKeys::CLI::Encrypt#run!
# File lib/secret_keys/cli.rb, line 307
def run!
  @actions.each do |action, key, value|
    raise ArgumentError.new("cannot set a key beginning with dot") if key.start_with?(".")
    case action
    when :encrypt
      secrets.encrypt!(key.split(".").first)
      unless value.nil?
        access_key(secrets, key, write: true) do |parent, child|
          parent[child] = value
        end
      end
    when :decrypt
      secrets.decrypt!(key.split(".").first)
      unless value.nil?
        access_key(secrets, key, write: true) do |parent, child|
          parent[child] = value
        end
      end
    when :remove
      access_key(secrets, key) do |parent, child|
        if parent.is_a?(Array)
          parent.delete_at(child)
        else
          parent.delete(child)
        end
      end
    end
  end

  super
end