Software /
code /
prosody
Comparison
tools/migration/main.lua @ 4211:9a12fc2baa37
tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 25 Feb 2011 03:32:44 +0000 |
parent | 4210:4583473dcce4 |
child | 4216:ff80a8471e86 |
comparison
equal
deleted
inserted
replaced
4210:4583473dcce4 | 4211:9a12fc2baa37 |
---|---|
1 local default_config = "./migrator.cfg.lua"; | |
2 | |
1 -- Command-line parsing | 3 -- Command-line parsing |
2 local options = {}; | 4 local options = {}; |
3 local handled_opts = 0; | 5 local handled_opts = 0; |
4 for i = 1, #arg do | 6 for i = 1, #arg do |
5 if arg[i]:sub(1,2) == "--" then | 7 if arg[i]:sub(1,2) == "--" then |
13 end | 15 end |
14 end | 16 end |
15 table.remove(arg, handled_opts); | 17 table.remove(arg, handled_opts); |
16 | 18 |
17 -- Load config file | 19 -- Load config file |
18 local function loadfilein(file, env) return loadin and loadin(env, io.open(file):read("*a")) or setfenv(loadfile(file), env); end | 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 | |
19 config = {}; | 36 config = {}; |
20 local config_env = setmetatable({}, { __index = function(t, k) return function(tbl) config[k] = tbl; end; end }); | 37 local config_env = setmetatable({}, { __index = function(t, k) return function(tbl) config[k] = tbl; end; end }); |
21 loadfilein(options.config or "config.lua", config_env)(); | 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(); | |
22 | 47 |
23 if not package.loaded["util.json"] then | 48 if not package.loaded["util.json"] then |
24 package.path = "../../?.lua;"..package.path | 49 package.path = "../../?.lua;"..package.path |
25 package.cpath = "../../?.dll;"..package.cpath | 50 package.cpath = "../../?.dll;"..package.cpath |
26 end | 51 end |
27 | 52 |
28 local from_store = arg[1] or "input"; | 53 local have_err; |
29 local to_store = arg[2] or "output"; | 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 | |
30 | 80 |
31 assert(config[from_store], "no input specified") | 81 if have_err then |
32 assert(config[to_store], "no output specified") | 82 print(""); |
33 local itype = assert(config[from_store].type, "no type specified for "..from_store); | 83 print("Usage: "..arg[0].." FROM_STORE TO_STORE"); |
34 local otype = assert(config[to_store].type, "no type specified for "..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; | |
35 local reader = require(itype).reader(config[from_store]); | 96 local reader = require(itype).reader(config[from_store]); |
36 local writer = require(otype).writer(config[to_store]); | 97 local writer = require(otype).writer(config[to_store]); |
37 | 98 |
38 local json = require "util.json"; | 99 local json = require "util.json"; |
39 | 100 |