kube-config-parser 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a3bea70414703dae92e1986bc9eb7cefeac36acf
4
- data.tar.gz: a78cc5ad1364004909a5e0928365b3e069abd450
3
+ metadata.gz: 5ca7c495727da94c6adfc298d27698c9c2857bda
4
+ data.tar.gz: e1c4fadfa1a501ca218bd6616737e98d58bea577
5
5
  SHA512:
6
- metadata.gz: 8e96d6d99e99f43165cef85c71e4fdb9d0ac5d0a7f91f4471e7e837d375fc34d7947ad40eb1a8377ba35c1fc00a8aaaa2baedd7140fb9f8bed9c1c0aed98ba7d
7
- data.tar.gz: 49a66a937cd9a391cb2eeb6e6ebdeb2a5c95b358d48bb8b8adf8bdb1dfb6d6f458f7d2122b80db923014dcf5a0ecd02a2e0ece25c184aa41e0ccfbff8a477308
6
+ metadata.gz: e046fab9dd2407a466b11865bea27f926a5eaac6e037e563a2eac7f164323f921b5214a662e3bb6e219003421e8d1ff720d6329ccbc723981c6d444302900d5e
7
+ data.tar.gz: '097796c2e467823e97c7e57e877aaf78f729c71a6e403055b40e80fc43a57308ba94a3afcc370bec603bd7376ffa3c7158d65dc64a37075fdfecdf9fcab70cb3'
@@ -31,9 +31,18 @@ module ParserModule
31
31
 
32
32
  class EnvParser
33
33
 
34
- def initialize(input_table)
34
+
35
+ False = false
36
+ True = true
37
+
38
+ def initialize(input_table, ifelse_stack)
35
39
  @input_table = input_table
36
40
  @variable_table = {}
41
+ @ifelse_stack = ifelse_stack
42
+ end
43
+
44
+ def ifelse_stack
45
+ @ifelse_stack
37
46
  end
38
47
 
39
48
  def update(line)
@@ -65,6 +74,28 @@ module ParserModule
65
74
 
66
75
  end
67
76
 
77
+ class IfElseStack
78
+ def initialize
79
+ @seq = []
80
+ end
81
+
82
+ def push(condition, str)
83
+ @seq.push([condition, str])
84
+ end
85
+
86
+ def top
87
+ condition, str = @seq.delete_at(0)
88
+ is_ruby_value = str.match(/#\{(.*?)\}/)
89
+ str = @env_parser.instance_eval(is_ruby_value[1]) if is_ruby_value
90
+ @env_parser.instance_eval(condition) ? str : nil
91
+ end
92
+
93
+ def set_env_parser(env_parser)
94
+ @env_parser = env_parser
95
+ end
96
+
97
+ end
98
+
68
99
  class RubyStrParser
69
100
  def self.parse(str)
70
101
  str.gsub(/\{\{(.*?)\}\}/, '#{\1}').gsub(/\s?\|\s?/, ' || ').gsub('"', '\"')
@@ -73,16 +104,45 @@ module ParserModule
73
104
 
74
105
  class KubeConfigParser
75
106
  def self.parse(template_path, variable_path)
107
+ ifelse_stack = IfElseStack.new
108
+
76
109
  yml_content = File.open(template_path).read.each_line.map(&:chomp).to_a
77
110
  env_lines = []
78
111
  normal_lines = []
79
112
 
113
+ in_if_else_block = false
114
+ if_else_matched_condition = nil
115
+ if_else_matched_lines = []
116
+
80
117
  yml_content.each do |line|
81
118
  match_result = line.match(/^\{\% (set (.*?)) \%\}$/)
82
119
  if match_result
83
120
  env_lines << match_result[1]
84
121
  else
85
- normal_lines << line
122
+ if in_if_else_block
123
+ match_if_else_end = line.match(/\{\% endif -?\%\}/)
124
+ if match_if_else_end
125
+ ifelse_stack.push(if_else_matched_condition, if_else_matched_lines.join('\n'))
126
+ normal_lines << '#{ifelse_stack.top}'
127
+ in_if_else_block = false
128
+ if_else_matched_condition = nil
129
+ if_else_matched_lines = []
130
+ else
131
+ if line.match(/\{\{.*?\}\}/)
132
+ if_else_matched_lines << RubyStrParser.parse(line)
133
+ else
134
+ if_else_matched_lines << line
135
+ end
136
+ end
137
+ else # not in if else block
138
+ match_if_else_start = line.match(/\{\% if (.*?) (is defined )?-?\%\}/)
139
+ if match_if_else_start
140
+ in_if_else_block = true
141
+ if_else_matched_condition = match_if_else_start[1]
142
+ else
143
+ normal_lines << line
144
+ end
145
+ end
86
146
  end
87
147
  end
88
148
 
@@ -91,7 +151,8 @@ module ParserModule
91
151
  normal_yml_content = normal_lines.join("\n")
92
152
  normal_yml_content
93
153
 
94
- parser = EnvParser.new(var_table)
154
+ parser = EnvParser.new(var_table, ifelse_stack)
155
+ ifelse_stack.set_env_parser(parser)
95
156
 
96
157
  env_lines.each do |first_line|
97
158
  parser.update(first_line)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kube-config-parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yukai Jin