Module: OCI::ConfigFileLoader

Defined in:
lib/oci/config_file_loader.rb

Overview

Module for loading Config from a file

Defined Under Namespace

Modules: Errors

Constant Summary collapse

DEFAULT_PROFILE =

Name of the default profile to load from a config file

'DEFAULT'.freeze
DEFAULT_CONFIG_FILE =

Default config file name and location

"#{Dir.home}/.oci/config".freeze
FALLBACK_DEFAULT_CONFIG_FILE =
"#{Dir.home}/.oraclebmc/config".freeze

Class Method Summary collapse

Class Method Details

.load_config(config_file_location: DEFAULT_CONFIG_FILE, profile_name: DEFAULT_PROFILE) ⇒ Config

Loads the a Config from the specified file and profile.

Defaults to “~/.oci/config” (on windows “C:Usersuser.ociconfig”) with a fallback to “~/.oraclebmc/config” (on windows “C:Usersuser.oraclebmcconfig”)



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/oci/config_file_loader.rb', line 30

def self.load_config(config_file_location: DEFAULT_CONFIG_FILE, profile_name: DEFAULT_PROFILE)
  if config_file_location == DEFAULT_CONFIG_FILE
    configs = if File.exist?(File.expand_path(config_file_location))
                load_configs(config_file_location)
              elsif File.exist?(File.expand_path(FALLBACK_DEFAULT_CONFIG_FILE))
                load_configs(FALLBACK_DEFAULT_CONFIG_FILE)
              else
                raise Errors::ConfigFileNotFoundError, 'Config file does not exist.'
              end
  else
    configs = load_configs(config_file_location)
  end

  return configs[profile_name] if configs && configs.key?(profile_name)
  raise Errors::ProfileNotFoundError, 'Profile not found in the given config file.'
end

.load_configs(config_file_location) ⇒ Array<Config>

Loads all of the Configs from the specified file.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/oci/config_file_loader.rb', line 51

def self.load_configs(config_file_location)
  config_file_location = File.expand_path(config_file_location)
  raise Errors::ConfigFileNotFoundError, 'Config file does not exist.' unless File.file?(config_file_location)

  config_file = IniFile.load(config_file_location)
  configs = {}

  if config_file.nil? || !config_file.has_section?(DEFAULT_PROFILE)
    raise Errors::DefaultProfileDoesNotExistError, 'The DEFAULT profile does not exist.'
  end

  config_file.each_section do |section|
    config = Config.new

    load_section(config_file[DEFAULT_PROFILE], config) unless section.equal? DEFAULT_PROFILE
    load_section(config_file[section], config)

    configs[section] = config
  end

  configs
end