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.



277
278
279
# File 'lib/secret_keys/cli.rb', line 277

def actions
  @actions
end

Instance Method Details

#action_nameObject



279
280
281
# File 'lib/secret_keys/cli.rb', line 279

def action_name
  "edit"
end

#parse_additional_options(opts) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/secret_keys/cli.rb', line 283

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



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/secret_keys/cli.rb', line 312

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