Remove all trailing whitespace
[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 -- Substitute ~ with path to home directory in paths
7 if CFG_CONFIGDIR then
8         CFG_CONFIGDIR = CFG_CONFIGDIR:gsub("^~", os.getenv("HOME"));
9 end
10
11 if CFG_SOURCEDIR then
12         CFG_SOURCEDIR = CFG_SOURCEDIR:gsub("^~", os.getenv("HOME"));
13 end
14
15 local default_config = (CFG_CONFIGDIR or ".").."/migrator.cfg.lua";
16
17 -- Command-line parsing
18 local options = {};
19 local handled_opts = 0;
20 for i = 1, #arg do
21         if arg[i]:sub(1,2) == "--" then
22                 local opt, val = arg[i]:match("([%w-]+)=?(.*)");
23                 if opt then
24                         options[(opt:sub(3):gsub("%-", "_"))] = #val > 0 and val or true;
25                 end
26                 handled_opts = i;
27         else
28                 break;
29         end
30 end
31 table.remove(arg, handled_opts);
32
33 if CFG_SOURCEDIR then
34         package.path = CFG_SOURCEDIR.."/?.lua;"..package.path;
35         package.cpath = CFG_SOURCEDIR.."/?.so;"..package.cpath;
36 else
37         package.path = "../../?.lua;"..package.path
38         package.cpath = "../../?.so;"..package.cpath
39 end
40
41 local envloadfile = require "util.envload".envloadfile;
42
43 -- Load config file
44 local function loadfilein(file, env)
45         if loadin then
46                 return loadin(env, io.open(file):read("*a"));
47         else
48                 return envloadfile(file, env);
49         end
50 end
51
52 local config_file = options.config or default_config;
53 local from_store = arg[1] or "input";
54 local to_store = arg[2] or "output";
55
56 config = {};
57 local config_env = setmetatable({}, { __index = function(t, k) return function(tbl) config[k] = tbl; end; end });
58 local config_chunk, err = loadfilein(config_file, config_env);
59 if not config_chunk then
60         print("There was an error loading the config file, check the file exists");
61         print("and that the syntax is correct:");
62         print("", err);
63         os.exit(1);
64 end
65
66 config_chunk();
67
68 local have_err;
69 if #arg > 0 and #arg ~= 2 then
70         have_err = true;
71         print("Error: Incorrect number of parameters supplied.");
72 end
73 if not config[from_store] then
74         have_err = true;
75         print("Error: Input store '"..from_store.."' not found in the config file.");
76 end
77 if not config[to_store] then
78         have_err = true;
79         print("Error: Output store '"..to_store.."' not found in the config file.");
80 end
81
82 function load_store_handler(name)
83         local store_type = config[name].type;
84         if not store_type then
85                 print("Error: "..name.." store type not specified in the config file");
86                 return false;
87         else
88                 local ok, err = pcall(require, "migrator."..store_type);
89                 if not ok then
90                         if package.loaded["migrator."..store_type] then
91                                 print(("Error: Failed to initialize '%s' store:\n\t%s")
92                                         :format(name, err));
93                         else
94                                 print(("Error: Unrecognised store type for '%s': %s")
95                                         :format(from_store, store_type));
96                         end
97                         return false;
98                 end
99         end
100         return true;
101 end
102
103 have_err = have_err or not(load_store_handler(from_store, "input") and load_store_handler(to_store, "output"));
104
105 if have_err then
106         print("");
107         print("Usage: "..arg[0].." FROM_STORE TO_STORE");
108         print("If no stores are specified, 'input' and 'output' are used.");
109         print("");
110         print("The available stores in your migrator config are:");
111         print("");
112         for store in pairs(config) do
113                 print("", store);
114         end
115         print("");
116         os.exit(1);
117 end
118
119 local itype = config[from_store].type;
120 local otype = config[to_store].type;
121 local reader = require("migrator."..itype).reader(config[from_store]);
122 local writer = require("migrator."..otype).writer(config[to_store]);
123
124 local json = require "util.json";
125
126 io.stderr:write("Migrating...\n");
127 for x in reader do
128         --print(json.encode(x))
129         writer(x);
130 end
131 writer(nil); -- close
132 io.stderr:write("Done!\n");