liquid_feedback_frontend

changeset 107:eeb167cf9dc4

add comand line admin tool

started a command line admin tool to help development.
currently supports setting login passwords and list users
author Daniel Poelzleithner <poelzi@poelzi.org>
date Sun Sep 19 01:33:23 2010 +0200 (2010-09-19)
parents c5c0b53f664b
children 4d1116ba5a8d
files bin/liquid-admin.lua utils/optparse.lua
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/bin/liquid-admin.lua	Sun Sep 19 01:33:23 2010 +0200
     1.3 @@ -0,0 +1,86 @@
     1.4 +#!/usr/bin/env lua
     1.5 +
     1.6 +OptionParser = require("utils.optparse")
     1.7 +
     1.8 +COMMANDS = {{"setpwd <username> <password>", "Set a user command"}, 
     1.9 +            {"listuser", "List usernames"},
    1.10 +           }
    1.11 +
    1.12 +t={usage="<some usage message>", version="<version string>", commands=COMMANDS}
    1.13 +op=OptionParser(t)
    1.14 +--op.add_option({"-t", action="safe_true", dest="test", help="<help message for this option>"})
    1.15 +op.add_option({"-c", action="store", dest="config", help="config name to use", default="default"})
    1.16 +op.add_option({"-w", action="store", dest="webmcp", help="path to webmcp", default="../webmcp"})
    1.17 +
    1.18 +options,args = op.parse_args()
    1.19 +
    1.20 +if #args == 0 then
    1.21 +    print("Error: command is required\n")
    1.22 +    op.print_help()
    1.23 +    return
    1.24 +end
    1.25 +
    1.26 +-- dirty dirty dirty, dirty dirty, dirty dirty dow monkey patch env
    1.27 +if not os.setenv then
    1.28 +
    1.29 +    local env, getenv = { }, os.getenv
    1.30 +
    1.31 +    function os.setenv(k,v)
    1.32 +        env[k] = v
    1.33 +    end
    1.34 +
    1.35 +    function os.getenv(k)
    1.36 +        return env[k] or getenv(k)
    1.37 +    end
    1.38 +
    1.39 +end
    1.40 +
    1.41 +-- detect current path FIXME: platform portable
    1.42 +local PWD = io.popen("pwd"):read()
    1.43 +os.setenv("WEBMCP_APP_BASEPATH", PWD)
    1.44 +os.setenv("WEBMCP_CONFIG_NAME", options.config)
    1.45 +os.setenv("WEBMCP_INTERACTIVE", "yes")
    1.46 +
    1.47 +-- load webmcp framework
    1.48 +WEBMCP_PATH = options.webmcp .. "/framework/"
    1.49 +dofile(options.webmcp .. "/framework/cgi-bin/webmcp.lua")
    1.50 +
    1.51 +function error(why)
    1.52 +    print(why)
    1.53 +    os.exit(2)
    1.54 +end
    1.55 +
    1.56 +if args[1] == "setpwd" then
    1.57 +  if #args < 2 then
    1.58 +    error("login is required")
    1.59 +  end
    1.60 +  require("model.member")
    1.61 +  user = Member:by_login(args[2])
    1.62 +  if not user then
    1.63 +    error("User "..args[2].." not found")
    1.64 +  end
    1.65 +  print("Enter password:")
    1.66 +  password = io.read()
    1.67 +  if password then
    1.68 +    user:set_password(password)
    1.69 +    user:save()
    1.70 +  end
    1.71 +end
    1.72 +
    1.73 +if args[1] == "listusers" then
    1.74 +  require("model.member")
    1.75 +  sel = Member:new_selector()
    1.76 +  users = sel:exec()
    1.77 +  --sel:optional_object_mode()
    1.78 +  print("Login                           Active")
    1.79 +  for i,v in pairs(users) do
    1.80 +    if v.login then
    1.81 +      print(v.login .. string.rep(" ", 25-#v.login), v.active)
    1.82 +    end
    1.83 +  end
    1.84 +end
    1.85 +
    1.86 +
    1.87 +
    1.88 +
    1.89 +
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/utils/optparse.lua	Sun Sep 19 01:33:23 2010 +0200
     2.3 @@ -0,0 +1,139 @@
     2.4 +-- Lua command line option parser.
     2.5 +-- Interface based on Pythons optparse.
     2.6 +-- http://docs.python.org/lib/module-optparse.html
     2.7 +-- (c) 2008 David Manura, Licensed under the same terms as Lua (MIT license)
     2.8 +--
     2.9 +-- To be used like this:                                                                  
    2.10 +-- t={usage="<some usage message>", version="<version string>"}                           
    2.11 +-- op=OptionParser(t)                                                                     
    2.12 +-- op=add_option{"<opt>", action=<action>, dest=<dest>, help="<help message for this option>"}
    2.13 +--
    2.14 +-- with :
    2.15 +--   <opt> the option string to be used (can be anything, if one letter opt, then should be -x val, more letters: -xy=val )
    2.16 +--   <action> one of
    2.17 +--   - store: store in options as key, val                                                  
    2.18 +--   - store_true: stores key, true                                                         
    2.19 +--   - store_false: stores key, false
    2.20 +--   <dest> is the key under which the option is saved
    2.21 +--                                      
    2.22 +-- options,args = op.parse_args()
    2.23 +--
    2.24 +-- now options is the table of options (key, val) and args is the table with non-option arguments.
    2.25 +-- You can use op.fail(message) for failing and op.print_help() for printing the usage as you like.
    2.26 +
    2.27 +--module('utils.optparse')
    2.28 +
    2.29 +local function OptionParser(t)
    2.30 +  local usage = t.usage
    2.31 +  local version = t.version
    2.32 +  local commands = t.commands
    2.33 +
    2.34 +  local o = {}
    2.35 +  local option_descriptions = {}
    2.36 +  local option_of = {}
    2.37 +
    2.38 +  function o.fail(s) -- extension
    2.39 +    io.stderr:write(s .. '\n')
    2.40 +    os.exit(1)
    2.41 +  end
    2.42 +
    2.43 +  function o.add_option(optdesc)
    2.44 +    option_descriptions[#option_descriptions+1] = optdesc
    2.45 +    for _,v in pairs(optdesc) do
    2.46 +      option_of[v] = optdesc
    2.47 +    end
    2.48 +  end
    2.49 +  function o.parse_args()
    2.50 +    -- expand options (e.g. "--input=file" -> "--input", "file")
    2.51 +    local arg = {unpack(arg)}
    2.52 +    for i=#arg,1,-1 do local v = arg[i]
    2.53 +      local flag, val = v:match('^(%-%-%w+)=(.*)')
    2.54 +      if flag then
    2.55 +        arg[i] = flag
    2.56 +        table.insert(arg, i+1, val)
    2.57 +      end
    2.58 +    end
    2.59 +
    2.60 +    local options = {}
    2.61 +    for _,optdesc in ipairs(option_descriptions) do
    2.62 +      options[optdesc["dest"]] = optdesc.default
    2.63 +    end
    2.64 +    local args = {}
    2.65 +    local i = 1
    2.66 +    while i <= #arg do local v = arg[i]
    2.67 +      local optdesc = option_of[v]
    2.68 +      if optdesc then
    2.69 +        local action = optdesc.action
    2.70 +        local val
    2.71 +        if action == 'store' or action == nil then
    2.72 +          i = i + 1
    2.73 +          val = arg[i]
    2.74 +          if not val then o.fail('option requires an argument ' .. v) end
    2.75 +        elseif action == 'store_true' then
    2.76 +          val = true
    2.77 +        elseif action == 'store_false' then
    2.78 +          val = false
    2.79 +        end
    2.80 +        options[optdesc.dest] = val
    2.81 +      else
    2.82 +        if v:match('^%-') then o.fail('invalid option ' .. v) end
    2.83 +        args[#args+1] = v
    2.84 +      end
    2.85 +      i = i + 1
    2.86 +    end
    2.87 +    if options.help then
    2.88 +      o.print_help()
    2.89 +      os.exit()
    2.90 +    end
    2.91 +    if options.version then
    2.92 +      io.stdout:write(t.version .. "\n")
    2.93 +      os.exit()
    2.94 +    end
    2.95 +    return options, args
    2.96 +  end
    2.97 +
    2.98 +  local function flags_str(optdesc)
    2.99 +    local sflags = {}
   2.100 +    local action = optdesc.action
   2.101 +    for _,flag in ipairs(optdesc) do
   2.102 +      local sflagend
   2.103 +      if action == nil or action == 'store' then
   2.104 +        local metavar = optdesc.metavar or optdesc.dest:upper()
   2.105 +        sflagend = #flag == 2 and ' ' .. metavar
   2.106 +                              or  '=' .. metavar
   2.107 +      else
   2.108 +        sflagend = ''
   2.109 +      end
   2.110 +      sflags[#sflags+1] = flag .. sflagend
   2.111 +    end
   2.112 +    return table.concat(sflags, ', ')
   2.113 +  end
   2.114 +
   2.115 +  function o.print_help()
   2.116 +    io.stdout:write("Usage: " .. usage:gsub('%%prog', arg[0]) .. "\n")
   2.117 +    io.stdout:write("\n")
   2.118 +    io.stdout:write("Options:\n")
   2.119 +    for _,optdesc in ipairs(option_descriptions) do
   2.120 +      io.stdout:write("  " .. flags_str(optdesc) ..
   2.121 +                      "  " .. optdesc.help .. "\n")
   2.122 +    end
   2.123 +    if commands then
   2.124 +        io.stdout:write("\nCommands:\n")
   2.125 +        for _,command in ipairs(commands) do
   2.126 +              io.stdout:write("  " .. command[1] .. 
   2.127 +                              string.rep(" ", 30-#command[1]) ..
   2.128 +                              command[2] .. "\n")
   2.129 +        end
   2.130 +    end
   2.131 +
   2.132 +  end
   2.133 +  o.add_option{"--help", action="store_true", dest="help",
   2.134 +               help="show this help message and exit"}
   2.135 +  if t.version then
   2.136 +    o.add_option{"--version", action="store_true", dest="version",
   2.137 +                 help="output version info."}
   2.138 +  end
   2.139 +  return o
   2.140 +end
   2.141 +
   2.142 +return OptionParser
   2.143 \ No newline at end of file

Impressum / About Us