aboutsummaryrefslogtreecommitdiffstats
path: root/tools/migration/main.lua
blob: e4891966d481b98abb42e3a618d6dc8b2a015894 (plain)
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
local default_config = "./migrator.cfg.lua";

-- Command-line parsing
local options = {};
local handled_opts = 0;
for i = 1, #arg do
	if arg[i]:sub(1,2) == "--" then
		local opt, val = arg[i]:match("([%w-]+)=?(.*)");
		if opt then
			options[(opt:sub(3):gsub("%-", "_"))] = #val > 0 and val or true;
		end
		handled_opts = i;
	else
		break;
	end
end
table.remove(arg, handled_opts);

-- Load config file
local function loadfilein(file, env)
	if loadin then
		return loadin(env, io.open(file):read("*a"));
	else
		local chunk, err = loadfile(file);
		if chunk then
			setfenv(chunk, env);
		end
		return chunk, err;
	end
end

local config_file = options.config or default_config;
local from_store = arg[1] or "input";
local to_store = arg[2] or "output";

config = {};
local config_env = setmetatable({}, { __index = function(t, k) return function(tbl) config[k] = tbl; end; end });
local config_chunk, err = loadfilein(config_file, config_env);
if not config_chunk then
	print("There was an error loading the config file, check the file exists");
	print("and that the syntax is correct:");
	print("", err);
	os.exit(1);
end

config_chunk();

if not package.loaded["util.json"] then
	package.path = "../../?.lua;"..package.path
	package.cpath = "../../?.dll;"..package.cpath
end

local have_err;
if #arg > 0 and #arg ~= 2 then
	have_err = true;
	print("Error: Incorrect number of parameters supplied.");
end
if not config[from_store] then
	have_err = true;
	print("Error: Input store '"..from_store.."' not found in the config file.");
end
if not config[to_store] then
	have_err = true;
	print("Error: Output store '"..to_store.."' not found in the config file.");
end
if not config[from_store].type then
	have_err = true;
	print("Error: Input store type not specified in the config file");
elseif not pcall(require, config[from_store].type) then
	have_err = true;
	print("Error: Unrecognised store type for '"..from_store.."': "..config[from_store].type);
end
if not config[to_store].type then
	have_err = true;
	print("Error: Output store type not specified in the config file");
elseif not pcall(require, config[to_store].type) then
	have_err = true;
	print("Error: Unrecognised store type for '"..to_store.."': "..config[to_store].type);
end

if have_err then
	print("");
	print("Usage: "..arg[0].." FROM_STORE TO_STORE");
	print("If no stores are specified, 'input' and 'output' are used.");
	print("");
	print("The available stores in your migrator config are:");
	print("");
	for store in pairs(config) do
		print("", store);
	end
	os.exit(1);
end
	
local itype = config[from_store].type;
local otype = config[to_store].type;
local reader = require(itype).reader(config[from_store]);
local writer = require(otype).writer(config[to_store]);

local json = require "util.json";

for x in reader do
	--print(json.encode(x))
	writer(x);
end
writer(nil); -- close