Sha256: ffc0f24b2139b88ce90fcf090ec999dfb330d32602d30d2c538fbe34be34f211

Contents?: true

Size: 1.38 KB

Versions: 17

Compression:

Stored size: 1.38 KB

Contents

require 'pathname'
require 'yaml'

module AwsEc2
  module Core
    @@config = nil
    def config
      @@config ||= Config.new.data
    end

    def settings
      Setting.new.data
    end

    def root
      path = ENV['AWS_EC2_ROOT'] || '.'
      Pathname.new(path)
    end

    def validate_in_project!
      unless File.exist?("#{root}/profiles")
        puts "Could not find a profiles folder in the current directory.  It does not look like you are running this command within a aws-ec2 project.  Please confirm that you are in a aws-ec2 project and try again.".colorize(:red)
        exit
      end
    end

    @@env = nil
    def env
      return @@env if @@env
      env = env_from_profile(ENV['AWS_PROFILE']) || 'development'
      env = ENV['AWS_EC2_ENV'] if ENV['AWS_EC2_ENV'] # highest precedence
      @@env = env
    end

    private
    # Do not use the Setting class to load the profile because it can cause an
    # infinite loop then if we decide to use AwsEc2.env from within settings class.
    def env_from_profile(aws_profile)
      settings_path = "#{AwsEc2.root}/config/settings.yml"
      return unless File.exist?(settings_path)

      data = YAML.load_file(settings_path)
      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

17 entries across 17 versions & 1 rubygems

Version Path
aws-ec2-1.4.9 lib/aws_ec2/core.rb
aws-ec2-1.4.8 lib/aws_ec2/core.rb
aws-ec2-1.4.7 lib/aws_ec2/core.rb
aws-ec2-1.4.6 lib/aws_ec2/core.rb
aws-ec2-1.4.5 lib/aws_ec2/core.rb
aws-ec2-1.4.4 lib/aws_ec2/core.rb
aws-ec2-1.4.3 lib/aws_ec2/core.rb
aws-ec2-1.4.2 lib/aws_ec2/core.rb
aws-ec2-1.4.1 lib/aws_ec2/core.rb
aws-ec2-1.4.0 lib/aws_ec2/core.rb
aws-ec2-1.3.2 lib/aws_ec2/core.rb
aws-ec2-1.3.1 lib/aws_ec2/core.rb
aws-ec2-1.3.0 lib/aws_ec2/core.rb
aws-ec2-1.2.2 lib/aws_ec2/core.rb
aws-ec2-1.2.1 lib/aws_ec2/core.rb
aws-ec2-1.2.0 lib/aws_ec2/core.rb
aws-ec2-1.1.0 lib/aws_ec2/core.rb