Module: OracleBMC::ConfigFileLoader

Defined in:
lib/oraclebmc/config_file_loader.rb

Defined Under Namespace

Modules: Errors

Constant Summary collapse

DEFAULT_PROFILE =

Name of the default profile to load from a config file

'DEFAULT'
DEFAULT_CONFIG_FILE =

Default config file name and location

'~/.oraclebmc/config'

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.

Parameters:

  • config_file_location (String) (defaults to: DEFAULT_CONFIG_FILE)

    Filename and path of the config file. Defaults to “~/.oraclebmc/config”

  • profile_name (String) (defaults to: DEFAULT_PROFILE)

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

Returns:



27
28
29
30
31
32
33
34
35
# File 'lib/oraclebmc/config_file_loader.rb', line 27

def self.load_config(config_file_location: DEFAULT_CONFIG_FILE, profile_name: DEFAULT_PROFILE)
  configs = load_configs(config_file_location)

  if configs && configs.key?(profile_name)
    return configs[profile_name]
  else
    raise Errors::ProfileNotFoundError.new("Profile not found in the given config file.")
  end
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.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/oraclebmc/config_file_loader.rb', line 41

def self.load_configs(config_file_location)
  config_file_location = File.expand_path(config_file_location)

  unless File.file?(config_file_location)
    raise Errors::ConfigFileNotFoundError.new("Config file does not exist.")
  end

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

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

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

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

    configs[section] = config
  end

  return configs
end