summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2012-06-29 22:30:39 +0100
committerDaniel Silverstone <dsilvers@digital-scurf.org>2012-06-29 22:30:39 +0100
commitaa93ae86a60b747e791e7abd6eac972d88a79e06 (patch)
tree547c7d5d9e71dbc55828ad001ec48e1bd0954a34
parented188b6f2e0603f8c7fb1c80e13f4112d580111d (diff)
downloadgitano-aa93ae86a60b747e791e7abd6eac972d88a79e06.tar.gz
USERCOMMAND: Initial work on 'as' command
-rw-r--r--lib/gitano/usercommand.lua81
1 files changed, 81 insertions, 0 deletions
diff --git a/lib/gitano/usercommand.lua b/lib/gitano/usercommand.lua
index cb4885b..35bbeaa 100644
--- a/lib/gitano/usercommand.lua
+++ b/lib/gitano/usercommand.lua
@@ -11,6 +11,84 @@ local config = require 'gitano.config'
local sio = require 'luxio.simple'
+local function cmdmod()
+ return require 'gitano.command'
+end
+
+local builtin_as_short = "Become someone else"
+local builtin_as_helptext = [[
+usage: as <user> <cmdline>...
+
+Runs the given command line as the given user. The only limitation
+is that you are not permitted to run 'as' as someone else.
+]]
+
+local function builtin_as_validate(config, _, cmdline)
+ -- as
+ if #cmdline < 3 then
+ log.error("usage: as <user> <cmdline>...")
+ return false
+ end
+ if cmdline[3] == "as" then
+ log.error("Cannot use 'as' to run 'as'")
+ return false
+ end
+ -- Strip the cmdline
+ local cmdline_copy = util.deep_copy(cmdline)
+ table.remove(cmdline_copy,1)
+ table.remove(cmdline_copy,1)
+ cmdline.copy = cmdline_copy
+ -- Attempt to locate the command
+ local cmd = cmdmod().get(cmdline[3])
+ if not cmd then
+ log.error("Unknown command <" .. cmdline[3] .. ">")
+ return false
+ end
+ cmdline.cmd = cmd
+ -- If the returned command needs a repo, find it (and save it for later)
+ local repo
+ if cmd.takes_repo and #cmdline > 3 then
+ -- Acquire the repository object for the target repo
+ local msg
+ repo, msg = gitano.repository.find(config, cmdline[4])
+ if not repo then
+ gitano.log.critical("Unable to locate repository.")
+ gitano.log.critical(" * " .. (tostring(msg)))
+ gitano.log.fatal("Cannot continue")
+ end
+
+ if repo.is_nascent then
+ gitano.log.info("Repository " .. repo.name .. " is nascent")
+ end
+ cmdline.repo = repo
+ end
+
+ -- Finally, validate us
+ return cmd.validate(config, cmdline.repo, cmdline.copy)
+end
+
+local function builtin_as_prep(conf, _, cmdline, context)
+ -- The context contains the user we are right now.
+ -- We need to acquire information about that, so ask the config
+ local as = { user = context.user }
+ config.populate_context(conf, as)
+ for k, v in pairs(as) do
+ context["as_" .. k] = v
+ end
+ context.user = cmdline[2]
+ -- Okay, we're now ready to chain through to the called command
+ return cmdline.cmd.prep(conf, cmdline.repo, cmdline.copy, context)
+end
+
+local function builtin_as_run(conf, _, cmdline, env)
+ -- Override some of the environment
+ env.GITANO_USER = cmdline[2]
+ env.GITANO_KEYTAG = "<*>"
+ env.GITANO_PROJECT = (cmdline.repo or {}).name
+ -- And then simply chain through
+ return cmdline.cmd.run(conf, cmdline.repo, cmdline.copy, env)
+end
+
local builtin_whoami_short = "Find out how Gitano identifies you"
local builtin_whoami_helptext = [[
@@ -186,6 +264,9 @@ local function builtin_sshkey_run(conf, _, cmdline, env)
end
local function register_commands(reg)
+ assert(reg("as", builtin_as_short, builtin_as_helptext,
+ builtin_as_validate, builtin_as_prep, builtin_as_run,
+ false, false))
assert(reg("whoami", builtin_whoami_short, builtin_whoami_helptext,
builtin_whoami_validate,
builtin_whoami_prep, builtin_whoami_run, false, false))