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
OCI_CONFIG_FILE_ENV_VAR_NAME =
'OCI_CONFIG_FILE'.freeze

Class Method Summary collapse

Class Method Details

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

rubocop:disable Metrics/CyclomaticComplexity 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”) If all the above fallbacks failed, it will use ENV VAR “OCI_CONFIG_FILE” to retrieve the path

Parameters:

  • config_file_location (String) (defaults to: DEFAULT_CONFIG_FILE)

    Filename and path of the config file.

  • profile_name (String) (defaults to: DEFAULT_PROFILE)

    Name of the profile from the file. Defaults to “DEFAULT”.

Returns:

Raises:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/oci/config_file_loader.rb', line 34

def self.load_config(config_file_location: DEFAULT_CONFIG_FILE, profile_name: DEFAULT_PROFILE)
  env_var_config_path = ENV[OCI_CONFIG_FILE_ENV_VAR_NAME]
  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)
              elsif !env_var_config_path.nil? && File.exist?(File.expand_path(env_var_config_path))
                load_configs(env_var_config_path)
              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.

Parameters:

  • config_file_location (String)

    Filename and path of the config file.

Returns:

  • (Array<Config>)

    An array containing all the configs found in the given file.

Raises:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/oci/config_file_loader.rb', line 60

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