Class: SecretKeys::CLI::Edit

Inherits:
Encrypt show all
Defined in:
lib/secret_keys/cli.rb

Constant Summary

Constants inherited from Base

Base::MAX_SUMMARY_LENGTH

Instance Attribute Summary collapse

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 Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



270
271
272
# File 'lib/secret_keys/cli.rb', line 270

def actions
  @actions
end

Instance Method Details

#action_nameObject



272
273
274
# File 'lib/secret_keys/cli.rb', line 272

def action_name
  "edit"
end

#parse_additional_options(opts) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/secret_keys/cli.rb', line 276

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!Object



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/secret_keys/cli.rb', line 305

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