tools/migration: Rename main.lua -> prosody-migrator.lua and update the Makefile
authorMatthew Wild <mwild1@gmail.com>
Mon, 21 Mar 2011 19:39:04 +0000 (19:39 +0000)
committerMatthew Wild <mwild1@gmail.com>
Mon, 21 Mar 2011 19:39:04 +0000 (19:39 +0000)
tools/migration/Makefile
tools/migration/main.lua [deleted file]
tools/migration/prosody-migrator.lua [new file with mode: 0644]

index adceafd406219a94eee05d57952c689f71f676ea..5998a5f706b344831cd8b06407106835569b84cd 100644 (file)
@@ -14,7 +14,7 @@ INSTALLEDDATA = $(DATADIR)
 
 SOURCE_FILES = migrator/*.lua
 
-all: prosody-migrator.install migrator.cfg.lua.install main.lua $(SOURCE_FILES)
+all: prosody-migrator.install migrator.cfg.lua.install prosody-migrator.lua $(SOURCE_FILES)
 
 install: prosody-migrator.install migrator.cfg.lua.install
        install -d $(BIN) $(CONFIG) $(SOURCE) $(SOURCE)/migrator
@@ -28,10 +28,10 @@ clean:
        rm -f prosody-migrator.install
        rm -f migrator.cfg.lua.install
 
-prosody-migrator.install: main.lua
+prosody-migrator.install: prosody-migrator.lua
        sed "s|^CFG_SOURCEDIR=.*;$$|CFG_SOURCEDIR='$(INSTALLEDSOURCE)';|; \
                s|^CFG_CONFIGDIR=.*;$$|CFG_CONFIGDIR='$(INSTALLEDCONFIG)';|;" \
-                       < main.lua > prosody-migrator.install
+                       < prosody-migrator.lua > prosody-migrator.install
 
 migrator.cfg.lua.install: migrator.cfg.lua
        sed "s|^local data_path = .*;$$|local data_path = '$(INSTALLEDDATA)';|;" \
diff --git a/tools/migration/main.lua b/tools/migration/main.lua
deleted file mode 100644 (file)
index 82eeab9..0000000
+++ /dev/null
@@ -1,115 +0,0 @@
-#!/usr/bin/env lua
-
-CFG_SOURCEDIR=os.getenv("PROSODY_SRCDIR");
-CFG_CONFIGDIR=os.getenv("PROSODY_CFGDIR");
-
-local default_config = (CFG_CONFIGDIR or ".").."/migrator.cfg.lua";
-
--- Command-line parsing
-local options = {};
-local handled_opts = 0;
-for i = 1, #arg do
-       if arg[i]:sub(1,2) == "--" then
-               local opt, val = arg[i]:match("([%w-]+)=?(.*)");
-               if opt then
-                       options[(opt:sub(3):gsub("%-", "_"))] = #val > 0 and val or true;
-               end
-               handled_opts = i;
-       else
-               break;
-       end
-end
-table.remove(arg, handled_opts);
-
--- Load config file
-local function loadfilein(file, env)
-       if loadin then
-               return loadin(env, io.open(file):read("*a"));
-       else
-               local chunk, err = loadfile(file);
-               if chunk then
-                       setfenv(chunk, env);
-               end
-               return chunk, err;
-       end
-end
-
-local config_file = options.config or default_config;
-local from_store = arg[1] or "input";
-local to_store = arg[2] or "output";
-
-config = {};
-local config_env = setmetatable({}, { __index = function(t, k) return function(tbl) config[k] = tbl; end; end });
-local config_chunk, err = loadfilein(config_file, config_env);
-if not config_chunk then
-       print("There was an error loading the config file, check the file exists");
-       print("and that the syntax is correct:");
-       print("", err);
-       os.exit(1);
-end
-
-config_chunk();
-
-if CFG_SOURCEDIR then
-       package.path = CFG_SOURCEDIR.."/?.lua;"..package.path;
-       package.cpath = CFG_SOURCEDIR.."/?.so;"..package.cpath;
-elseif not package.loaded["util.json"] then
-       package.path = "../../?.lua;"..package.path
-       package.cpath = "../../?.so;"..package.cpath
-end
-
-local have_err;
-if #arg > 0 and #arg ~= 2 then
-       have_err = true;
-       print("Error: Incorrect number of parameters supplied.");
-end
-if not config[from_store] then
-       have_err = true;
-       print("Error: Input store '"..from_store.."' not found in the config file.");
-end
-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);
-end
-
-if have_err then
-       print("");
-       print("Usage: "..arg[0].." FROM_STORE TO_STORE");
-       print("If no stores are specified, 'input' and 'output' are used.");
-       print("");
-       print("The available stores in your migrator config are:");
-       print("");
-       for store in pairs(config) do
-               print("", store);
-       end
-       print("");
-       os.exit(1);
-end
-       
-local itype = config[from_store].type;
-local otype = config[to_store].type;
-local reader = require("migrator."..itype).reader(config[from_store]);
-local writer = require("migrator."..otype).writer(config[to_store]);
-
-local json = require "util.json";
-
-for x in reader do
-       --print(json.encode(x))
-       writer(x);
-end
-writer(nil); -- close
-
diff --git a/tools/migration/prosody-migrator.lua b/tools/migration/prosody-migrator.lua
new file mode 100644 (file)
index 0000000..82eeab9
--- /dev/null
@@ -0,0 +1,115 @@
+#!/usr/bin/env lua
+
+CFG_SOURCEDIR=os.getenv("PROSODY_SRCDIR");
+CFG_CONFIGDIR=os.getenv("PROSODY_CFGDIR");
+
+local default_config = (CFG_CONFIGDIR or ".").."/migrator.cfg.lua";
+
+-- Command-line parsing
+local options = {};
+local handled_opts = 0;
+for i = 1, #arg do
+       if arg[i]:sub(1,2) == "--" then
+               local opt, val = arg[i]:match("([%w-]+)=?(.*)");
+               if opt then
+                       options[(opt:sub(3):gsub("%-", "_"))] = #val > 0 and val or true;
+               end
+               handled_opts = i;
+       else
+               break;
+       end
+end
+table.remove(arg, handled_opts);
+
+-- Load config file
+local function loadfilein(file, env)
+       if loadin then
+               return loadin(env, io.open(file):read("*a"));
+       else
+               local chunk, err = loadfile(file);
+               if chunk then
+                       setfenv(chunk, env);
+               end
+               return chunk, err;
+       end
+end
+
+local config_file = options.config or default_config;
+local from_store = arg[1] or "input";
+local to_store = arg[2] or "output";
+
+config = {};
+local config_env = setmetatable({}, { __index = function(t, k) return function(tbl) config[k] = tbl; end; end });
+local config_chunk, err = loadfilein(config_file, config_env);
+if not config_chunk then
+       print("There was an error loading the config file, check the file exists");
+       print("and that the syntax is correct:");
+       print("", err);
+       os.exit(1);
+end
+
+config_chunk();
+
+if CFG_SOURCEDIR then
+       package.path = CFG_SOURCEDIR.."/?.lua;"..package.path;
+       package.cpath = CFG_SOURCEDIR.."/?.so;"..package.cpath;
+elseif not package.loaded["util.json"] then
+       package.path = "../../?.lua;"..package.path
+       package.cpath = "../../?.so;"..package.cpath
+end
+
+local have_err;
+if #arg > 0 and #arg ~= 2 then
+       have_err = true;
+       print("Error: Incorrect number of parameters supplied.");
+end
+if not config[from_store] then
+       have_err = true;
+       print("Error: Input store '"..from_store.."' not found in the config file.");
+end
+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);
+end
+
+if have_err then
+       print("");
+       print("Usage: "..arg[0].." FROM_STORE TO_STORE");
+       print("If no stores are specified, 'input' and 'output' are used.");
+       print("");
+       print("The available stores in your migrator config are:");
+       print("");
+       for store in pairs(config) do
+               print("", store);
+       end
+       print("");
+       os.exit(1);
+end
+       
+local itype = config[from_store].type;
+local otype = config[to_store].type;
+local reader = require("migrator."..itype).reader(config[from_store]);
+local writer = require("migrator."..otype).writer(config[to_store]);
+
+local json = require "util.json";
+
+for x in reader do
+       --print(json.encode(x))
+       writer(x);
+end
+writer(nil); -- close
+