fix(process): deal with process errors

This commit is contained in:
Folke Lemaitre 2024-06-29 13:52:50 +02:00
parent 332a7ff9b3
commit a75d950b8f
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
2 changed files with 14 additions and 8 deletions

View File

@ -42,8 +42,8 @@ function Process.new(cmd, opts)
opts = opts or {} opts = opts or {}
opts.args = opts.args or {} opts.args = opts.args or {}
if type(cmd) == "table" then if type(cmd) == "table" then
self.cmd = table.remove(cmd, 1) self.cmd = cmd[1]
vim.list_extend(opts.args, cmd) vim.list_extend(opts.args, vim.list_slice(cmd, 2))
else else
self.cmd = cmd self.cmd = cmd
end end
@ -233,10 +233,7 @@ function M.exec(cmd, opts)
opts = opts or {} opts = opts or {}
local proc = M.spawn(cmd, opts) local proc = M.spawn(cmd, opts)
proc:wait() proc:wait()
if proc.code ~= 0 then return vim.split(proc.data, "\n"), proc.code
error("Process failed with code " .. proc.code)
end
return vim.split(proc.data, "\n")
end end
return M return M

View File

@ -162,12 +162,21 @@ end
---@param opts? LazyCmdOptions|{filetype?:string} ---@param opts? LazyCmdOptions|{filetype?:string}
function M.float_cmd(cmd, opts) function M.float_cmd(cmd, opts)
opts = opts or {} opts = opts or {}
local Process = require("lazy.manage.process")
local lines, code = Process.exec(cmd, { cwd = opts.cwd })
if code ~= 0 then
M.error({
"`" .. table.concat(cmd, " ") .. "`",
"",
"## Error",
table.concat(lines, "\n"),
}, { title = "Command Failed (" .. code .. ")" })
return
end
local float = M.float(opts) local float = M.float(opts)
if opts.filetype then if opts.filetype then
vim.bo[float.buf].filetype = opts.filetype vim.bo[float.buf].filetype = opts.filetype
end end
local Process = require("lazy.manage.process")
local lines = Process.exec(cmd, { cwd = opts.cwd })
vim.api.nvim_buf_set_lines(float.buf, 0, -1, false, lines) vim.api.nvim_buf_set_lines(float.buf, 0, -1, false, lines)
vim.bo[float.buf].modifiable = false vim.bo[float.buf].modifiable = false
return float return float