From a33886bcb1257fb0629c64658036d232623c6d1e Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 25 Feb 2011 01:29:31 +0000 Subject: tools/migration/config.lua: Minor fixes for more sensible defaults --- tools/migration/config.lua | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tools/migration/config.lua b/tools/migration/config.lua index 3f2057f0..fa37f2a3 100644 --- a/tools/migration/config.lua +++ b/tools/migration/config.lua @@ -1,26 +1,26 @@ +local data_path = "../../data"; + input { - type = "prosody_sql"; - driver = "SQLite3"; - database = "out.sqlite"; + type = "prosody_files"; + path = data_path; } + output { - type = "prosody_files"; - path = "out"; + type = "prosody_sql"; + driver = "SQLite3"; + database = data_path.."/prosody.sqlite"; } --[[ input { - path = "../../data"; type = "prosody_files"; - driver = "SQLite3"; - database = "../../prosody.sqlite"; + path = data_path; } output { type = "prosody_sql"; driver = "SQLite3"; - database = "out.sqlite"; - path = "out"; + database = data_path.."/prosody.sqlite"; } ]] -- cgit v1.2.3 From 7c054e5160dc6e2a42ee8e66b9031f57c634421f Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 25 Feb 2011 01:31:08 +0000 Subject: tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate --- tools/migration/main.lua | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/tools/migration/main.lua b/tools/migration/main.lua index a88da21e..772349bf 100644 --- a/tools/migration/main.lua +++ b/tools/migration/main.lua @@ -1,21 +1,39 @@ +-- Command-line parsing +local options = {}; +local handled_opts = 0; +for i = 1, #arg do + if arg[i]:sub(1,2) == "--" then + local opt, val = arg[i]:match("([%w-]+)=?(.*)"); + if opt then + options[(opt:sub(3):gsub("%-", "_"))] = #val > 0 and val or true; + end + handled_opts = i; + else + break; + end +end +table.remove(arg, handled_opts); - - +-- Load config file local function loadfilein(file, env) return loadin and loadin(env, io.open(file):read("*a")) or setfenv(loadfile(file), env); end config = {}; local config_env = setmetatable({}, { __index = function(t, k) return function(tbl) config[k] = tbl; end; end }); -loadfilein("config.lua", config_env)(); +loadfilein(options.config or "config.lua", config_env)(); -package.path = "../../?.lua;"..package.path -package.cpath = "../../?.dll;"..package.cpath +if not package.loaded["util.json"] then + package.path = "../../?.lua;"..package.path + package.cpath = "../../?.dll;"..package.cpath +end +local from_store = arg[1] or "input"; +local to_store = arg[2] or "output"; -assert(config.input, "no input specified") -assert(config.output, "no output specified") -local itype = assert(config.input.type, "no input.type specified"); -local otype = assert(config.output.type, "no output.type specified"); -local reader = require(itype).reader(config.input); -local writer = require(otype).writer(config.output); +assert(config[from_store], "no input specified") +assert(config[to_store], "no output specified") +local itype = assert(config[from_store].type, "no type specified for "..from_store); +local otype = assert(config[to_store].type, "no type specified for "..to_store); +local reader = require(itype).reader(config[from_store]); +local writer = require(otype).writer(config[to_store]); local json = require "util.json"; -- cgit v1.2.3 From 35a339b1d3defc1be8387324a601db4889096a22 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 25 Feb 2011 03:32:44 +0000 Subject: tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters --- tools/migration/config.lua | 26 -------------- tools/migration/main.lua | 77 +++++++++++++++++++++++++++++++++++----- tools/migration/migrator.cfg.lua | 26 ++++++++++++++ 3 files changed, 95 insertions(+), 34 deletions(-) delete mode 100644 tools/migration/config.lua create mode 100644 tools/migration/migrator.cfg.lua diff --git a/tools/migration/config.lua b/tools/migration/config.lua deleted file mode 100644 index fa37f2a3..00000000 --- a/tools/migration/config.lua +++ /dev/null @@ -1,26 +0,0 @@ -local data_path = "../../data"; - -input { - type = "prosody_files"; - path = data_path; -} - -output { - type = "prosody_sql"; - driver = "SQLite3"; - database = data_path.."/prosody.sqlite"; -} - ---[[ - -input { - type = "prosody_files"; - path = data_path; -} -output { - type = "prosody_sql"; - driver = "SQLite3"; - database = data_path.."/prosody.sqlite"; -} - -]] diff --git a/tools/migration/main.lua b/tools/migration/main.lua index 772349bf..e4891966 100644 --- a/tools/migration/main.lua +++ b/tools/migration/main.lua @@ -1,3 +1,5 @@ +local default_config = "./migrator.cfg.lua"; + -- Command-line parsing local options = {}; local handled_opts = 0; @@ -15,23 +17,82 @@ end table.remove(arg, handled_opts); -- Load config file -local function loadfilein(file, env) return loadin and loadin(env, io.open(file):read("*a")) or setfenv(loadfile(file), env); end +local function loadfilein(file, env) + if loadin then + return loadin(env, io.open(file):read("*a")); + else + local chunk, err = loadfile(file); + if chunk then + setfenv(chunk, env); + end + return chunk, err; + end +end + +local config_file = options.config or default_config; +local from_store = arg[1] or "input"; +local to_store = arg[2] or "output"; + config = {}; local config_env = setmetatable({}, { __index = function(t, k) return function(tbl) config[k] = tbl; end; end }); -loadfilein(options.config or "config.lua", config_env)(); +local config_chunk, err = loadfilein(config_file, config_env); +if not config_chunk then + print("There was an error loading the config file, check the file exists"); + print("and that the syntax is correct:"); + print("", err); + os.exit(1); +end + +config_chunk(); if not package.loaded["util.json"] then package.path = "../../?.lua;"..package.path package.cpath = "../../?.dll;"..package.cpath end -local from_store = arg[1] or "input"; -local to_store = arg[2] or "output"; +local have_err; +if #arg > 0 and #arg ~= 2 then + have_err = true; + print("Error: Incorrect number of parameters supplied."); +end +if not config[from_store] then + have_err = true; + print("Error: Input store '"..from_store.."' not found in the config file."); +end +if not config[to_store] then + have_err = true; + print("Error: Output store '"..to_store.."' not found in the config file."); +end +if not config[from_store].type then + have_err = true; + print("Error: Input store type not specified in the config file"); +elseif not pcall(require, config[from_store].type) then + have_err = true; + print("Error: Unrecognised store type for '"..from_store.."': "..config[from_store].type); +end +if not config[to_store].type then + have_err = true; + print("Error: Output store type not specified in the config file"); +elseif not pcall(require, config[to_store].type) then + have_err = true; + print("Error: Unrecognised store type for '"..to_store.."': "..config[to_store].type); +end -assert(config[from_store], "no input specified") -assert(config[to_store], "no output specified") -local itype = assert(config[from_store].type, "no type specified for "..from_store); -local otype = assert(config[to_store].type, "no type specified for "..to_store); +if have_err then + print(""); + print("Usage: "..arg[0].." FROM_STORE TO_STORE"); + print("If no stores are specified, 'input' and 'output' are used."); + print(""); + print("The available stores in your migrator config are:"); + print(""); + for store in pairs(config) do + print("", store); + end + os.exit(1); +end + +local itype = config[from_store].type; +local otype = config[to_store].type; local reader = require(itype).reader(config[from_store]); local writer = require(otype).writer(config[to_store]); diff --git a/tools/migration/migrator.cfg.lua b/tools/migration/migrator.cfg.lua new file mode 100644 index 00000000..fa37f2a3 --- /dev/null +++ b/tools/migration/migrator.cfg.lua @@ -0,0 +1,26 @@ +local data_path = "../../data"; + +input { + type = "prosody_files"; + path = data_path; +} + +output { + type = "prosody_sql"; + driver = "SQLite3"; + database = data_path.."/prosody.sqlite"; +} + +--[[ + +input { + type = "prosody_files"; + path = data_path; +} +output { + type = "prosody_sql"; + driver = "SQLite3"; + database = data_path.."/prosody.sqlite"; +} + +]] -- cgit v1.2.3