yaml-merge

1
#!/usr/bin/env lua5.1
2
 
3
local yaml = require "yaml";
4
 
5
function merge_in(t1, t2, depth)
6
	depth = depth or 0;
7
	if type(t1) == "table" and type(t2) == "table" then
8
		local array = {};
9
		if depth < 2 then -- numeric
10
			for i,v in ipairs(t2) do
11
				array[v] = true;
12
				table.insert(t1, v);
13
			end
14
		end
15
		for k,v in pairs(t2) do -- non-numeric
16
			if not(type(k) == "number" and array[v]) then -- fixme, merge non-numeric
17
				t1[k] = merge_in(t1[k], v, depth + 1);
18
			end
19
		end
20
	elseif t2 ~= nil then
21
		t1 = t2;
22
	end
23
	return t1;
24
end
25
 
26
local out = {};
27
for i, fn in ipairs(arg) do
28
	local f = assert(io.open(fn, "r"));
29
	merge_in(out, yaml.load(f:read("*a")));
30
	f:close();
31
end
32
 
33
print(yaml.dump(out));