Module: StackedConfig::SourceHelper

Included in:
Layers::GenericLayer
Defined in:
lib/stacked_config/source_helper.rb

Constant Summary collapse

OS_FLAVOURS =
{
    mingw32: :windows,
    linux: :unix
}
DEFAULT_OS_FLAVOUR =
:unix
SYSTEM_CONFIG_ROOT =
{
    windows: [File.join(ENV['systemRoot'] || '', 'Config')],
    unix: '/etc'
}
EXTENSIONS =
%w(conf CONF cfg CFG yml YML yaml YAML)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.gem_config_rootObject



33
34
35
36
37
38
39
40
41
# File 'lib/stacked_config/source_helper.rb', line 33

def self.gem_config_root
  return nil unless $PROGRAM_NAME

  Gem.loaded_specs.each_pair do |name, spec|
    executable_basename = File.basename($PROGRAM_NAME)
    return spec.full_gem_path if spec.executables.include? executable_basename
  end
  nil
end

.os_flavourObject



17
18
19
# File 'lib/stacked_config/source_helper.rb', line 17

def self.os_flavour
  OS_FLAVOURS[RbConfig::CONFIG['target_os'].to_sym] || DEFAULT_OS_FLAVOUR
end

.supported_osesObject



21
22
23
# File 'lib/stacked_config/source_helper.rb', line 21

def self.supported_oses
  OS_FLAVOURS.values.sort.uniq
end

.system_config_rootObject



25
26
27
# File 'lib/stacked_config/source_helper.rb', line 25

def self.system_config_root
  SYSTEM_CONFIG_ROOT[os_flavour]
end

.user_config_rootObject



29
30
31
# File 'lib/stacked_config/source_helper.rb', line 29

def self.user_config_root
  Dir.home
end

Instance Method Details

#gem_config_rootObject



43
44
45
# File 'lib/stacked_config/source_helper.rb', line 43

def gem_config_root
  StackedConfig::SourceHelper.gem_config_root
end

#os_flavourObject



51
52
53
# File 'lib/stacked_config/source_helper.rb', line 51

def os_flavour
  @os_flavour ||= StackedConfig::SourceHelper.os_flavour
end

#perform_substitutions(path_part) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/stacked_config/source_helper.rb', line 86

def perform_substitutions path_part
  res = path_part.dup
  res.gsub! '##SYSTEM_CONFIG_ROOT##', system_config_root
  res.gsub! '##USER_CONFIG_ROOT##', user_config_root

  exec_name = manager.nil? ? StackedConfig::Orchestrator.default_config_file_base_name : manager.config_file_base_name
  res.gsub! '##PROGRAM_NAME##', exec_name
  if res =~ /##GEM_CONFIG_ROOT##/
    return nil unless gem_config_root
    res.gsub! '##GEM_CONFIG_ROOT##', gem_config_root
  end

  res
end

#set_config_file(places) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/stacked_config/source_helper.rb', line 63

def set_config_file(places)
  @file_name = nil
  places.each do |path_array|
    # Perform path substitutions
    begin
      potential_config_file = File.join(path_array.map { |path_part| perform_substitutions path_part })
    rescue
      #do nothing
    end
    return unless potential_config_file
    # Try to find config file with extension
    EXTENSIONS.each do |extension|
      file  = potential_config_file.gsub '##EXTENSION##', extension
      if File.readable? file
        @file_name = file
        return @file_name
      end
    end
  end
end

#supported_osesObject



47
48
49
# File 'lib/stacked_config/source_helper.rb', line 47

def supported_oses
  StackedConfig::SourceHelper.supported_oses
end

#system_config_rootObject



55
56
57
# File 'lib/stacked_config/source_helper.rb', line 55

def system_config_root
  StackedConfig::SourceHelper.system_config_root
end

#user_config_rootObject



59
60
61
# File 'lib/stacked_config/source_helper.rb', line 59

def user_config_root
  StackedConfig::SourceHelper.user_config_root
end