parent
94a0a841ae
commit
4c0b717522
@ -0,0 +1,174 @@
|
|||||||
|
local action_state = require('telescope.actions.state')
|
||||||
|
local actions = require('telescope.actions')
|
||||||
|
local conf = require("telescope.config").values
|
||||||
|
local finders = require("telescope.finders")
|
||||||
|
local pickers = require("telescope.pickers")
|
||||||
|
local previewers = require("telescope.previewers")
|
||||||
|
local log = require('plenary.log').new {
|
||||||
|
plugin = 'knot',
|
||||||
|
level = 'info',
|
||||||
|
}
|
||||||
|
|
||||||
|
local _get_name = function(record)
|
||||||
|
local name = record.name .. " (" .. record.rtype .. "): " .. record.data
|
||||||
|
return name
|
||||||
|
end
|
||||||
|
|
||||||
|
local _filter_records = function(rec, records)
|
||||||
|
local r = {}
|
||||||
|
local record_name = rec.value
|
||||||
|
for _, record in ipairs(records) do
|
||||||
|
local name = _get_name(record)
|
||||||
|
if name == record_name then
|
||||||
|
r = { "ZONE: " .. record.zone, "RTYPE: " .. record.rtype, "NAME: " .. record.name, "DATA: " .. record.data, "TTL: " ..
|
||||||
|
record.ttl }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return r
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
local M = {}
|
||||||
|
M.add = function(_)
|
||||||
|
local tempfile = vim.fn.tempname()
|
||||||
|
local buffer = vim.api.nvim_create_buf(false, false)
|
||||||
|
vim.api.nvim_buf_set_name(buffer, tempfile)
|
||||||
|
local r = { "ZONE: example.net.", "RTYPE: A", "NAME: example.net.", "DATA: 127.0.0.1", "TTL: 360" }
|
||||||
|
vim.api.nvim_buf_set_lines(buffer, 0, 0, false, r)
|
||||||
|
local width = vim.api.nvim_win_get_width(0)
|
||||||
|
local height = vim.api.nvim_win_get_height(0) - 10
|
||||||
|
vim.api.nvim_open_win(buffer, true,
|
||||||
|
{ relative = 'win', bufpos = { 0, 0 }, width = width, height = height, border = 'rounded' })
|
||||||
|
vim.api.nvim_create_autocmd({ "BufWipeout" }, {
|
||||||
|
buffer = buffer,
|
||||||
|
callback = function(_)
|
||||||
|
vim.api.nvim_buf_delete(buffer, { force = true })
|
||||||
|
vim.fn.delete(tempfile)
|
||||||
|
end
|
||||||
|
})
|
||||||
|
vim.api.nvim_create_autocmd({ "BufWrite" }, {
|
||||||
|
buffer = buffer,
|
||||||
|
callback = function(_)
|
||||||
|
local new = vim.api.nvim_buf_get_lines(buffer, 0, -1, false)
|
||||||
|
local record = {}
|
||||||
|
for _, v in ipairs(new) do
|
||||||
|
if v ~= nil and v ~= "" then
|
||||||
|
local key = v:match("%w+"):lower()
|
||||||
|
record[key] = v:gsub("%w+%:%s", "")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
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
|
||||||
|
end
|
||||||
|
})
|
||||||
|
end
|
||||||
|
M.update = function(opts)
|
||||||
|
local results = vim.json.decode(vim.fn.system("knotctl --json list"))
|
||||||
|
local records = {}
|
||||||
|
|
||||||
|
for _, zone in ipairs(results) do
|
||||||
|
for _, record in ipairs(zone.records) do
|
||||||
|
if record then
|
||||||
|
record.zone = zone.name
|
||||||
|
table.insert(records, record)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local entries = vim.tbl_map(
|
||||||
|
function(record)
|
||||||
|
return _get_name(record)
|
||||||
|
end,
|
||||||
|
records
|
||||||
|
)
|
||||||
|
pickers.new(opts, {
|
||||||
|
dropdown = true,
|
||||||
|
prompt_title = "Records",
|
||||||
|
sorter = conf.generic_sorter(opts),
|
||||||
|
finder = finders.new_table(entries),
|
||||||
|
previewer = previewers.new_buffer_previewer({
|
||||||
|
title = "Record",
|
||||||
|
define_preview = function(self, entry)
|
||||||
|
local record = _filter_records(entry, records)
|
||||||
|
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, 0, false, record)
|
||||||
|
end
|
||||||
|
|
||||||
|
}),
|
||||||
|
attach_mappings = function(prompt_bufnr)
|
||||||
|
actions.select_default:replace(function()
|
||||||
|
local entry = action_state.get_selected_entry()
|
||||||
|
local record = _filter_records(entry, records)
|
||||||
|
actions.close(prompt_bufnr)
|
||||||
|
local tempfile = vim.fn.tempname()
|
||||||
|
local buffer = vim.api.nvim_create_buf(false, false)
|
||||||
|
vim.api.nvim_buf_set_name(buffer, tempfile)
|
||||||
|
vim.api.nvim_buf_set_lines(buffer, 0, 0, false, record)
|
||||||
|
local width = vim.api.nvim_win_get_width(0)
|
||||||
|
local height = vim.api.nvim_win_get_height(0) - 10
|
||||||
|
vim.api.nvim_open_win(buffer, true,
|
||||||
|
{ relative = 'win', bufpos = { 0, 0 }, width = width, height = height, border = 'rounded' })
|
||||||
|
vim.api.nvim_create_autocmd({ "BufWipeout" }, {
|
||||||
|
buffer = buffer,
|
||||||
|
callback = function(_)
|
||||||
|
vim.api.nvim_buf_delete(buffer, { force = true })
|
||||||
|
vim.fn.delete(tempfile)
|
||||||
|
end
|
||||||
|
})
|
||||||
|
vim.api.nvim_create_autocmd({ "BufWrite" }, {
|
||||||
|
buffer = buffer,
|
||||||
|
callback = function(_)
|
||||||
|
local new = vim.api.nvim_buf_get_lines(buffer, 0, -1, false)
|
||||||
|
if new ~= record then
|
||||||
|
local cleanold = {}
|
||||||
|
local updated = {}
|
||||||
|
local did_update = false
|
||||||
|
for _, v in ipairs(record) do
|
||||||
|
if v ~= nil and v ~= "" then
|
||||||
|
local key = v:match("%w+"):lower()
|
||||||
|
cleanold[key] = v:gsub("%w+%:%s", "")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local cleannew = {}
|
||||||
|
for _, v in ipairs(new) do
|
||||||
|
if v ~= nil and v ~= "" then
|
||||||
|
local key = v:match("%w+"):lower()
|
||||||
|
cleannew[key] = v:gsub("%w+%:%s", "")
|
||||||
|
if cleanold[key] ~= cleannew[key] and (key ~= "url" or key ~= "zone") then
|
||||||
|
table.insert(updated, " -a " .. key .. "=" .. cleannew[key])
|
||||||
|
did_update = true
|
||||||
|
end
|
||||||
|
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))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
})
|
||||||
|
end)
|
||||||
|
return true
|
||||||
|
end,
|
||||||
|
}):find()
|
||||||
|
end
|
||||||
|
return require('telescope').register_extension({
|
||||||
|
exports = {
|
||||||
|
add = M.add,
|
||||||
|
update = M.update,
|
||||||
|
},
|
||||||
|
})
|
@ -1,129 +0,0 @@
|
|||||||
local action_state = require('telescope.actions.state')
|
|
||||||
local actions = require('telescope.actions')
|
|
||||||
local conf = require("telescope.config").values
|
|
||||||
local finders = require("telescope.finders")
|
|
||||||
local pickers = require("telescope.pickers")
|
|
||||||
local previewers = require("telescope.previewers")
|
|
||||||
local log = require('plenary.log').new {
|
|
||||||
plugin = 'telescope_knot',
|
|
||||||
level = 'info',
|
|
||||||
}
|
|
||||||
|
|
||||||
local _get_name = function(record)
|
|
||||||
local name = record.name .. " (" .. record.rtype .. "): " .. record.data
|
|
||||||
return name
|
|
||||||
end
|
|
||||||
|
|
||||||
local _filter_records = function(rec, records)
|
|
||||||
local r = {}
|
|
||||||
local record_name = rec.value
|
|
||||||
for _, record in ipairs(records) do
|
|
||||||
local name = _get_name(record)
|
|
||||||
if name == record_name then
|
|
||||||
r = { "ZONE: " .. record.zone, "RTYPE: " .. record.rtype, "NAME: " .. record.name, "DATA: " .. record.data, "TTL: " ..
|
|
||||||
record.ttl, "URL: " .. record.url }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return r
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local telescope_knot = {}
|
|
||||||
telescope_knot.zones = function(opts)
|
|
||||||
local results = vim.json.decode(vim.fn.system("knotctl --json list"))
|
|
||||||
local records = {}
|
|
||||||
|
|
||||||
for _, zone in ipairs(results) do
|
|
||||||
for _, record in ipairs(zone.records) do
|
|
||||||
if record then
|
|
||||||
record.zone = zone.name
|
|
||||||
table.insert(records, record)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
local entries = vim.tbl_map(
|
|
||||||
function(record)
|
|
||||||
return _get_name(record)
|
|
||||||
end,
|
|
||||||
records
|
|
||||||
)
|
|
||||||
pickers.new(opts, {
|
|
||||||
dropdown = true,
|
|
||||||
prompt_title = "Records",
|
|
||||||
sorter = conf.generic_sorter(opts),
|
|
||||||
finder = finders.new_table(entries),
|
|
||||||
previewer = previewers.new_buffer_previewer({
|
|
||||||
title = "Record",
|
|
||||||
define_preview = function(self, entry)
|
|
||||||
local record = _filter_records(entry, records)
|
|
||||||
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, 0, false, record)
|
|
||||||
end
|
|
||||||
|
|
||||||
}),
|
|
||||||
attach_mappings = function(prompt_bufnr)
|
|
||||||
actions.select_default:replace(function()
|
|
||||||
local entry = action_state.get_selected_entry()
|
|
||||||
local record = _filter_records(entry, records)
|
|
||||||
actions.close(prompt_bufnr)
|
|
||||||
local buffer = vim.api.nvim_create_buf(false, true)
|
|
||||||
vim.api.nvim_buf_set_lines(buffer, 0, 0, false, record)
|
|
||||||
local width = vim.api.nvim_win_get_width(0)
|
|
||||||
local height = vim.api.nvim_win_get_height(0) - 10
|
|
||||||
vim.api.nvim_open_win(buffer, true,
|
|
||||||
{ relative = 'win', bufpos = { 0, 0 }, width = width, height = height, border = 'rounded' })
|
|
||||||
vim.api.nvim_buf_set_keymap(buffer, "n", "q", "<CMD>bd<CR>", {})
|
|
||||||
vim.api.nvim_buf_set_keymap(buffer, "c", "w",
|
|
||||||
"lua vim.api.nvim_exec_autocmds('User', { pattern = 'KnotWrite' })<CR>", {})
|
|
||||||
vim.api.nvim_create_autocmd({ "User" }, {
|
|
||||||
buffer = buffer,
|
|
||||||
callback = function(event)
|
|
||||||
if event.match == "KnotWrite" then
|
|
||||||
local new = vim.api.nvim_buf_get_lines(buffer, 0, -1, false)
|
|
||||||
if new ~= record then
|
|
||||||
local cleanold = {}
|
|
||||||
local updated = {}
|
|
||||||
local did_update = false
|
|
||||||
for _, v in ipairs(record) do
|
|
||||||
if v ~= nil and v ~= "" then
|
|
||||||
local key = v:match("%w+"):lower()
|
|
||||||
cleanold[key] = v:gsub("%w+%:%s", "")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
local cleannew = {}
|
|
||||||
for _, v in ipairs(new) do
|
|
||||||
if v ~= nil and v ~= "" then
|
|
||||||
local key = v:match("%w+"):lower()
|
|
||||||
cleannew[key] = v:gsub("%w+%:%s", "")
|
|
||||||
if cleanold[key] ~= cleannew[key] and (key ~= "url" or key ~= "zone") then
|
|
||||||
table.insert(updated, " -a " .. key .. "=" .. cleannew[key])
|
|
||||||
did_update = true
|
|
||||||
end
|
|
||||||
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 and close buffer?", "&Yes\n&No", 2)
|
|
||||||
if choice == 1 then
|
|
||||||
log.info(vim.fn.system(command))
|
|
||||||
vim.api.nvim_buf_delete(buffer, {})
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
})
|
|
||||||
end)
|
|
||||||
return true
|
|
||||||
end,
|
|
||||||
}):find()
|
|
||||||
end
|
|
||||||
return require('telescope').register_extension({
|
|
||||||
exports = {
|
|
||||||
telescope_knot = telescope_knot.zones,
|
|
||||||
},
|
|
||||||
})
|
|
Loading…
Reference in new issue