summaryrefslogtreecommitdiff
path: root/xmake/core/sandbox/modules/os.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2017-03-28 21:58:52 +0800
committerruki <[email protected]>2017-03-28 21:58:52 +0800
commit8e0c711fb2ea0fb3a54b9f2b41d1907fc4be8b02 (patch)
treec9f7fdc3dc1ea9155e8fd01e57b66c71f0abaf72 /xmake/core/sandbox/modules/os.lua
parent6672a212d0ea74c3d6c6425b58b4fbb1dea7999c (diff)
add sudo interface
Diffstat (limited to 'xmake/core/sandbox/modules/os.lua')
-rw-r--r--xmake/core/sandbox/modules/os.lua73
1 files changed, 70 insertions, 3 deletions
diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua
index 15f75ae08..94b78c6b8 100644
--- a/xmake/core/sandbox/modules/os.lua
+++ b/xmake/core/sandbox/modules/os.lua
@@ -26,6 +26,7 @@
local io = require("base/io")
local os = require("base/os")
local utils = require("base/utils")
+local option = require("base/option")
local sandbox = require("sandbox/sandbox")
local vformat = require("sandbox/modules/vformat")
@@ -41,6 +42,18 @@ sandbox_os.mtime = os.mtime
sandbox_os.mclock = os.mclock
sandbox_os.emptydir = os.emptydir
+-- get sudo program name for running program with administrator permission
+function sandbox_os._sudoname()
+
+ -- on windows?
+ if xmake._HOST == "windows" then
+ -- TODO
+ -- add sudo.bat
+ else
+ return "sudo"
+ end
+end
+
-- copy file or directory
function sandbox_os.cp(...)
@@ -445,9 +458,63 @@ function sandbox_os.uuid(name)
return uuid
end
--- get sudo program name for running program with administrator permission
-function sandbox_os.sudo()
- return os.sudo()
+-- get the feature of os
+function sandbox_os.feature(name)
+
+ -- has 'sudo' feature?
+ if name == "sudo" then
+ return sandbox_os._sudoname() ~= nil
+ end
+end
+
+-- sudo run shell with administrator permission
+--
+-- .e.g
+-- os.sudo(os.run, "echo", "hello xmake!")
+--
+function sandbox_os.sudo(runner, cmd, ...)
+
+ -- check
+ assert(sandbox_os.feature("sudo"), "no sudo!")
+
+ -- run it with administrator permission
+ runner(sandbox_os._sudoname() .. " " .. cmd, ...)
+end
+
+-- sudo run shell with administrator permission and arguments list
+--
+-- .e.g
+-- os.sudov(os.runv, {"echo", "hello xmake!"})
+--
+function sandbox_os.sudov(runner, shellname, argv)
+
+ -- check
+ assert(sandbox_os.feature("sudo"), "no sudo!")
+
+ -- run it with administrator permission
+ runner(sandbox_os._sudoname(), table.join(shellname, argv))
+end
+
+-- sudo run lua script with administrator permission and arguments list
+--
+-- .e.g
+-- os.sudol(os.runv, "xxx.lua", {"arg1", "arg2"})
+--
+function sandbox_os.sudol(runner, luafile, luaargv)
+
+ -- init argv
+ local argv = {"lua"}
+ for _, name in ipairs({"file", "project", "backtrace", "verbose", "quiet"}) do
+ local value = option.get(name)
+ if type(value) == "string" then
+ table.insert(argv, "--" .. name .. "=" .. value)
+ elseif value then
+ table.insert(argv, "--" .. name)
+ end
+ end
+
+ -- run it with administrator permission
+ sandbox_os.sudov(runner, "xmake", table.join(argv, luafile, luaargv))
end
-- return module