kube-config-parser 0.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/utils/kube_config_parser.rb +104 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a3bea70414703dae92e1986bc9eb7cefeac36acf
4
+ data.tar.gz: a78cc5ad1364004909a5e0928365b3e069abd450
5
+ SHA512:
6
+ metadata.gz: 8e96d6d99e99f43165cef85c71e4fdb9d0ac5d0a7f91f4471e7e837d375fc34d7947ad40eb1a8377ba35c1fc00a8aaaa2baedd7140fb9f8bed9c1c0aed98ba7d
7
+ data.tar.gz: 49a66a937cd9a391cb2eeb6e6ebdeb2a5c95b358d48bb8b8adf8bdb1dfb6d6f458f7d2122b80db923014dcf5a0ecd02a2e0ece25c184aa41e0ccfbff8a477308
@@ -0,0 +1,104 @@
1
+ require 'yaml'
2
+
3
+ module ::YAML
4
+ def self.kube_load(all_str)
5
+ all_str.split("---\n").reject(&:empty?).map do |paragraph|
6
+ YAML.load(paragraph)
7
+ end
8
+ end
9
+ end
10
+
11
+ module ParserModule
12
+ class SimpleHash
13
+ def initialize(hash)
14
+ @hash = hash
15
+ end
16
+
17
+ def method_missing(method_name_sym)
18
+ result = @hash[method_name_sym.to_s] || @hash[method_name_sym]
19
+ return nil if result.nil?
20
+ SimpleHash.new(result)
21
+ end
22
+
23
+ def inspect
24
+ @hash.inspect
25
+ end
26
+
27
+ def to_s
28
+ @hash.to_s
29
+ end
30
+ end
31
+
32
+ class EnvParser
33
+
34
+ def initialize(input_table)
35
+ @input_table = input_table
36
+ @variable_table = {}
37
+ end
38
+
39
+ def update(line)
40
+ command = line.gsub(/\s?\|\s?/, ' || ').gsub(/set (.*?) =/, 'set[\'\1\'] =')
41
+ eval(command)
42
+ end
43
+
44
+ def set
45
+ @variable_table
46
+ end
47
+
48
+ def item
49
+ return SimpleHash.new(@input_table)
50
+ end
51
+
52
+ def default(value_str)
53
+ value_str
54
+ end
55
+
56
+ def method_missing(method_name_sym)
57
+ @variable_table[method_name_sym.to_s] || @variable_table[method_name_sym]
58
+ end
59
+
60
+ def echo(str)
61
+ command = '"' + str + '"'
62
+ #puts command
63
+ eval(command)
64
+ end
65
+
66
+ end
67
+
68
+ class RubyStrParser
69
+ def self.parse(str)
70
+ str.gsub(/\{\{(.*?)\}\}/, '#{\1}').gsub(/\s?\|\s?/, ' || ').gsub('"', '\"')
71
+ end
72
+ end
73
+
74
+ class KubeConfigParser
75
+ def self.parse(template_path, variable_path)
76
+ yml_content = File.open(template_path).read.each_line.map(&:chomp).to_a
77
+ env_lines = []
78
+ normal_lines = []
79
+
80
+ yml_content.each do |line|
81
+ match_result = line.match(/^\{\% (set (.*?)) \%\}$/)
82
+ if match_result
83
+ env_lines << match_result[1]
84
+ else
85
+ normal_lines << line
86
+ end
87
+ end
88
+
89
+ var_table = YAML.load(File.open(variable_path).read)
90
+
91
+ normal_yml_content = normal_lines.join("\n")
92
+ normal_yml_content
93
+
94
+ parser = EnvParser.new(var_table)
95
+
96
+ env_lines.each do |first_line|
97
+ parser.update(first_line)
98
+ end
99
+
100
+ ruby_style_content = RubyStrParser.parse(normal_yml_content)
101
+ parser.echo(ruby_style_content)
102
+ end
103
+ end
104
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kube-config-parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yukai Jin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: a simple kube config parser
14
+ email: fish1928@outlook.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/utils/kube_config_parser.rb
20
+ homepage:
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.6.12
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: KubeConfigparser
44
+ test_files: []