module OCI::ConfigFileLoader

Module for loading Config from a file

Constants

DEFAULT_CONFIG_FILE

Default config file name and location

DEFAULT_PROFILE

Name of the default profile to load from a config file

FALLBACK_DEFAULT_CONFIG_FILE
OCI_CONFIG_FILE_ENV_VAR_NAME

Public Class Methods

load_config(config_file_location: DEFAULT_CONFIG_FILE, profile_name: DEFAULT_PROFILE) click to toggle source

rubocop:disable Metrics/CyclomaticComplexity Loads the a Config from the specified file and profile.

@param [String] config_file_location Filename and path of the config file. Defaults to “~/.oci/config” (on windows “C:Users{user}.ociconfig”) with a fallback to “~/.oraclebmc/config” (on windows “C:Users{user}.oraclebmcconfig”) If all the above fallbacks failed, it will use ENV VAR “OCI_CONFIG_FILE” to retrieve the path @param [String] profile_name Name of the profile from the file. Defaults to “DEFAULT”. @return [Config] A Config

# 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) click to toggle source

Loads all of the Configs from the specified file.

@param [String] config_file_location Filename and path of the config file. @return [Array<Config>] An array containing all the configs found in the given file.

# 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

Private Class Methods

load_section(section, config) click to toggle source
# File lib/oci/config_file_loader.rb, line 83
def self.load_section(section, config)
  section.each_key do |key|
    value = section[key]
    value = File.expand_path(value) if key == 'key_file'

    # key_content is not allowed in configuration file
    next if key == 'key_content'

    config.instance_variable_set('@' + key, value) if config.respond_to?("#{key}=") && config.respond_to?(key)
    # TODO: log the key been ignored by Ruby SDK
  end
end