Class: AWSConfig::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_config/parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#credential_file_modeObject

Returns the value of attribute credential_file_mode.



6
7
8
# File 'lib/aws_config/parser.rb', line 6

def credential_file_mode
  @credential_file_mode
end

Class Method Details

.from_hash(hash) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# 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) ⇒ Object



8
9
10
# File 'lib/aws_config/parser.rb', line 8

def self.parse(string, credential_file_mode = false)
  from_hash(new.tokenize(string))
end

Instance Method Details

#tokenize(string) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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