# HG changeset patch # User Daniel Poelzleithner # Date 1284852803 -7200 # Node ID eeb167cf9dc464daa577984835fa0d14c9f633ed # Parent c5c0b53f664ba8ba2dd58e07b428e7ccaa5a27e0 add comand line admin tool started a command line admin tool to help development. currently supports setting login passwords and list users diff -r c5c0b53f664b -r eeb167cf9dc4 bin/liquid-admin.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bin/liquid-admin.lua Sun Sep 19 01:33:23 2010 +0200 @@ -0,0 +1,86 @@ +#!/usr/bin/env lua + +OptionParser = require("utils.optparse") + +COMMANDS = {{"setpwd ", "Set a user command"}, + {"listuser", "List usernames"}, + } + +t={usage="", version="", commands=COMMANDS} +op=OptionParser(t) +--op.add_option({"-t", action="safe_true", dest="test", help=""}) +op.add_option({"-c", action="store", dest="config", help="config name to use", default="default"}) +op.add_option({"-w", action="store", dest="webmcp", help="path to webmcp", default="../webmcp"}) + +options,args = op.parse_args() + +if #args == 0 then + print("Error: command is required\n") + op.print_help() + return +end + +-- dirty dirty dirty, dirty dirty, dirty dirty dow monkey patch env +if not os.setenv then + + local env, getenv = { }, os.getenv + + function os.setenv(k,v) + env[k] = v + end + + function os.getenv(k) + return env[k] or getenv(k) + end + +end + +-- detect current path FIXME: platform portable +local PWD = io.popen("pwd"):read() +os.setenv("WEBMCP_APP_BASEPATH", PWD) +os.setenv("WEBMCP_CONFIG_NAME", options.config) +os.setenv("WEBMCP_INTERACTIVE", "yes") + +-- load webmcp framework +WEBMCP_PATH = options.webmcp .. "/framework/" +dofile(options.webmcp .. "/framework/cgi-bin/webmcp.lua") + +function error(why) + print(why) + os.exit(2) +end + +if args[1] == "setpwd" then + if #args < 2 then + error("login is required") + end + require("model.member") + user = Member:by_login(args[2]) + if not user then + error("User "..args[2].." not found") + end + print("Enter password:") + password = io.read() + if password then + user:set_password(password) + user:save() + end +end + +if args[1] == "listusers" then + require("model.member") + sel = Member:new_selector() + users = sel:exec() + --sel:optional_object_mode() + print("Login Active") + for i,v in pairs(users) do + if v.login then + print(v.login .. string.rep(" ", 25-#v.login), v.active) + end + end +end + + + + + diff -r c5c0b53f664b -r eeb167cf9dc4 utils/optparse.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/utils/optparse.lua Sun Sep 19 01:33:23 2010 +0200 @@ -0,0 +1,139 @@ +-- Lua command line option parser. +-- Interface based on Pythons optparse. +-- http://docs.python.org/lib/module-optparse.html +-- (c) 2008 David Manura, Licensed under the same terms as Lua (MIT license) +-- +-- To be used like this: +-- t={usage="", version=""} +-- op=OptionParser(t) +-- op=add_option{"", action=, dest=, help=""} +-- +-- with : +-- the option string to be used (can be anything, if one letter opt, then should be -x val, more letters: -xy=val ) +-- one of +-- - store: store in options as key, val +-- - store_true: stores key, true +-- - store_false: stores key, false +-- is the key under which the option is saved +-- +-- options,args = op.parse_args() +-- +-- now options is the table of options (key, val) and args is the table with non-option arguments. +-- You can use op.fail(message) for failing and op.print_help() for printing the usage as you like. + +--module('utils.optparse') + +local function OptionParser(t) + local usage = t.usage + local version = t.version + local commands = t.commands + + local o = {} + local option_descriptions = {} + local option_of = {} + + function o.fail(s) -- extension + io.stderr:write(s .. '\n') + os.exit(1) + end + + function o.add_option(optdesc) + option_descriptions[#option_descriptions+1] = optdesc + for _,v in pairs(optdesc) do + option_of[v] = optdesc + end + end + function o.parse_args() + -- expand options (e.g. "--input=file" -> "--input", "file") + local arg = {unpack(arg)} + for i=#arg,1,-1 do local v = arg[i] + local flag, val = v:match('^(%-%-%w+)=(.*)') + if flag then + arg[i] = flag + table.insert(arg, i+1, val) + end + end + + local options = {} + for _,optdesc in ipairs(option_descriptions) do + options[optdesc["dest"]] = optdesc.default + end + local args = {} + local i = 1 + while i <= #arg do local v = arg[i] + local optdesc = option_of[v] + if optdesc then + local action = optdesc.action + local val + if action == 'store' or action == nil then + i = i + 1 + val = arg[i] + if not val then o.fail('option requires an argument ' .. v) end + elseif action == 'store_true' then + val = true + elseif action == 'store_false' then + val = false + end + options[optdesc.dest] = val + else + if v:match('^%-') then o.fail('invalid option ' .. v) end + args[#args+1] = v + end + i = i + 1 + end + if options.help then + o.print_help() + os.exit() + end + if options.version then + io.stdout:write(t.version .. "\n") + os.exit() + end + return options, args + end + + local function flags_str(optdesc) + local sflags = {} + local action = optdesc.action + for _,flag in ipairs(optdesc) do + local sflagend + if action == nil or action == 'store' then + local metavar = optdesc.metavar or optdesc.dest:upper() + sflagend = #flag == 2 and ' ' .. metavar + or '=' .. metavar + else + sflagend = '' + end + sflags[#sflags+1] = flag .. sflagend + end + return table.concat(sflags, ', ') + end + + function o.print_help() + io.stdout:write("Usage: " .. usage:gsub('%%prog', arg[0]) .. "\n") + io.stdout:write("\n") + io.stdout:write("Options:\n") + for _,optdesc in ipairs(option_descriptions) do + io.stdout:write(" " .. flags_str(optdesc) .. + " " .. optdesc.help .. "\n") + end + if commands then + io.stdout:write("\nCommands:\n") + for _,command in ipairs(commands) do + io.stdout:write(" " .. command[1] .. + string.rep(" ", 30-#command[1]) .. + command[2] .. "\n") + end + end + + end + o.add_option{"--help", action="store_true", dest="help", + help="show this help message and exit"} + if t.version then + o.add_option{"--version", action="store_true", dest="version", + help="output version info."} + end + return o +end + +return OptionParser \ No newline at end of file