3 CFG_SOURCEDIR=os.getenv("PROSODY_SRCDIR");
4 CFG_CONFIGDIR=os.getenv("PROSODY_CFGDIR");
6 local default_config = (CFG_CONFIGDIR or ".").."/migrator.cfg.lua";
8 -- Command-line parsing
10 local handled_opts = 0;
12 if arg[i]:sub(1,2) == "--" then
13 local opt, val = arg[i]:match("([%w-]+)=?(.*)");
15 options[(opt:sub(3):gsub("%-", "_"))] = #val > 0 and val or true;
22 table.remove(arg, handled_opts);
25 local function loadfilein(file, env)
27 return loadin(env, io.open(file):read("*a"));
29 local chunk, err = loadfile(file);
37 local config_file = options.config or default_config;
38 local from_store = arg[1] or "input";
39 local to_store = arg[2] or "output";
42 local config_env = setmetatable({}, { __index = function(t, k) return function(tbl) config[k] = tbl; end; end });
43 local config_chunk, err = loadfilein(config_file, config_env);
44 if not config_chunk then
45 print("There was an error loading the config file, check the file exists");
46 print("and that the syntax is correct:");
54 package.path = CFG_SOURCEDIR.."/?.lua;"..package.path;
55 package.cpath = CFG_SOURCEDIR.."/?.so;"..package.cpath;
56 elseif not package.loaded["util.json"] then
57 package.path = "../../?.lua;"..package.path
58 package.cpath = "../../?.so;"..package.cpath
62 if #arg > 0 and #arg ~= 2 then
64 print("Error: Incorrect number of parameters supplied.");
66 if not config[from_store] then
68 print("Error: Input store '"..from_store.."' not found in the config file.");
70 if not config[to_store] then
72 print("Error: Output store '"..to_store.."' not found in the config file.");
74 if not config[from_store].type then
76 print("Error: Input store type not specified in the config file");
77 elseif not pcall(require, "migrator."..config[from_store].type) then
79 print("Error: Unrecognised store type for '"..from_store.."': "..config[from_store].type);
81 if not config[to_store].type then
83 print("Error: Output store type not specified in the config file");
84 elseif not pcall(require, "migrator."..config[to_store].type) then
86 print("Error: Unrecognised store type for '"..to_store.."': "..config[to_store].type);
91 print("Usage: "..arg[0].." FROM_STORE TO_STORE");
92 print("If no stores are specified, 'input' and 'output' are used.");
94 print("The available stores in your migrator config are:");
96 for store in pairs(config) do
103 local itype = config[from_store].type;
104 local otype = config[to_store].type;
105 local reader = require("migrator."..itype).reader(config[from_store]);
106 local writer = require("migrator."..otype).writer(config[to_store]);
108 local json = require "util.json";
111 --print(json.encode(x))
114 writer(nil); -- close