class AwsCliConfigParser::Profiles

Public Class Methods

from_io(io) click to toggle source
# File lib/aws_cli_config_parser/profiles.rb, line 13
def self.from_io io
  new(
    io.each_line.with_object([]) do |line, profiles|
      case line
      when /^ *\[(?:profile +)?(.+)\] *\n$/ then $1.strip! ;           ; profiles.push({ name: $1 })
      when /^ *(\w+) *= *(.+) *\n?$/        then $1.strip! ; $2.strip! ; profiles.last.store($1, $2)
      else next
      end
    end
    .map do |pairs|
      ::AwsCliConfigParser::Profile.new(pairs.delete(:name), pairs)
    end
  )
end
new(profiles) click to toggle source
# File lib/aws_cli_config_parser/profiles.rb, line 9
def initialize profiles
  @profiles = profiles.each.to_a or raise TypeError
end

Public Instance Methods

get(name) click to toggle source
# File lib/aws_cli_config_parser/profiles.rb, line 53
def get name
  @profiles.find{ |profile| profile.name == name }
end
merge!(other) click to toggle source
# File lib/aws_cli_config_parser/profiles.rb, line 28
def merge! other
  raise TypeError unless other.is_a?(self.class)

  @profiles = [
    *@profiles,
    *other.instance_variable_get(:@profiles),
  ]
  .group_by(&:name)
  .map do |(_name, profiles)|
    profiles.reduce(:merge!)
  end

  self
end
merge_credentials!(credentials) click to toggle source
# File lib/aws_cli_config_parser/profiles.rb, line 43
def merge_credentials! credentials
  credentials = credentials.to_a.index_by(&:assumed_role)

  @profiles.each do |profile|
    profile.merge_credential!(credentials[profile.role]) if credentials.has_key?(profile.role)
  end

  self
end
to_h() click to toggle source
# File lib/aws_cli_config_parser/profiles.rb, line 57
def to_h
  @profiles.map{ |profile| [profile.name, profile.to_h] }.to_h
end