From 80bbc91cfb1d3ba31495330f9b929c576d2af8c4 Mon Sep 17 00:00:00 2001 From: Tobias Ulmer Date: Thu, 11 Jul 2013 12:50:29 +0200 Subject: [PATCH] Modify callcmd_log() to return an error object Signed-off-by: Tobias Ulmer --- generic/e2lib.lua | 44 +++++++++++++++++++++++-------------- global/e2-fetch-project.lua | 3 ++- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/generic/e2lib.lua b/generic/e2lib.lua index 74deb74..a2a0848 100644 --- a/generic/e2lib.lua +++ b/generic/e2lib.lua @@ -1191,22 +1191,35 @@ function e2lib.callcmd_capture(cmd, capture) return rc end ---- call a command, log its output to a loglevel, catch the last line of --- output and return it in addition to the commands return code +--- Call a command, log its output and catch the last lines for error reporting. -- @param cmd string: the command --- @param loglevel number: loglevel (optional, defaults to 3) --- @return number: the return code --- @return string: the program output, or nil -function e2lib.callcmd_log(cmd, loglevel) - local e = "" - if not loglevel then - loglevel = 3 - end - local function logto(output) - e2lib.log(loglevel, output) - e = e .. output +-- @return Return code of the command (number). +-- @return Error object containing command line and last lines of output. +function e2lib.callcmd_log(cmd) + local e = err.new("command %s failed:", cmd) + local fifo = {} + + local function logto(msg) + e2lib.log(3, msg) + + if msg ~= "" then + if #fifo > 4 then -- keep the last n lines. + table.remove(fifo, 1) + end + table.insert(fifo, msg) + end end + local rc = e2lib.callcmd_capture(cmd, logto) + + if #fifo == 0 then + table.insert(fifo, "command failed silently, no output captured") + end + + for _,v in ipairs(fifo) do + e:append("%s", v) + end + return rc, e end @@ -1555,7 +1568,7 @@ function e2lib.call_tool(tool, args) if rc ~= 0 then return false, e end - return true, e + return true end --- call a tool with argv @@ -1616,7 +1629,7 @@ function e2lib.call_tool_argv_capture(tool, argv, capturefn) if rc ~= 0 then return false, e end - return true, e + return true end --- call git @@ -1643,7 +1656,6 @@ function e2lib.git(gitdir, subtool, args) e2lib.shquote(gitdir), e2lib.shquote(git), e2lib.shquote(subtool), args) rc, re = e2lib.callcmd_log(call) if rc ~= 0 then - e:append(call) return false, e:cat(re) end return true, e diff --git a/global/e2-fetch-project.lua b/global/e2-fetch-project.lua index ef3dcaa..0744cc2 100644 --- a/global/e2-fetch-project.lua +++ b/global/e2-fetch-project.lua @@ -196,7 +196,8 @@ local function e2_fetch_project(arg) e2lib.shquote(buildconfig.LUA), e2lib.shquote(buildconfig.TOOLDIR)) rc, re = e2lib.callcmd_log(e2_install_e2) if rc ~= 0 then - return false, err.new("installing local e2 failed") + local e = err.new("installing local e2 failed") + return false, e:cat(re) end return true -- 2.39.5