]> git.e2factory.org Git - e2factory.git/commitdiff
Add e2lib.call_tool_argv_capture()
authorTobias Ulmer <tu@emlix.com>
Mon, 3 Mar 2014 13:30:43 +0000 (14:30 +0100)
committerTobias Ulmer <tu@emlix.com>
Tue, 25 Mar 2014 17:07:15 +0000 (18:07 +0100)
Signed-off-by: Tobias Ulmer <tu@emlix.com>
generic/e2lib.lua

index 795e689686567779737f4d617365bd50803b35a3..4e555cbe8dabc3f6c9ffed08e51dcf3636cb1116 100644 (file)
@@ -1445,6 +1445,37 @@ function e2lib.call_tool_argv(tool, argv)
     return true, e
 end
 
+--- call a tool with argv and capture output
+-- @param tool string: tool name as registered in the tools library
+-- @param argv table: a vector of (string) arguments
+-- @param capturefn function called for ever chunk of output
+-- @return bool
+-- @return string: the last line ouf captured output
+function e2lib.call_tool_argv_capture(tool, argv, capturefn)
+    local cmd = tools.get_tool(tool)
+    if not cmd then
+        e2lib.bomb("trying to call invalid tool: " .. tostring(tool))
+    end
+    local flags = tools.get_tool_flags(tool)
+    if not flags then
+        e2lib.bomb("invalid tool flags for tool: " .. tostring(tool))
+    end
+
+    -- TODO: flags should be quoted as well, requires config changes
+    local call = string.format("%s %s", e2lib.shquote(cmd), flags)
+
+    for _,arg in ipairs(argv) do
+        assert(type(arg) == "string")
+        call = call .. " " .. e2lib.shquote(arg)
+    end
+
+    local rc, e = e2lib.callcmd_capture(call, capturefn)
+    if rc ~= 0 then
+        return false, e
+    end
+    return true, e
+end
+
 --- call git
 -- @param gitdir string: GIT_DIR (optional, defaults to ".git")
 -- @param subtool string: git tool name