1 local default_config = "./migrator.cfg.lua";
3 -- Command-line parsing
5 local handled_opts = 0;
7 if arg[i]:sub(1,2) == "--" then
8 local opt, val = arg[i]:match("([%w-]+)=?(.*)");
10 options[(opt:sub(3):gsub("%-", "_"))] = #val > 0 and val or true;
17 table.remove(arg, handled_opts);
20 local function loadfilein(file, env)
22 return loadin(env, io.open(file):read("*a"));
24 local chunk, err = loadfile(file);
32 local config_file = options.config or default_config;
33 local from_store = arg[1] or "input";
34 local to_store = arg[2] or "output";
37 local config_env = setmetatable({}, { __index = function(t, k) return function(tbl) config[k] = tbl; end; end });
38 local config_chunk, err = loadfilein(config_file, config_env);
39 if not config_chunk then
40 print("There was an error loading the config file, check the file exists");
41 print("and that the syntax is correct:");
48 if not package.loaded["util.json"] then
49 package.path = "../../?.lua;"..package.path
50 package.cpath = "../../?.dll;"..package.cpath
54 if #arg > 0 and #arg ~= 2 then
56 print("Error: Incorrect number of parameters supplied.");
58 if not config[from_store] then
60 print("Error: Input store '"..from_store.."' not found in the config file.");
62 if not config[to_store] then
64 print("Error: Output store '"..to_store.."' not found in the config file.");
66 if not config[from_store].type then
68 print("Error: Input store type not specified in the config file");
69 elseif not pcall(require, config[from_store].type) then
71 print("Error: Unrecognised store type for '"..from_store.."': "..config[from_store].type);
73 if not config[to_store].type then
75 print("Error: Output store type not specified in the config file");
76 elseif not pcall(require, config[to_store].type) then
78 print("Error: Unrecognised store type for '"..to_store.."': "..config[to_store].type);
83 print("Usage: "..arg[0].." FROM_STORE TO_STORE");
84 print("If no stores are specified, 'input' and 'output' are used.");
86 print("The available stores in your migrator config are:");
88 for store in pairs(config) do
94 local itype = config[from_store].type;
95 local otype = config[to_store].type;
96 local reader = require(itype).reader(config[from_store]);
97 local writer = require(otype).writer(config[to_store]);
99 local json = require "util.json";
102 --print(json.encode(x))
105 writer(nil); -- close