Class: Credman::Base

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

Direct Known Subclasses

Conflicts, Delete, Diff, Get, List, Set

Instance Method Summary collapse

Constructor Details

#initialize(environment_list) ⇒ Base

Returns a new instance of Base.



3
4
5
6
# File 'lib/credman/base.rb', line 3

def initialize(environment_list)
  @environment_list = environment_list.keep_if { |env| env.in?(Credman.config.available_environments) }
  abort pastel.red("No valid environments specified. Valid example: `-e development,test`") if @environment_list.empty?
end

Instance Method Details

#config_has_keys?(config, keys_path) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
# File 'lib/credman/base.rb', line 31

def config_has_keys?(config, keys_path)
  dig_keys = keys_path[0...-1]
  return config.key?(keys_path.first) if dig_keys.empty?

  config.dig(*dig_keys)&.key?(keys_path.last)
end

#configsObject



8
9
10
# File 'lib/credman/base.rb', line 8

def configs
  @configs ||= @environment_list.inject({}) { |conf, env| conf.merge({env => config_for(env)}) }
end

#decript(key, content) ⇒ Object



49
50
51
52
# File 'lib/credman/base.rb', line 49

def decript(key, content)
  ActiveSupport::MessageEncryptor.new([key].pack("H*"), cipher: "aes-128-gcm")
    .decrypt_and_verify(content)
end

#key_for(environment) ⇒ Object



45
46
47
# File 'lib/credman/base.rb', line 45

def key_for(environment)
  ENV["RAILS_MASTER_KEY"] || Pathname.new("config/credentials/#{environment}.key").binread.strip
end

#pastelObject



12
13
14
# File 'lib/credman/base.rb', line 12

def pastel
  @pastel ||= Pastel.new
end


16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/credman/base.rb', line 16

def print_key_and_value(key, value)
  print pastel.blue("\t#{key}:\t")

  case value
  when "not set"
    print pastel.red("not set")
  when ->(v) { v.is_a?(String) }
    print pastel.yellow(value)
  else
    print pastel.yellow(value.inspect)
  end

  print "\n"
end

#rewrite_config_for(environment, new_config) ⇒ Object



38
39
40
41
42
43
# File 'lib/credman/base.rb', line 38

def rewrite_config_for(environment, new_config)
  # removes "---\n" in the very beginning
  config_as_string = new_config.deep_stringify_keys.to_yaml[4..]

  encrypted_configuration(environment).write(config_as_string)
end