Skip to content

Commit

Permalink
feat: Add configurable timeout for run operations #61
Browse files Browse the repository at this point in the history
  • Loading branch information
Allaman committed Oct 11, 2024
1 parent fbad9b1 commit 26f9ced
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,12 @@ You can define and run arbitrary commands on yaml files, for instance:
trivy = {
cmd = "trivy",
args = { "-q", "fs" },
timeout = 10000, -- in ms
},
deprecations29 = {
cmd = "kubent",
args = { "-t", "1.29", "-c=false", "--helm3=false", "-l=error", "-e", "-f" },
-- the default timeout is 5000 when not specified
},
deprecations30 = {
cmd = "kubent",
Expand Down
4 changes: 2 additions & 2 deletions lua/kustomize/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ M.set_default_mappings = function()

vim.keymap.set("n", "<leader>kv", function()
local validate = config.options.run.validate
run.run_checked(validate.cmd, validate.args)
run.run_checked(validate.cmd, validate.args, validate.timeout or 5000)
end, { desc = "Validate manifests" })

vim.keymap.set("n", "<leader>kd", function()
local deprecate = config.options.run.deprecations
run.run_checked(deprecate.cmd, deprecate.args)
run.run_checked(deprecate.cmd, deprecate.args, deprecate.timeout or 5000)
end, { desc = "Check for deprecations" })
end

Expand Down
11 changes: 6 additions & 5 deletions lua/kustomize/run.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ local M = {}
---comment
---@param cmd string
---@param args table
---@param timeout number
---@return table
---@return table
M.run = function(cmd, args)
M.run = function(cmd, args, timeout)
if not utils.check_exec(cmd) then
return { "cmd was not found on path" }, {}
end
Expand All @@ -28,13 +29,13 @@ M.run = function(cmd, args)

table.insert(args, file_to_validate)

utils.info("Running: " .. cmd .. " " .. vim.inspect(args))
utils.info("Running: " .. cmd .. " " .. vim.inspect(args) .. "timeout: " .. timeout)
local Job = require("plenary.job")
local job = Job:new({
command = cmd,
args = args,
})
job:sync()
job:sync(timeout)
local err, out = job:stderr_result(), job:result()

-- Removes `file_to_validate` so that a second call does not include the first file_to_validate
Expand All @@ -47,8 +48,8 @@ M.run = function(cmd, args)
return err, out
end

M.run_checked = function(cmd, args)
local err, out = M.run(cmd, args)
M.run_checked = function(cmd, args, timeout)
local err, out = M.run(cmd, args, timeout)

if next(err) ~= nil then
local err_msg = table.concat(err, "\n")
Expand Down
3 changes: 2 additions & 1 deletion plugin/kustomize.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ vim.api.nvim_create_user_command("KustomizeRun", function(opts)
local choice = opts.fargs[1]
local cmd = config.options.run[choice].cmd
local args = config.options.run[choice].args
run.run_checked(cmd, args)
local timeout = config.options.run[choice].timeout or 5000
run.run_checked(cmd, args, timeout)
end, {
desc = "Run commands",
nargs = 1,
Expand Down

0 comments on commit 26f9ced

Please sign in to comment.