Class: Credman::Delete

Inherits:
Base
  • Object
show all
Defined in:
lib/credman/delete.rb

Instance Method Summary collapse

Methods inherited from Base

#config_has_keys?, #configs, #decript, #initialize, #key_for, #pastel, #print_key_and_value, #rewrite_config_for

Constructor Details

This class inherits a constructor from Credman::Base

Instance Method Details

#deep_delete!(obj, keys) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/credman/delete.rb', line 27

def deep_delete!(obj, keys)
  key = keys.first
  if keys.length == 1
    obj.delete(key)
  else
    obj[key] = {} unless obj[key]
    deep_delete!(obj[key], keys.slice(1..-1))
  end
end

#normalize_new_value(new_value) ⇒ Object



37
38
39
40
41
# File 'lib/credman/delete.rb', line 37

def normalize_new_value(new_value)
  return if new_value.in?(%w[nil null])

  new_value
end

#perform(keys) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/credman/delete.rb', line 3

def perform(keys)
  abort pastel.red("At least one key required") if keys.empty?

  configs.each do |env, config|
    puts pastel.green("#{env}:")

    updated_config = config.dup
    is_updated = false

    keys_with_path = keys.index_with { |key| key.split(".").map(&:to_sym) }
    keys_with_path.each do |full_key, key_path|
      if config_has_keys?(config, key_path)
        is_updated ||= true
        deep_delete!(updated_config, key_path)
        print pastel.blue("\t#{full_key}:\t"), pastel.green("✅ deleted\n")
      else
        print pastel.blue("\t#{full_key}:\t"), pastel.red("❌ key not found, can't delete\n")
      end
    end

    rewrite_config_for(env, updated_config) if is_updated
  end
end