tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for...
[prosody.git] / tools / migration / main.lua
1 local default_config = "./migrator.cfg.lua";
2
3 -- Command-line parsing
4 local options = {};
5 local handled_opts = 0;
6 for i = 1, #arg do
7         if arg[i]:sub(1,2) == "--" then
8                 local opt, val = arg[i]:match("([%w-]+)=?(.*)");
9                 if opt then
10                         options[(opt:sub(3):gsub("%-", "_"))] = #val > 0 and val or true;
11                 end
12                 handled_opts = i;
13         else
14                 break;
15         end
16 end
17 table.remove(arg, handled_opts);
18
19 -- Load config file
20 local function loadfilein(file, env)
21         if loadin then
22                 return loadin(env, io.open(file):read("*a"));
23         else
24                 local chunk, err = loadfile(file);
25                 if chunk then
26                         setfenv(chunk, env);
27                 end
28                 return chunk, err;
29         end
30 end
31
32 local config_file = options.config or default_config;
33 local from_store = arg[1] or "input";
34 local to_store = arg[2] or "output";
35
36 config = {};
37 local config_env = setmetatable({}, { __index = function(t, k) return function(tbl) config[k] = tbl; end; end });
38 local config_chunk, err = loadfilein(config_file, config_env);
39 if not config_chunk then
40         print("There was an error loading the config file, check the file exists");
41         print("and that the syntax is correct:");
42         print("", err);
43         os.exit(1);
44 end
45
46 config_chunk();
47
48 if not package.loaded["util.json"] then
49         package.path = "../../?.lua;"..package.path
50         package.cpath = "../../?.dll;"..package.cpath
51 end
52
53 local have_err;
54 if #arg > 0 and #arg ~= 2 then
55         have_err = true;
56         print("Error: Incorrect number of parameters supplied.");
57 end
58 if not config[from_store] then
59         have_err = true;
60         print("Error: Input store '"..from_store.."' not found in the config file.");
61 end
62 if not config[to_store] then
63         have_err = true;
64         print("Error: Output store '"..to_store.."' not found in the config file.");
65 end
66 if not config[from_store].type then
67         have_err = true;
68         print("Error: Input store type not specified in the config file");
69 elseif not pcall(require, config[from_store].type) then
70         have_err = true;
71         print("Error: Unrecognised store type for '"..from_store.."': "..config[from_store].type);
72 end
73 if not config[to_store].type then
74         have_err = true;
75         print("Error: Output store type not specified in the config file");
76 elseif not pcall(require, config[to_store].type) then
77         have_err = true;
78         print("Error: Unrecognised store type for '"..to_store.."': "..config[to_store].type);
79 end
80
81 if have_err then
82         print("");
83         print("Usage: "..arg[0].." FROM_STORE TO_STORE");
84         print("If no stores are specified, 'input' and 'output' are used.");
85         print("");
86         print("The available stores in your migrator config are:");
87         print("");
88         for store in pairs(config) do
89                 print("", store);
90         end
91         os.exit(1);
92 end
93         
94 local itype = config[from_store].type;
95 local otype = config[to_store].type;
96 local reader = require(itype).reader(config[from_store]);
97 local writer = require(otype).writer(config[to_store]);
98
99 local json = require "util.json";
100
101 for x in reader do
102         --print(json.encode(x))
103         writer(x);
104 end
105 writer(nil); -- close
106