class Ufo::Setting

Public Class Methods

new(check_ufo_project=true) click to toggle source
# File lib/ufo/setting.rb, line 6
def initialize(check_ufo_project=true)
  @check_ufo_project = check_ufo_project
end

Public Instance Methods

data() click to toggle source

data contains the settings.yml config. The order or precedence for settings is the project ufo/settings.yml and then the ~/.ufo/settings.yml.

# File lib/ufo/setting.rb, line 12
def data
  if @check_ufo_project && !File.exist?(project_settings_path)
    Ufo.check_ufo_project!
  end

  # project based settings files
  project = load_file(project_settings_path)

  user_file = "#{ENV['HOME']}/.ufo/settings.yml"
  user = File.exist?(user_file) ? YAML.load_file(user_file) : {}

  default_file = File.expand_path("../default/settings.yml", __FILE__)
  default = load_file(default_file)

  all_envs = default.deep_merge(user.deep_merge(project))
  all_envs = merge_base(all_envs)
  data = all_envs[ufo_env] || all_envs["base"] || {}
  data.deep_symbolize_keys
end
ufo_env() click to toggle source

Resovles infinite problem since Ufo.env can be determined from UFO_ENV or settings.yml files. When ufo is determined from settings it should not called Ufo.env since that in turn calls Settings.new.data which can then cause an infinite loop.

# File lib/ufo/setting.rb, line 36
def ufo_env
  settings = YAML.load_file("#{Ufo.root}/.ufo/settings.yml")
  env = settings.find do |_env, section|
    section ||= {}
    ENV['AWS_PROFILE'] && ENV['AWS_PROFILE'] == section['aws_profile']
  end

  ufo_env = env.first if env
  ufo_env = ENV['UFO_ENV'] if ENV['UFO_ENV'] # highest precedence
  ufo_env || 'development'
end

Private Instance Methods

load_file(path) click to toggle source
# File lib/ufo/setting.rb, line 49
def load_file(path)
  return Hash.new({}) unless File.exist?(path)

  content = RenderMePretty.result(path)
  data = YAML.load(content)
  # If key is is accidentally set to nil it screws up the merge_base later.
  # So ensure that all keys with nil value are set to {}
  data.each do |env, _setting|
    data[env] ||= {}
  end
  data
end
merge_base(all_envs) click to toggle source

automatically add base settings to the rest of the environments

# File lib/ufo/setting.rb, line 63
def merge_base(all_envs)
  base = all_envs["base"] || {}
  all_envs.each do |env, settings|
    all_envs[env] = base.merge(settings) unless env == "base"
  end
  all_envs
end
project_settings_path() click to toggle source
# File lib/ufo/setting.rb, line 71
def project_settings_path
  "#{Ufo.root}/.ufo/settings.yml"
end