Class: HieraSimulator::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/hiera-simulator/config.rb

Overview

Load the configuration for the Hiera simulator

The order in which files are read in are:

  • $CWD/.hiera-simulator.yaml

  • $HOME/.hiera-simulator.yaml

  • /etc/hiera-simulator.yaml

The reading of the configuration values happens very much like in Hiera itself, where the first time a key is seen, subsequent occurrences of that key are ignored.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(overrides = {}) ⇒ Config

Constructor

Parameters:

  • overrides (Hash) (defaults to: {})

    Override default settings for home_dir, global_config, and/or cwd



18
19
20
21
22
23
# File 'lib/hiera-simulator/config.rb', line 18

def initialize(overrides = {})
  @home_dir = overrides.fetch(:home_dir, ENV['HOME'])
  @global_configfile = overrides.fetch(:global_config, '/etc/hiera-simulator.yaml')
  @working_dir = overrides.fetch(:cwd) { |_x| File.absolute_path(Dir.getwd) }
  @config = overrides.fetch(:config) { |_x| retrieve_config }
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



14
15
16
# File 'lib/hiera-simulator/config.rb', line 14

def config
  @config
end

Instance Method Details

#get(key, default = nil) ⇒ Object

Get - shorthand method to retrieve a particular key from the config hash

Parameters:

  • key (String)

    Key to retrieve

  • default (Object) (defaults to: nil)

    Default value

Returns:

  • (Object)

    Value of config setting



29
30
31
# File 'lib/hiera-simulator/config.rb', line 29

def get(key, default = nil)
  @config.fetch(key, @config.fetch(key.to_s, @config.fetch(key.to_sym, default)))
end

#merge!(newhash) ⇒ Object

Merge! - Merge a hash

Parameters:

  • newhash (Hash)

    Hash to merge in



35
36
37
# File 'lib/hiera-simulator/config.rb', line 35

def merge!(newhash)
  @config.merge!(newhash)
end

#raise_missing_parameter(parameter) ⇒ Object

Throw “Missing Parameter” error

Raises:



45
46
47
48
49
50
51
# File 'lib/hiera-simulator/config.rb', line 45

def raise_missing_parameter(parameter)
  errmsg = "\n\nERROR!!! A required parameter (#{parameter}) was missing.\n"
  errmsg += "Tried these files: #{filelist.inspect}\n"
  errmsg += "(None of these files could be found)\n" if retrieve_config.empty?
  errmsg += "Please see https://github.com/kpaulisse/hiera-simulator/blob/master/doc/troubleshooting.rb for more.\n\n"
  raise ConfigError, errmsg
end

#validateObject

Validate configuration



40
41
42
# File 'lib/hiera-simulator/config.rb', line 40

def validate
  raise_no_config_files_found if config.empty?
end