Annotate

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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4210
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
1 -- Command-line parsing
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
2 local options = {};
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
3 local handled_opts = 0;
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
4 for i = 1, #arg do
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
5 if arg[i]:sub(1,2) == "--" then
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
6 local opt, val = arg[i]:match("([%w-]+)=?(.*)");
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
7 if opt then
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
8 options[(opt:sub(3):gsub("%-", "_"))] = #val > 0 and val or true;
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
9 end
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
10 handled_opts = i;
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
11 else
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
12 break;
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
13 end
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
14 end
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
15 table.remove(arg, handled_opts);
4166
3ac90743039b tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4162
diff changeset
16
4210
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
17 -- Load config file
4166
3ac90743039b tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4162
diff changeset
18 local function loadfilein(file, env) return loadin and loadin(env, io.open(file):read("*a")) or setfenv(loadfile(file), env); end
3ac90743039b tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4162
diff changeset
19 config = {};
3ac90743039b tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4162
diff changeset
20 local config_env = setmetatable({}, { __index = function(t, k) return function(tbl) config[k] = tbl; end; end });
4210
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
21 loadfilein(options.config or "config.lua", config_env)();
4166
3ac90743039b tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4162
diff changeset
22
4210
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
23 if not package.loaded["util.json"] then
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
24 package.path = "../../?.lua;"..package.path
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
25 package.cpath = "../../?.dll;"..package.cpath
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
26 end
4166
3ac90743039b tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4162
diff changeset
27
4210
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
28 local from_store = arg[1] or "input";
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
29 local to_store = arg[2] or "output";
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
30
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
31 assert(config[from_store], "no input specified")
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
32 assert(config[to_store], "no output specified")
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
33 local itype = assert(config[from_store].type, "no type specified for "..from_store);
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
34 local otype = assert(config[to_store].type, "no type specified for "..to_store);
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
35 local reader = require(itype).reader(config[from_store]);
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
36 local writer = require(otype).writer(config[to_store]);
4166
3ac90743039b tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4162
diff changeset
37
3ac90743039b tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4162
diff changeset
38 local json = require "util.json";
3ac90743039b tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4162
diff changeset
39
3ac90743039b tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4162
diff changeset
40 for x in reader do
3ac90743039b tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4162
diff changeset
41 --print(json.encode(x))
3ac90743039b tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4162
diff changeset
42 writer(x);
3ac90743039b tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4162
diff changeset
43 end
3ac90743039b tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4162
diff changeset
44 writer(nil); -- close
3ac90743039b tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4162
diff changeset
45