Class: Cratus::Config

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/cratus/config.rb

Overview

A generic way of constructing a mergeable configuration

rubocop:disable Metrics/MethodLength

Instance Method Summary collapse

Instance Method Details

#defaultsObject

A Hash of the default configuration options


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cratus/config.rb', line 6

def defaults
  {
    group_dn_attribute: :cn,
    group_member_attribute: :member,
    group_description_attribute: :description,
    group_objectclass: :group,
    group_basedn: 'ou=groups,dc=example,dc=com',
    group_memberof_attribute: :memberOf,
    user_dn_attribute: :samaccountname,
    user_objectclass: :user,
    user_basedn: 'ou=users,dc=example,dc=com',
    user_account_control_attribute: :userAccountControl,
    user_department_attribute: :department,
    user_lockout_attribute: :lockouttime,
    user_mail_attribute: :mail,
    user_displayname_attribute: :displayName,
    user_memberof_attribute: :memberOf,
    host: 'ldap.example.com', port: 389,
    basedn: 'dc=example,dc=com',
    username: 'username',
    password: 'p@assedWard!',
    include_distribution_groups: true
  }
end

#loadObject

Construct a base config using the following order of precedence:

* environment variables
* YAML file
* defaults

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

def load
  # First, apply the defaults
  merge defaults

  # Then apply the config file, if one exists
  begin
    apprc_dir = File.expand_path('~')
    config_file = File.expand_path(File.join(apprc_dir, '.cratus.yml'))
    merge YAML.load_file(config_file) if File.readable?(config_file)
  rescue => e
    puts "WARNING: Unable to read from #{config_file}: #{e.message}"
  end

  # Finally, apply any environment variables specified
  env_conf = {}
  defaults.keys.each do |key|
    cratus_key = "CRATUS_#{key}".upcase
    env_conf[key] = ENV[cratus_key] if ENV.key?(cratus_key)
  end
  merge env_conf unless env_conf.empty?
end

#merge(data) ⇒ Object


57
58
59
60
61
62
# File 'lib/cratus/config.rb', line 57

def merge(data)
  raise 'Invalid Config Data' unless data.is_a?(Hash)
  data.each do |k, v|
    self[k.to_sym] = v
  end
end