class AWS::ProfileParser

Constants

VERSION

Public Class Methods

new() click to toggle source
# File lib/aws/profile_parser.rb, line 6
def initialize
  @file = ENV['AWS_CONFIG_FILE'] || ENV['HOME'] + "/.aws/config"
  @credentials = nil
end

Public Instance Methods

get(profile='default') click to toggle source

returns hash of AWS credential

# File lib/aws/profile_parser.rb, line 12
def get(profile='default')
  raise 'Config File does not exist' unless File.exists?(@file)

  @credentials = parse if @credentials.nil?
  raise 'The profile is not specified in the config file' unless @credentials.has_key?(profile)

  @credentials[profile]
end
parse() click to toggle source
# File lib/aws/profile_parser.rb, line 21
def parse
  section = {}
  current = {}
  s = StringScanner.new(File.read(@file))

  while !s.eos?
    case
    when s.scan(/\s+/)
    when s.scan(/^#.*/)
      # do nothing
    when s.scan(/\[(profile\s+)?(.+?)\]/)
      # strip 'profile' from each profile key and set to section key
      section[s[2]] = current = {}
    when s.scan(/(\w+)\s*=\s*([^\n]+)/)
      # strip 'aws_' from each key
      current[s[1].sub(/^aws_/, '').to_sym] = s[2]
    else
      raise "Parse Error. I guess config file syntax is something wrong."
    end
  end

  @credentials = section
end