-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_yaml.rb
executable file
·47 lines (39 loc) · 1.11 KB
/
merge_yaml.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
#!/usr/bin/env ruby
require 'yaml'
require 'optparse'
# Re-indent code due to ruby yaml output fails to indent multiline text
# properly when blank lines occurs in the middle of text
def reindent(text)
prev_line = ""
indent = ""
text.each_line do |line|
if line == "\n"
# Check white spaces in the beginning of previous line
indent = prev_line.match(/^([ ]+)/)[0] rescue false
line = indent + "\n"
end
print line
prev_line = line
end
end
options = {}
usage = "Usage: mergeyaml.rb --objects [objects.yaml] --site [site.yaml]"
OptionParser.new do |opts|
opts.banner = usage
opts.on("--sxl [YAML]", "Signal Exchange List") do |x|
options[:sxl] = x
end
opts.on("--site [YAML]", "Site configuration") do |t|
options[:site] = t
end
end.parse!
abort("--sxl needs to be set") if options[:sxl].nil?
abort("--site needs to be set") if options[:site].nil?
# Read yaml
objects = YAML.load_file(options[:sxl])
site = YAML.load_file(options[:site])
# Merge
#objects["sites"] = site["sites"]
site["objects"] = objects["objects"]
#reindent(objects.to_yaml)
reindent(site.to_yaml)