Class: R10K::Settings::Loader

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/r10k/settings/loader.rb

Overview

Look for the r10k configuration file in standard locations.

r10k.yaml is checked for in the following locations:

- $PWD/r10k.yaml
- /etc/puppetlabs/r10k/r10k.yaml
- /etc/r10k.yaml

Defined Under Namespace

Classes: ConfigError

Constant Summary collapse

CONFIG_FILE =
'r10k.yaml'
DEFAULT_LOCATION =
File.join('/etc/puppetlabs/r10k', CONFIG_FILE)
OLD_DEFAULT_LOCATION =
File.join('/etc', CONFIG_FILE)

Constants included from Logging

Logging::LOG_LEVELS, Logging::SYSLOG_LEVELS_MAP

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

add_outputters, debug_formatter, default_formatter, default_outputter, #logger, #logger_name, parse_level

Constructor Details

#initializeLoader

Returns a new instance of Loader.



27
28
29
30
# File 'lib/r10k/settings/loader.rb', line 27

def initialize
  @loadpath = []
  populate_loadpath
end

Instance Attribute Details

#loadpathObject (readonly)

Returns the value of attribute loadpath.



21
22
23
# File 'lib/r10k/settings/loader.rb', line 21

def loadpath
  @loadpath
end

Class Method Details

.search(override = nil) ⇒ Object



15
16
17
# File 'lib/r10k/settings/loader.rb', line 15

def self.search(override = nil)
  new.search(override)
end

Instance Method Details

#read(override = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/r10k/settings/loader.rb', line 59

def read(override = nil)
  path = search(override)

  if path.nil?
    raise ConfigError, _("No configuration file given, no config file found in current directory, and no global config present")
  end

  begin
    contents = ::YAML.load_file(path)
  rescue => e
    raise ConfigError, _("Couldn't load config file: %{error_msg}") % {error_msg: e.message}
  end

  if !contents
    raise ConfigError, _("File exists at #{path} but doesn't contain any YAML") % {path: path}
  end
  R10K::Util::SymbolizeKeys.symbolize_keys!(contents, true)
  contents
end

#search(override = nil) ⇒ String?

Find the first valid config file.

Parameters:

  • override (String, nil) (defaults to: nil)

    An optional path that when is truthy will be preferred over all other files, to make it easy to optionally supply an explicit configuration file that will always be used when set.

Returns:

  • (String, nil)

    The path to the first valid configfile, or nil if no file was found.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/r10k/settings/loader.rb', line 40

def search(override = nil)
  return override if override

  # If both default files are present, issue a warning.
  if (File.file? DEFAULT_LOCATION) && (File.file? OLD_DEFAULT_LOCATION)
    logger.warn _("Both %{default_path} and %{old_default_path} configuration files exist.") % {default_path: DEFAULT_LOCATION, old_default_path: OLD_DEFAULT_LOCATION}
    logger.warn _("%{default_path} will be used.") % {default_path: DEFAULT_LOCATION}
  end

  path = @loadpath.find {|filename| File.file? filename}

  if path == OLD_DEFAULT_LOCATION
    logger.warn _("The r10k configuration file at %{old_default_path} is deprecated.") % {old_default_path: OLD_DEFAULT_LOCATION}
    logger.warn _("Please move your r10k configuration to %{default_path}.") % {default_path: DEFAULT_LOCATION}
  end

  path
end