Sha256: 9695ba5c5dbf0b53669028cd4a1537f1f96628643f2a955ba1a78774a5451290

Contents?: true

Size: 1.35 KB

Versions: 8

Compression:

Stored size: 1.35 KB

Contents

require 'pathname'

module Lono
  module Core
    extend Memoist
    autoload :Config, 'lono/core/config'

    def config
      Config.new
    end
    memoize :config

    def root
      path = ENV['LONO_ROOT'] || '.'
      Pathname.new(path)
    end
    memoize :root

    def env
      ufo_env = env_from_profile(ENV['AWS_PROFILE']) || 'development'
      ufo_env = ENV['LONO_ENV'] if ENV['LONO_ENV'] # highest precedence
      ufo_env
    end
    memoize :env

    # Precedence (highest to lowest)
    #   1. LONO_SUFFIX
    #   2. .current/lono
    #   3. config/settings.yml
    def suffix
      suffix = ENV['LONO_SUFFIX'] # highest precedence
      suffix ||= Cfn::Current.suffix
      unless suffix
        settings = Setting.new.data
        suffix ||= settings["stack_name_suffix"] # lowest precedence
      end

      return if suffix&.empty?
      suffix
    end
    memoize :suffix

    private
    # Do not use the Setting class to load the profile because it can cause an
    # infinite loop then if we decide to use Lono.env from within settings class.
    def env_from_profile(aws_profile)
      data = YAML.load_file("#{Lono.root}/config/settings.yml")
      env = data.find do |_env, setting|
        setting ||= {}
        profiles = setting['aws_profiles']
        profiles && profiles.include?(aws_profile)
      end
      env.first if env
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
lono-4.2.7 lib/lono/core.rb
lono-4.2.6 lib/lono/core.rb
lono-4.2.5 lib/lono/core.rb
lono-4.2.4 lib/lono/core.rb
lono-4.2.3 lib/lono/core.rb
lono-4.2.2 lib/lono/core.rb
lono-4.2.1 lib/lono/core.rb
lono-4.2.0 lib/lono/core.rb