3 CFG_SOURCEDIR=os.getenv("PROSODY_SRCDIR");
4 CFG_CONFIGDIR=os.getenv("PROSODY_CFGDIR");
6 -- Substitute ~ with path to home directory in paths
8 CFG_CONFIGDIR = CFG_CONFIGDIR:gsub("^~", os.getenv("HOME"));
12 CFG_SOURCEDIR = CFG_SOURCEDIR:gsub("^~", os.getenv("HOME"));
15 local default_config = (CFG_CONFIGDIR or ".").."/migrator.cfg.lua";
17 -- Command-line parsing
19 local handled_opts = 0;
21 if arg[i]:sub(1,2) == "--" then
22 local opt, val = arg[i]:match("([%w-]+)=?(.*)");
24 options[(opt:sub(3):gsub("%-", "_"))] = #val > 0 and val or true;
31 table.remove(arg, handled_opts);
34 package.path = CFG_SOURCEDIR.."/?.lua;"..package.path;
35 package.cpath = CFG_SOURCEDIR.."/?.so;"..package.cpath;
37 package.path = "../../?.lua;"..package.path
38 package.cpath = "../../?.so;"..package.cpath
41 local envloadfile = require "util.envload".envloadfile;
44 local function loadfilein(file, env)
46 return loadin(env, io.open(file):read("*a"));
48 return envloadfile(file, env);
52 local config_file = options.config or default_config;
53 local from_store = arg[1] or "input";
54 local to_store = arg[2] or "output";
57 local config_env = setmetatable({}, { __index = function(t, k) return function(tbl) config[k] = tbl; end; end });
58 local config_chunk, err = loadfilein(config_file, config_env);
59 if not config_chunk then
60 print("There was an error loading the config file, check the file exists");
61 print("and that the syntax is correct:");
69 if #arg > 0 and #arg ~= 2 then
71 print("Error: Incorrect number of parameters supplied.");
73 if not config[from_store] then
75 print("Error: Input store '"..from_store.."' not found in the config file.");
77 if not config[to_store] then
79 print("Error: Output store '"..to_store.."' not found in the config file.");
82 function load_store_handler(name)
83 local store_type = config[name].type;
84 if not store_type then
85 print("Error: "..name.." store type not specified in the config file");
88 local ok, err = pcall(require, "migrator."..store_type);
90 if package.loaded["migrator."..store_type] then
91 print(("Error: Failed to initialize '%s' store:\n\t%s")
94 print(("Error: Unrecognised store type for '%s': %s")
95 :format(from_store, store_type));
103 have_err = have_err or not(load_store_handler(from_store, "input") and load_store_handler(to_store, "output"));
107 print("Usage: "..arg[0].." FROM_STORE TO_STORE");
108 print("If no stores are specified, 'input' and 'output' are used.");
110 print("The available stores in your migrator config are:");
112 for store in pairs(config) do
119 local itype = config[from_store].type;
120 local otype = config[to_store].type;
121 local reader = require("migrator."..itype).reader(config[from_store]);
122 local writer = require("migrator."..otype).writer(config[to_store]);
124 local json = require "util.json";
126 io.stderr:write("Migrating...\n");
128 --print(json.encode(x))
131 writer(nil); -- close
132 io.stderr:write("Done!\n");