Comparison

tools/migration/main.lua @ 4210:4583473dcce4

tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
author Matthew Wild <mwild1@gmail.com>
date Fri, 25 Feb 2011 01:31:08 +0000
parent 4166:3ac90743039b
child 4211:9a12fc2baa37
comparison
equal deleted inserted replaced
4209:df753c398aa0 4210:4583473dcce4
1 -- Command-line parsing
2 local options = {};
3 local handled_opts = 0;
4 for i = 1, #arg do
5 if arg[i]:sub(1,2) == "--" then
6 local opt, val = arg[i]:match("([%w-]+)=?(.*)");
7 if opt then
8 options[(opt:sub(3):gsub("%-", "_"))] = #val > 0 and val or true;
9 end
10 handled_opts = i;
11 else
12 break;
13 end
14 end
15 table.remove(arg, handled_opts);
1 16
2 17 -- Load config file
3
4 local function loadfilein(file, env) return loadin and loadin(env, io.open(file):read("*a")) or setfenv(loadfile(file), env); end 18 local function loadfilein(file, env) return loadin and loadin(env, io.open(file):read("*a")) or setfenv(loadfile(file), env); end
5 config = {}; 19 config = {};
6 local config_env = setmetatable({}, { __index = function(t, k) return function(tbl) config[k] = tbl; end; end }); 20 local config_env = setmetatable({}, { __index = function(t, k) return function(tbl) config[k] = tbl; end; end });
7 loadfilein("config.lua", config_env)(); 21 loadfilein(options.config or "config.lua", config_env)();
8 22
9 package.path = "../../?.lua;"..package.path 23 if not package.loaded["util.json"] then
10 package.cpath = "../../?.dll;"..package.cpath 24 package.path = "../../?.lua;"..package.path
25 package.cpath = "../../?.dll;"..package.cpath
26 end
11 27
28 local from_store = arg[1] or "input";
29 local to_store = arg[2] or "output";
12 30
13 assert(config.input, "no input specified") 31 assert(config[from_store], "no input specified")
14 assert(config.output, "no output specified") 32 assert(config[to_store], "no output specified")
15 local itype = assert(config.input.type, "no input.type specified"); 33 local itype = assert(config[from_store].type, "no type specified for "..from_store);
16 local otype = assert(config.output.type, "no output.type specified"); 34 local otype = assert(config[to_store].type, "no type specified for "..to_store);
17 local reader = require(itype).reader(config.input); 35 local reader = require(itype).reader(config[from_store]);
18 local writer = require(otype).writer(config.output); 36 local writer = require(otype).writer(config[to_store]);
19 37
20 local json = require "util.json"; 38 local json = require "util.json";
21 39
22 for x in reader do 40 for x in reader do
23 --print(json.encode(x)) 41 --print(json.encode(x))