class CIDE::ConfigFileLoader

Public Class Methods

new(config) click to toggle source
# File lib/cide/config_file_loader.rb, line 16
def initialize(config)
  @config = config
end

Public Instance Methods

load(data) click to toggle source
# File lib/cide/config_file_loader.rb, line 20
def load(data)
  data.each_pair do |key, value|
    key = key.to_s
    path = Path.new(key)
    case key
    when 'from', 'image' then
      wanted_key(path, 'from', key)
      @config.from = expect_string(path, value)
    when 'as_root' then
      @config.as_root = maybe_step(path, value)
    when 'use_ssh' then
      @config.use_ssh = expect_boolean(path, value)
    when 'before' then
      @config.before = maybe_step(path, value)
    when 'env', 'forward_env' then
      wanted_key(path, 'env', key)
      @config.env = expect_env_hash(path, value)
    when 'export_dir' then
      @config.export_dir = maybe_string(path, value)
    when 'link', 'links' then
      @config.links = expect_links(path, value)
    when 'run', 'command' then
      wanted_key(path, 'run', key)
      @config.run = expect_run(path, value)
    when 'package' then
      @config.package = maybe_package(path, value)
    else
      unknown_key(path)
    end
  end
  @config
end

Protected Instance Methods

error(message) click to toggle source
# File lib/cide/config_file_loader.rb, line 59
def error(message)
  @config.errors << message
end
expect_adds(path, value) click to toggle source
# File lib/cide/config_file_loader.rb, line 260
def expect_adds(path, value)
  array = []
  case value
  when Array then
    value.compact.each_with_index do |value_, i|
      str = expect_string(path.append(i), value_)
      array << load_add_str(str)
    end
  when Hash then
    value.each_pair do |key_, value_|
      src = expect_array(path.append(key_), value_)
      array << ConfigFile::FileAdd.new(src: src, dest: key_.to_s)
    end
  when String, Symbol, Integer then
    array << load_add_str(value.to_s)
  when nil then
    # nothing to do
  else
    type_error(path, 'arrays of string, hash, string or nil', value)
  end
  array.compact
end
expect_array(path, value) click to toggle source
# File lib/cide/config_file_loader.rb, line 243
def expect_array(path, value)
  array = []
  case value
  when Array then
    value.compact.each_with_index do |value_, i|
      array << expect_string(path.append(i), value_)
    end
  when String, Symbol, Integer then
    array << value.to_s
  when nil then
    # nothing to do
  else
    type_error(path, 'array of string, string or nil', value)
  end
  array.compact
end
expect_boolean(path, value) click to toggle source
# File lib/cide/config_file_loader.rb, line 231
def expect_boolean(path, value)
  case value
  when true then
    true
  when false then
    false
  else
    type_error(path, 'boolean', value)
    false
  end
end
expect_env(path, key) click to toggle source
# File lib/cide/config_file_loader.rb, line 290
def expect_env(path, key)
  str = expect_string(path, key)
  return nil if str == ''
  value = ENV[str]
  error "Missing environment variable #{key} in #{path}" if value.nil?
  value
end
expect_env_hash(path, value) click to toggle source
# File lib/cide/config_file_loader.rb, line 298
def expect_env_hash(path, value)
  hash = {}
  case value
  when String, Symbol, Integer
    key1 = value
    value1 = expect_env(path, key1)
    hash[key1] = value1 if value1
  when Array then
    value.compact.each_with_index do |key, i|
      value_ = expect_env(path.append(i), key)
      hash[key.to_s] = value_ if value_
    end
  when Hash then
    value.each_pair do |key, value_|
      key = key.to_s
      path_ = path.append(key)
      value_ =
        if value_.nil?
          expect_env(path_, key)
        else
          expect_string(path_, value_)
        end
      hash[key.to_s] = value_ if value_
    end
  else
    type_error(path, 'hash or array of keys or just a string', value)
  end
  hash
end
expect_run(path, value) click to toggle source
# File lib/cide/config_file_loader.rb, line 328
def expect_run(path, value)
  array = []
  has_error = false
  case value
  when Array
    value.compact.each_with_index do |key, i|
      array << expect_string(path.append(i), key)
    end
  when String, Symbol, Integer
    array.push('sh', '-e', '-c', value.to_s)
  when nil then
  else
    has_error = true
    type_error(path, 'string or array of string', value)
  end
  error("#{path} shouldn't be empty") if array.empty? && !has_error
  array
end
expect_string(path, value) click to toggle source
# File lib/cide/config_file_loader.rb, line 76
def expect_string(path, value)
  case value
  when String, Symbol, Integer
    value.to_s
  else
    type_error(path, 'string', value)
    ''
  end
end
load_add_str(str) click to toggle source
# File lib/cide/config_file_loader.rb, line 283
def load_add_str(str)
  ConfigFile::FileAdd.new(
    src: [str],
    dest: str,
  )
end
load_package_config(path, data) click to toggle source
# File lib/cide/config_file_loader.rb, line 144
def load_package_config(path, data)
  package = ConfigFile::PackageConfig.new
  data.each_pair do |key, value|
    key = key.to_s
    path_ = path.append(key)
    case key
    when 'add_version' then
      case value.to_s
      when 'short_sha'
        package.add_version = 'short_sha'
      when 'sha'
        package.add_version = 'sha'
      when 'auto'
        package.add_version = 'auto'
      when nil, 'no', 'none'
        # ignore
      else
        error('expected value to be one of ' \
              "\"short_sha\", \"sha\" or \"auto\" in #{_path}")
      end
    else
      unknown_key(path_)
    end
  end
  package
end
load_step(path, data) click to toggle source
# File lib/cide/config_file_loader.rb, line 112
def load_step(path, data)
  step = ConfigFile::Step.new
  data.each_pair do |key, value|
    key = key.to_s
    path_ = path.append(key)
    case key
    when 'run' then
      step.run = expect_array(path_, value)
    when 'env', 'forward_env' then
      wanted_key(path_, 'env', key)
      step.env = expect_env_hash(path_, value)
    when 'add' then
      step.add = expect_adds(path_, value)
    else
      unknown_key(path_)
    end
  end
  step
end
maybe_package(path, value) click to toggle source
# File lib/cide/config_file_loader.rb, line 132
def maybe_package(path, value)
  case value
  when Hash then
    load_package_config(path, value)
  when nil then
    nil
  else
    type_error(path, 'hash or nil', value)
    nil
  end
end
maybe_step(path, value) click to toggle source
# File lib/cide/config_file_loader.rb, line 98
def maybe_step(path, value)
  case value
  when String, Symbol, Integer, Array then
    load_step(path, run: value)
  when Hash then
    load_step(path, value)
  when nil then
    nil
  else
    type_error(path, 'string, array, hash or nil', value)
    nil
  end
end
maybe_string(path, value) click to toggle source
# File lib/cide/config_file_loader.rb, line 86
def maybe_string(path, value)
  case value
  when String, Symbol, Integer
    value.to_s
  when nil
    nil
  else
    type_error(path, 'string or nil', value)
    nil
  end
end
type_error(path, wanted_type, value) click to toggle source
# File lib/cide/config_file_loader.rb, line 72
def type_error(path, wanted_type, value)
  error "expected #{path} to be a #{wanted_type} but got a #{value.class}"
end
unknown_key(path) click to toggle source
# File lib/cide/config_file_loader.rb, line 68
def unknown_key(path)
  warn "Unknown key #{path}"
end
wanted_key(path, wanted_key, key) click to toggle source
# File lib/cide/config_file_loader.rb, line 63
def wanted_key(path, wanted_key, key)
  return if key == wanted_key
  warn "#{path} is deprecated. use '#{wanted_key}' instead."
end
warn(message) click to toggle source
# File lib/cide/config_file_loader.rb, line 55
def warn(message)
  @config.warnings << message
end