tools/migration/prosody-migrator.lua: Add messages to show when migration is in progress
[prosody.git] / tools / migration / prosody-migrator.lua
index 82eeab9d90d527465e71a5ec6f70d34fd4b7be1b..2a8bf1c3d445eadae87b046c713df327e838601a 100644 (file)
@@ -3,6 +3,15 @@
 CFG_SOURCEDIR=os.getenv("PROSODY_SRCDIR");
 CFG_CONFIGDIR=os.getenv("PROSODY_CFGDIR");
 
+-- Substitute ~ with path to home directory in paths
+if CFG_CONFIGDIR then
+        CFG_CONFIGDIR = CFG_CONFIGDIR:gsub("^~", os.getenv("HOME"));
+end
+
+if CFG_SOURCEDIR then
+        CFG_SOURCEDIR = CFG_SOURCEDIR:gsub("^~", os.getenv("HOME"));
+end
+
 local default_config = (CFG_CONFIGDIR or ".").."/migrator.cfg.lua";
 
 -- Command-line parsing
@@ -71,21 +80,30 @@ 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, "migrator."..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, "migrator."..config[to_store].type) then
-       have_err = true;
-       print("Error: Unrecognised store type for '"..to_store.."': "..config[to_store].type);
+
+function load_store_handler(name)
+       local store_type = config[name].type;
+       if not store_type then
+               print("Error: "..name.." store type not specified in the config file");
+               return false;
+       else
+               local ok, err = pcall(require, "migrator."..store_type);
+               if not ok then
+                       if package.loaded["migrator."..store_type] then
+                               print(("Error: Failed to initialize '%s' store:\n\t%s")
+                                       :format(name, err));
+                       else
+                               print(("Error: Unrecognised store type for '%s': %s")
+                                       :format(from_store, store_type));
+                       end
+                       return false;
+               end
+       end
+       return true;
 end
 
+have_err = have_err or not(load_store_handler(from_store, "input") and load_store_handler(to_store, "output"));
+
 if have_err then
        print("");
        print("Usage: "..arg[0].." FROM_STORE TO_STORE");
@@ -107,9 +125,10 @@ local writer = require("migrator."..otype).writer(config[to_store]);
 
 local json = require "util.json";
 
+io.stderr:write("Migrating...\n");
 for x in reader do
        --print(json.encode(x))
        writer(x);
 end
 writer(nil); -- close
-
+io.stderr:write("Done!\n");