From 5f821db8009e80080b30a7e3929b19584433a509 Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Fri, 27 Dec 2024 23:55:20 +0100 Subject: [PATCH] Add some error handling --- lua/telescope/_extensions/knot.lua | 53 ++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/lua/telescope/_extensions/knot.lua b/lua/telescope/_extensions/knot.lua index 5e1356b..7223865 100644 --- a/lua/telescope/_extensions/knot.lua +++ b/lua/telescope/_extensions/knot.lua @@ -53,12 +53,21 @@ local _parse_record = function(rec) for _, v in ipairs(rec) do if v ~= nil and v ~= "" then local key = v:match("%w+"):lower() - record[key] = v:gsub("%w+%:%s", "") + if vim.tbl_contains({ "zone", "rtype", "name", "data", "ttl" }, key) then + record[key] = v:gsub("%w+%:%s", "") + end end end return record end +local _validate_record = function(record) + if record.zone == nil or record.rtype == nil or record.name == nil or record.data == nil then + return false + end + return true +end + local M = {} M.add = function(_) local tempfile = vim.fn.tempname() @@ -82,14 +91,18 @@ M.add = function(_) callback = function(_) local new = vim.api.nvim_buf_get_lines(buffer, 0, -1, false) local record = _parse_record(new) - local command = "knotctl add -z " .. record.zone .. - " -t " .. record.ttl .. - " -n " .. record.name .. - " -r " .. record.rtype .. - " -d " .. record.data - local choice = vim.fn.confirm("Create record?", "&Yes\n&No", 2) - if choice == 1 then - log.info(vim.fn.system(command)) + if _validate_record(record) then + local command = "knotctl add -z " .. record.zone .. + " -t " .. record.ttl .. + " -n " .. record.name .. + " -r " .. record.rtype .. + " -d " .. record.data + local choice = vim.fn.confirm("Create record?", "&Yes\n&No", 2) + if choice == 1 then + log.info(vim.fn.system(command)) + end + else + log.error("Invalid record") end end }) @@ -182,15 +195,19 @@ M.update = function(opts) end end if did_update then - local command = "knotctl update -z " .. cleanold.zone .. - " -t " .. cleanold.ttl .. - " -n " .. cleanold.name .. - " -r " .. cleanold.rtype .. - " -d " .. cleanold.data .. - table.concat(updated, " ") - local choice = vim.fn.confirm("Update record?", "&Yes\n&No", 2) - if choice == 1 then - log.info(vim.fn.system(command)) + if _validate_record(cleannew) and _validate_record(cleanold) then + local command = "knotctl update -z " .. cleanold.zone .. + " -t " .. cleanold.ttl .. + " -n " .. cleanold.name .. + " -r " .. cleanold.rtype .. + " -d " .. cleanold.data .. + table.concat(updated, " ") + local choice = vim.fn.confirm("Update record?", "&Yes\n&No", 2) + if choice == 1 then + log.info(vim.fn.system(command)) + end + else + log.error("Invalid record") end end end