Class: PicsolveDockerBuilder::Helpers::ConfigManager

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/picsolve_docker_builder/helpers/config_manager.rb

Overview

Parses the config file

Instance Method Summary collapse

Methods included from Base

#base_dir, #config_file, #config_path, #config_paths, #create_logger, #default_config, #log, #read_config, #validate_config

Constructor Details

#initialize(paths, stage) ⇒ ConfigManager

Returns a new instance of ConfigManager.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/picsolve_docker_builder/helpers/config_manager.rb', line 10

def initialize(paths, stage)
  if paths.is_a?(Array)
    @paths = paths
  else
    @paths = [paths]
  end

  # TODO: Do this properly, this is a very very dirty hack
  stage = (
    !ENV['STAGE'].nil? &&
    ENV['STAGE'].downcase
  ) || 'ci' if stage.nil?
  @stage = stage
end

Instance Method Details

#configObject



29
30
31
32
33
# File 'lib/picsolve_docker_builder/helpers/config_manager.rb', line 29

def config
  return @config unless @config.nil?
  read
  parse_variables
end

#containers(composer) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/picsolve_docker_builder/helpers/config_manager.rb', line 62

def containers(composer)
  config['compose']['containers'].map do |name, raw_config|
    PicsolveDockerBuilder::Composer::Container.new(
      name,
      eval_container_config(raw_config),
      composer
    )
  end
end

#defaultObject



25
26
27
# File 'lib/picsolve_docker_builder/helpers/config_manager.rb', line 25

def default
  {}
end

#eval_container_config(config) ⇒ Object



35
36
37
38
39
40
# File 'lib/picsolve_docker_builder/helpers/config_manager.rb', line 35

def eval_container_config(config)
  # eval environment if key exits
  config['environment'] = eval_container_env(config['environment']) \
    if config.key? 'environment'
  config
end

#eval_container_env(env) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/picsolve_docker_builder/helpers/config_manager.rb', line 54

def eval_container_env(env)
  env = eval_container_env_split(env)
  output = {}
  output = output.update(env['default']) if env.key?('default')
  output = output.update(env[@stage]) if env.key?(@stage)
  output
end

#eval_container_env_split(env) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/picsolve_docker_builder/helpers/config_manager.rb', line 42

def eval_container_env_split(env)
  output = {}
  env.each do |key, lst|
    output[key] = {}
    lst.each do |elem|
      ikey, val = elem.split('=', 2)
      output[key][ikey] = val
    end
  end
  output
end

#parse_variablesObject



72
73
74
# File 'lib/picsolve_docker_builder/helpers/config_manager.rb', line 72

def parse_variables
  parse_variables_real(@config)
end

#parse_variables_real(obj) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/picsolve_docker_builder/helpers/config_manager.rb', line 76

def parse_variables_real(obj)
  if obj.is_a?(Hash)
    obj.each do |k, v|
      obj[k] = parse_variables_real(v)
    end
  elsif obj.is_a?(Array)
    obj.map do |v|
      parse_variables_real(v)
    end
  elsif obj.is_a?(String)
    Config::VariableObject.replace_string(obj)
  else
    obj
  end
end

#readObject



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/picsolve_docker_builder/helpers/config_manager.rb', line 92

def read
  @config = default
  @paths.each do |file|
    begin
      yaml = Psych.load_file file
      @config = @config.deep_merge(yaml)
    rescue Errno::ENOENT
      raise "cannot find config at '#{file}'"
    end
  end
end