diff options
author | Daniel Silverstone <dsilvers@digital-scurf.org> | 2012-05-01 17:19:12 +0100 |
---|---|---|
committer | Daniel Silverstone <dsilvers@digital-scurf.org> | 2012-05-01 17:19:12 +0100 |
commit | fb1c1ed5aafdf96c73a168a62fabd0930ccf0be2 (patch) | |
tree | 0e3c1cf0874339110f40611ee0958d445dc1d834 /lib/gitano/command.lua | |
parent | 94f886e0ef07cee5f0afd269c2a40c415b4a59e0 (diff) | |
download | gitano-fb1c1ed5aafdf96c73a168a62fabd0930ccf0be2.tar.gz |
COMMAND: Support help
Diffstat (limited to 'lib/gitano/command.lua')
-rw-r--r-- | lib/gitano/command.lua | 105 |
1 files changed, 93 insertions, 12 deletions
diff --git a/lib/gitano/command.lua b/lib/gitano/command.lua index 9aa114d..872aa26 100644 --- a/lib/gitano/command.lua +++ b/lib/gitano/command.lua @@ -10,7 +10,9 @@ local sp = require "luxio.subprocess" local cmds = {} -local function register_cmd(cmdname, validate_fn, prep_fn, run_fn, takes_repo) +local function register_cmd(cmdname, short, helptext, + validate_fn, prep_fn, run_fn, + takes_repo, hidden) log.ddebug("Register command", cmdname) if takes_repo then log.ddebug(" => Takes a repo") @@ -20,11 +22,17 @@ local function register_cmd(cmdname, validate_fn, prep_fn, run_fn, takes_repo) return false, "Attempt to double-register" .. cmdname end cmds[cmdname] = { + name = cmdname, validate = validate_fn, prep = prep_fn, run = run_fn, - takes_repo = takes_repo + takes_repo = takes_repo, + hidden = hidden, + short = short, + helptext = helptext } + cmds[#cmds+1] = cmdname + table.sort(cmds) return true end @@ -41,6 +49,67 @@ local function get_cmd(cmdname) } end +local function builtin_help_validate(config, repo, cmdline) + if #cmdline > 2 then + log.error("usage: help [cmd]") + return false + end + if #cmdline == 2 then + if not cmds[cmdline[2]] then + log.error("Unknown command:", help) + return false + end + end + return true +end + +local function builtin_help_prep(config, repo, cmdline) + return { } +end + +local function builtin_help_run(config, repo, cmdline, env) + if #cmdline == 1 then + -- List all commands + local maxcmdn = 0 + for i = 1, #cmds do + local cmd = cmds[cmds[i]] + if not cmd.hidden then + if #cmd.name > maxcmdn then + maxcmdn = #cmd.name + end + end + end + for i = 1, #cmds do + local cmd = cmds[cmds[i]] + if not cmd.hidden then + local gap = (" "):rep(maxcmdn - #cmd.name) + local desc = (cmd.short or "No description") + if cmd.takes_repo then + desc = desc .. " (Takes a repo)" + end + log.state(gap .. cmd.name, "--", desc) + end + end + else + local cmd = cmds[cmdline[2]] + local desc = (cmd.short or "No description") + if cmd.takes_repo then + desc = desc .. " (Takes a repo)" + end + log.state(cmd.name, "--", desc) + if cmd.helptext then + log.state("") + for line in (cmd.helptext):gmatch("([^\n]*)\n") do + log.state("=>", line) + end + end + end + return "exit", 0 +end + +assert(register_cmd("help", nil, nil, builtin_help_validate, builtin_help_prep, + builtin_help_run, false, false)) + local function builtin_upload_pack_validate(config, repo, cmdline) -- git-upload-pack repo if #cmdline > 2 then @@ -64,11 +133,11 @@ local function builtin_upload_pack_run(config, repo, cmdline, env) return proc:wait() end -assert(register_cmd("git-upload-pack", +assert(register_cmd("git-upload-pack", nil, nil, builtin_upload_pack_validate, builtin_upload_pack_prep, builtin_upload_pack_run, - true)) + true, true)) local function builtin_receive_pack_validate(config, repo, cmdline) -- git-receive-pack repo @@ -93,11 +162,11 @@ local function builtin_receive_pack_run(config, repo, cmdline, env) return proc:wait() end -assert(register_cmd("git-receive-pack", +assert(register_cmd("git-receive-pack", nil, nil, builtin_receive_pack_validate, builtin_receive_pack_prep, builtin_receive_pack_run, - true)) + true, true)) local function builtin_create_validate(config, repo, cmdline) -- create reponame @@ -148,8 +217,8 @@ local function builtin_create_run(config, repo, cmdline, env) return "exit", 0 end -assert(register_cmd("create", builtin_create_validate, - builtin_create_prep, builtin_create_run, true)) +assert(register_cmd("create", nil, nil, builtin_create_validate, + builtin_create_prep, builtin_create_run, true, false)) local function builtin_set_owner_validate(config, repo, cmdline) -- set-owner reponame ownername @@ -190,8 +259,18 @@ local function builtin_set_owner_run(config, repo, cmdline, env) return "exit", 0 end -assert(register_cmd("set-owner", builtin_set_owner_validate, - builtin_set_owner_prep, builtin_set_owner_run, true)) +assert(register_cmd("set-owner", nil, nil, builtin_set_owner_validate, + builtin_set_owner_prep, builtin_set_owner_run, + true, false)) + +local builtin_whoami_short = "Find out how Gitano identifies you" + +local builtin_whoami_helptext = [[ +usage: whoami + +Tells you who you are, what your email address is set to, what keys you have +registered etc. +]] local function builtin_whoami_validate(config, repo, cmdline) -- whoami @@ -248,8 +327,10 @@ local function builtin_whoami_run(config, repo, cmdline, env) return "exit", 0 end -assert(register_cmd("whoami", builtin_whoami_validate, - builtin_whoami_prep, builtin_whoami_run, false)) +assert(register_cmd("whoami", builtin_whoami_short, builtin_whoami_helptext, + builtin_whoami_validate, + builtin_whoami_prep, builtin_whoami_run, false, false)) + return { register = register_cmd, |