forked from voxpupuli/puppet-gitlab_ci_runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dumper.rb
140 lines (115 loc) · 4.42 KB
/
dumper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# frozen_string_literal: true
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# The Dumper class was blindly copied from https://github.com/emancu/toml-rb/blob/v2.0.1/lib/toml-rb/dumper.rb
# This allows us to use the `to_toml` function as a `Deferred` function because the `toml-rb` gem is usually
# installed on the agent and the `Deferred` function gets evaluated before the catalog gets applied. This
# makes it in most scenarios impossible to install the gem before it is used.
require 'date'
module PuppetX
module Gitlab
class Dumper
attr_reader :toml_str
def initialize(hash)
@toml_str = ''
visit(hash, [])
end
private
def visit(hash, prefix, extra_brackets = false)
simple_pairs, nested_pairs, table_array_pairs = sort_pairs hash
print_prefix prefix, extra_brackets if prefix.any? && (simple_pairs.any? || hash.empty?)
dump_pairs simple_pairs, nested_pairs, table_array_pairs, prefix
end
def sort_pairs(hash)
nested_pairs = []
simple_pairs = []
table_array_pairs = []
hash.keys.sort.each do |key|
val = hash[key]
element = [key, val]
if val.is_a? Hash
nested_pairs << element
elsif val.is_a?(Array) && val.first.is_a?(Hash)
table_array_pairs << element
else
simple_pairs << element
end
end
[simple_pairs, nested_pairs, table_array_pairs]
end
def dump_pairs(simple, nested, table_array, prefix = [])
# First add simple pairs, under the prefix
dump_simple_pairs simple
dump_nested_pairs nested, prefix
dump_table_array_pairs table_array, prefix
end
def dump_simple_pairs(simple_pairs)
simple_pairs.each do |key, val|
key = quote_key(key) unless bare_key? key
@toml_str += "#{key} = #{to_toml(val)}\n"
end
end
def dump_nested_pairs(nested_pairs, prefix)
nested_pairs.each do |key, val|
key = quote_key(key) unless bare_key? key
visit val, prefix + [key], false
end
end
def dump_table_array_pairs(table_array_pairs, prefix)
table_array_pairs.each do |key, val|
key = quote_key(key) unless bare_key? key
aux_prefix = prefix + [key]
val.each do |child|
print_prefix aux_prefix, true
args = sort_pairs(child) << aux_prefix
dump_pairs(*args)
end
end
end
def print_prefix(prefix, extra_brackets = false)
new_prefix = prefix.join('.')
new_prefix = '[' + new_prefix + ']' if extra_brackets
@toml_str += "[" + new_prefix + "]\n" # rubocop:disable Style/StringLiterals
end
def to_toml(obj)
if obj.is_a?(Time) || obj.is_a?(DateTime)
obj.strftime('%Y-%m-%dT%H:%M:%SZ')
elsif obj.is_a?(Date)
obj.strftime('%Y-%m-%d')
elsif obj.is_a? Regexp
obj.inspect.inspect
elsif obj.is_a? String
obj.inspect.gsub(/\\(#[$@{])/, '\1') # rubocop:disable Style/RegexpLiteral
else
obj.inspect
end
end
def bare_key?(key)
# rubocop:disable Style/RegexpLiteral
!!key.to_s.match(/^[a-zA-Z0-9_-]*$/)
# rubocop:enable Style/RegexpLiteral
end
def quote_key(key)
'"' + key.gsub('"', '\\"') + '"'
end
end
end
end