Class: EacCli::OldConfigs::EntryReader

Inherits:
Object
  • Object
show all
Defined in:
lib/eac_cli/old_configs/entry_reader.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.entry_key_to_envvar_name(entry_key) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/eac_cli/old_configs/entry_reader.rb', line 9

def entry_key_to_envvar_name(entry_key)
  path = if entry_key.is_a?(::Array)
           entry_key
         else
           ::EacConfig::PathsHash.parse_entry_key(entry_key)
         end
  path.join('_').gsub(/[^a-z0-9_]/i, '').gsub(/\A_+/, '').gsub(/_+\z/, '')
    .gsub(/_{2,}/, '_').upcase
end

Instance Method Details

#readObject



24
25
26
27
28
29
30
31
32
# File 'lib/eac_cli/old_configs/entry_reader.rb', line 24

def read
  %w[envvars storage console].each do |suffix|
    value = send("read_from_#{suffix}")
    return value if value.present?
  end
  return nil unless options[:required]

  raise "No value found for entry \"#{entry_key}\""
end

#read_from_consoleObject



47
48
49
50
51
52
53
54
# File 'lib/eac_cli/old_configs/entry_reader.rb', line 47

def read_from_console
  return if options[:noinput]

  options[:before_input].if_present(&:call)
  entry_value = looped_entry_value_from_input
  console_configs.configs.write_entry(entry_key, entry_value) if options[:store]
  entry_value
end

#read_from_envvarsObject



38
39
40
41
42
43
44
45
# File 'lib/eac_cli/old_configs/entry_reader.rb', line 38

def read_from_envvars
  return if options[:noenv]

  env_entry_key = self.class.entry_key_to_envvar_name(entry_key)
  return unless ENV.key?(env_entry_key)

  ENV.fetch(env_entry_key).if_present(::EacRubyUtils::BlankNotBlank.instance)
end

#read_from_storageObject



34
35
36
# File 'lib/eac_cli/old_configs/entry_reader.rb', line 34

def read_from_storage
  console_configs.configs.read_entry(entry_key)
end