Class: Kubes::Compiler::Dsl::Core::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/kubes/compiler/dsl/core/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Parser

Returns a new instance of Parser.



3
4
5
# File 'lib/kubes/compiler/dsl/core/parser.rb', line 3

def initialize(path)
  @path = path
end

Instance Method Details

#parseObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/kubes/compiler/dsl/core/parser.rb', line 7

def parse
  contents = IO.read(@path)
  lines = parse_contents(contents)

  # First use a Hash structure so that overlay env files will override
  # the base param file.
  data = {}
  lines.each do |line|
    key,value = line.strip.split("=").map {|x| x.strip}
    value = remove_surrounding_quotes(value)
    data[key] = value
  end
  data
end

#parse_contents(contents) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/kubes/compiler/dsl/core/parser.rb', line 22

def parse_contents(contents)
  lines = contents.split("\n")
  # remove comment at the end of the line
  lines.map! { |l| l.sub(/#.*/,'').strip }
  # filter out commented lines
  lines = lines.reject { |l| l =~ /(^|\s)#/i }
  # filter out empty lines
  lines = lines.reject { |l| l.strip.empty? }
  lines
end

#remove_surrounding_quotes(s) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/kubes/compiler/dsl/core/parser.rb', line 33

def remove_surrounding_quotes(s)
  if s =~ /^"/ && s =~ /"$/
    s.sub(/^["]/, '').gsub(/["]$/,'') # remove surrounding double quotes
  elsif s =~ /^'/ && s =~ /'$/
    s.sub(/^[']/, '').gsub(/[']$/,'') # remove surrounding single quotes
  else
    s
  end
end