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 local function loadfilein(file, env)
36 return loadin(env, io.open(file):read("*a"));
38 local chunk, err = loadfile(file);
46 local config_file = options.config or default_config;
47 local from_store = arg[1] or "input";
48 local to_store = arg[2] or "output";
51 local config_env = setmetatable({}, { __index = function(t, k) return function(tbl) config[k] = tbl; end; end });
52 local config_chunk, err = loadfilein(config_file, config_env);
53 if not config_chunk then
54 print("There was an error loading the config file, check the file exists");
55 print("and that the syntax is correct:");
63 package.path = CFG_SOURCEDIR.."/?.lua;"..package.path;
64 package.cpath = CFG_SOURCEDIR.."/?.so;"..package.cpath;
65 elseif not package.loaded["util.json"] then
66 package.path = "../../?.lua;"..package.path
67 package.cpath = "../../?.so;"..package.cpath
71 if #arg > 0 and #arg ~= 2 then
73 print("Error: Incorrect number of parameters supplied.");
75 if not config[from_store] then
77 print("Error: Input store '"..from_store.."' not found in the config file.");
79 if not config[to_store] then
81 print("Error: Output store '"..to_store.."' not found in the config file.");
84 function load_store_handler(name)
85 local store_type = config[name].type;
86 if not store_type then
87 print("Error: "..name.." store type not specified in the config file");
90 local ok, err = pcall(require, "migrator."..store_type);
92 if package.loaded["migrator."..store_type] then
93 print(("Error: Failed to initialize '%s' store:\n\t%s")
96 print(("Error: Unrecognised store type for '%s': %s")
97 :format(from_store, store_type));
105 have_err = have_err or not(load_store_handler(from_store, "input") and load_store_handler(to_store, "output"));
109 print("Usage: "..arg[0].." FROM_STORE TO_STORE");
110 print("If no stores are specified, 'input' and 'output' are used.");
112 print("The available stores in your migrator config are:");
114 for store in pairs(config) do
121 local itype = config[from_store].type;
122 local otype = config[to_store].type;
123 local reader = require("migrator."..itype).reader(config[from_store]);
124 local writer = require("migrator."..otype).writer(config[to_store]);
126 local json = require "util.json";
128 io.stderr:write("Migrating...\n");
130 --print(json.encode(x))
133 writer(nil); -- close
134 io.stderr:write("Done!\n");