class AWSConfig::Parser

Attributes

credential_file_mode[RW]

Public Class Methods

from_hash(hash) click to toggle source
# File lib/aws_config/parser.rb, line 12
def self.from_hash(hash)
  hash.inject({}) do |memo, (k,v)|
    if v.is_a?(Hash)
      memo[k]=Profile.new(k,v)
    else
      memo[k] = v
    end

    memo
  end
end
parse(string, credential_file_mode = false) click to toggle source
# File lib/aws_config/parser.rb, line 8
def self.parse(string, credential_file_mode = false)
  from_hash(new.tokenize(string))
end

Public Instance Methods

tokenize(string) click to toggle source
# File lib/aws_config/parser.rb, line 24
def tokenize(string)
  tokens = { }
  current_profile = nil
  current_nesting = nil

  string.lines.each do |line|
    comment = line.match(/^\s*#.*/)
    blank = line.match(/^\s*$/)
    next if comment || blank

    profile_match = line.match(/\[\s*(profile)?\s*(?<profile>[^\]]+)\s*\]/)
    if profile_match
      current_profile = profile_match[:profile]
      tokens[current_profile] ||= {}
      next
    end

    nest_key_value = line.match(/(?<nest>^\s+)?(?<key>[^\s=#]+)\s*=\s*(?<value>[^\s#]+)/)
    if nest_key_value
      nest, key, value = !!nest_key_value[:nest], nest_key_value[:key], nest_key_value[:value]
      if nest
        fail("Nesting without a parent error") if current_nesting.nil?
        tokens[current_profile][current_nesting][key] = value
      else
        current_nesting = nil
        tokens[current_profile][key] = value
      end
      next
    end

    nesting = line.match(/(?<name>[^\s=#]+)\s*=.*/)
    if nesting
      current_nesting = nesting[:name]
      tokens[current_profile][current_nesting] ||= {}
    end
  end
  tokens
end