tools/migration/prosody-migrator.lua: Refactor store handler loading to report errors...
[prosody.git] / tools / migration / prosody-migrator.lua
1 #!/usr/bin/env lua
2
3 CFG_SOURCEDIR=os.getenv("PROSODY_SRCDIR");
4 CFG_CONFIGDIR=os.getenv("PROSODY_CFGDIR");
5
6 local default_config = (CFG_CONFIGDIR or ".").."/migrator.cfg.lua";
7
8 -- Command-line parsing
9 local options = {};
10 local handled_opts = 0;
11 for i = 1, #arg do
12         if arg[i]:sub(1,2) == "--" then
13                 local opt, val = arg[i]:match("([%w-]+)=?(.*)");
14                 if opt then
15                         options[(opt:sub(3):gsub("%-", "_"))] = #val > 0 and val or true;
16                 end
17                 handled_opts = i;
18         else
19                 break;
20         end
21 end
22 table.remove(arg, handled_opts);
23
24 -- Load config file
25 local function loadfilein(file, env)
26         if loadin then
27                 return loadin(env, io.open(file):read("*a"));
28         else
29                 local chunk, err = loadfile(file);
30                 if chunk then
31                         setfenv(chunk, env);
32                 end
33                 return chunk, err;
34         end
35 end
36
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";
40
41 config = {};
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:");
47         print("", err);
48         os.exit(1);
49 end
50
51 config_chunk();
52
53 if CFG_SOURCEDIR then
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
59 end
60
61 local have_err;
62 if #arg > 0 and #arg ~= 2 then
63         have_err = true;
64         print("Error: Incorrect number of parameters supplied.");
65 end
66 if not config[from_store] then
67         have_err = true;
68         print("Error: Input store '"..from_store.."' not found in the config file.");
69 end
70 if not config[to_store] then
71         have_err = true;
72         print("Error: Output store '"..to_store.."' not found in the config file.");
73 end
74
75 function load_store_handler(name)
76         local store_type = config[name].type;
77         if not store_type then
78                 print("Error: "..name.." store type not specified in the config file");
79                 return false;
80         else
81                 local ok, err = pcall(require, "migrator."..store_type);
82                 if not ok then
83                         if package.loaded["migrator."..store_type] then
84                                 print(("Error: Failed to initialize '%s' store:\n\t%s")
85                                         :format(name, err));
86                         else
87                                 print(("Error: Unrecognised store type for '%s': %s")
88                                         :format(from_store, store_type));
89                         end
90                         return false;
91                 end
92         end
93         return true;
94 end
95
96 have_err = have_err or not(load_store_handler(from_store, "input") and load_store_handler(to_store, "output"));
97
98 if have_err then
99         print("");
100         print("Usage: "..arg[0].." FROM_STORE TO_STORE");
101         print("If no stores are specified, 'input' and 'output' are used.");
102         print("");
103         print("The available stores in your migrator config are:");
104         print("");
105         for store in pairs(config) do
106                 print("", store);
107         end
108         print("");
109         os.exit(1);
110 end
111         
112 local itype = config[from_store].type;
113 local otype = config[to_store].type;
114 local reader = require("migrator."..itype).reader(config[from_store]);
115 local writer = require("migrator."..otype).writer(config[to_store]);
116
117 local json = require "util.json";
118
119 for x in reader do
120         --print(json.encode(x))
121         writer(x);
122 end
123 writer(nil); -- close
124