Add ability do delete records
This commit is contained in:
parent
4c0b717522
commit
f4c9ccf630
2 changed files with 84 additions and 41 deletions
|
@ -23,15 +23,22 @@ If knotctl works for you in your terminal, it should work in this plugin as well
|
||||||
This plugin provides a Telescope picker for viewing and editing DNS records. You can summon it with:
|
This plugin provides a Telescope picker for viewing and editing DNS records. You can summon it with:
|
||||||
```vim
|
```vim
|
||||||
:Telescope knot update
|
:Telescope knot update
|
||||||
|
|
||||||
```
|
```
|
||||||
You can also add new records with:
|
You can also add new records with:
|
||||||
```vim
|
```vim
|
||||||
:Telescope knot add
|
:Telescope knot add
|
||||||
```
|
```
|
||||||
|
|
||||||
|
or delete records with:
|
||||||
|
```vim
|
||||||
|
:Telescope knot delete
|
||||||
|
```
|
||||||
|
|
||||||
or add a mapping:
|
or add a mapping:
|
||||||
```vim
|
```vim
|
||||||
nnoremap <leader>ka :Telescope knot add<cr>
|
nnoremap <leader>ka :Telescope knot add<cr>
|
||||||
|
nnoremap <leader>kd :Telescope knot delete<cr>
|
||||||
nnoremap <leader>ku :Telescope knot update<cr>
|
nnoremap <leader>ku :Telescope knot update<cr>
|
||||||
```
|
```
|
||||||
You can edit the record and when saving it with `:w` you will be asked to confirm. You can quit the buffer with `:q` or `q` to discard changes.
|
You can edit the record and when saving it with `:w` you will be asked to confirm. You can quit the buffer with `:q` or `q` to discard changes.
|
||||||
|
|
|
@ -14,6 +14,27 @@ local _get_name = function(record)
|
||||||
return name
|
return name
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local _get_records_and_entries = function()
|
||||||
|
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
|
||||||
|
)
|
||||||
|
return records, entries
|
||||||
|
end
|
||||||
|
|
||||||
local _filter_records = function(rec, records)
|
local _filter_records = function(rec, records)
|
||||||
local r = {}
|
local r = {}
|
||||||
local record_name = rec.value
|
local record_name = rec.value
|
||||||
|
@ -27,7 +48,16 @@ local _filter_records = function(rec, records)
|
||||||
return r
|
return r
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local _parse_record = function(rec)
|
||||||
|
local record = {}
|
||||||
|
for _, v in ipairs(rec) do
|
||||||
|
if v ~= nil and v ~= "" then
|
||||||
|
local key = v:match("%w+"):lower()
|
||||||
|
record[key] = v:gsub("%w+%:%s", "")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return record
|
||||||
|
end
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
M.add = function(_)
|
M.add = function(_)
|
||||||
|
@ -51,18 +81,12 @@ M.add = function(_)
|
||||||
buffer = buffer,
|
buffer = buffer,
|
||||||
callback = function(_)
|
callback = function(_)
|
||||||
local new = vim.api.nvim_buf_get_lines(buffer, 0, -1, false)
|
local new = vim.api.nvim_buf_get_lines(buffer, 0, -1, false)
|
||||||
local record = {}
|
local record = _parse_record(new)
|
||||||
for _, v in ipairs(new) do
|
local command = "knotctl add -z " .. record.zone ..
|
||||||
if v ~= nil and v ~= "" then
|
" -t " .. record.ttl ..
|
||||||
local key = v:match("%w+"):lower()
|
" -n " .. record.name ..
|
||||||
record[key] = v:gsub("%w+%:%s", "")
|
" -r " .. record.rtype ..
|
||||||
end
|
" -d " .. record.data
|
||||||
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)
|
local choice = vim.fn.confirm("Create record?", "&Yes\n&No", 2)
|
||||||
if choice == 1 then
|
if choice == 1 then
|
||||||
log.info(vim.fn.system(command))
|
log.info(vim.fn.system(command))
|
||||||
|
@ -70,24 +94,41 @@ M.add = function(_)
|
||||||
end
|
end
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
M.update = function(opts)
|
M.delete = function(opts)
|
||||||
local results = vim.json.decode(vim.fn.system("knotctl --json list"))
|
local records, entries = _get_records_and_entries()
|
||||||
local 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
|
||||||
|
|
||||||
for _, zone in ipairs(results) do
|
}),
|
||||||
for _, record in ipairs(zone.records) do
|
attach_mappings = function(prompt_bufnr)
|
||||||
if record then
|
actions.select_default:replace(function()
|
||||||
record.zone = zone.name
|
local entry = action_state.get_selected_entry()
|
||||||
table.insert(records, record)
|
local record = _parse_record(_filter_records(entry, records))
|
||||||
|
local choice = vim.fn.confirm("Delete record?", "&Yes\n&No", 2)
|
||||||
|
if choice == 1 then
|
||||||
|
local command = "knotctl delete -z " .. record.zone ..
|
||||||
|
" -n " .. record.name ..
|
||||||
|
" -r " .. record.rtype ..
|
||||||
|
" -d " .. record.data
|
||||||
|
log.info(vim.fn.system(command))
|
||||||
|
actions.close(prompt_bufnr)
|
||||||
end
|
end
|
||||||
end
|
end)
|
||||||
end
|
return true
|
||||||
local entries = vim.tbl_map(
|
|
||||||
function(record)
|
|
||||||
return _get_name(record)
|
|
||||||
end,
|
end,
|
||||||
records
|
}):find()
|
||||||
)
|
end
|
||||||
|
M.update = function(opts)
|
||||||
|
local records, entries = _get_records_and_entries()
|
||||||
pickers.new(opts, {
|
pickers.new(opts, {
|
||||||
dropdown = true,
|
dropdown = true,
|
||||||
prompt_title = "Records",
|
prompt_title = "Records",
|
||||||
|
@ -126,15 +167,9 @@ M.update = function(opts)
|
||||||
callback = function(_)
|
callback = function(_)
|
||||||
local new = vim.api.nvim_buf_get_lines(buffer, 0, -1, false)
|
local new = vim.api.nvim_buf_get_lines(buffer, 0, -1, false)
|
||||||
if new ~= record then
|
if new ~= record then
|
||||||
local cleanold = {}
|
local cleanold = _parse_record(record)
|
||||||
local updated = {}
|
local updated = {}
|
||||||
local did_update = false
|
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 = {}
|
local cleannew = {}
|
||||||
for _, v in ipairs(new) do
|
for _, v in ipairs(new) do
|
||||||
if v ~= nil and v ~= "" then
|
if v ~= nil and v ~= "" then
|
||||||
|
@ -147,11 +182,11 @@ M.update = function(opts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if did_update then
|
if did_update then
|
||||||
local command = "knotctl update -z" .. cleanold.zone ..
|
local command = "knotctl update -z " .. cleanold.zone ..
|
||||||
" -t" .. cleanold.ttl ..
|
" -t " .. cleanold.ttl ..
|
||||||
" -n" .. cleanold.name ..
|
" -n " .. cleanold.name ..
|
||||||
" -r" .. cleanold.rtype ..
|
" -r " .. cleanold.rtype ..
|
||||||
" -d" .. cleanold.data ..
|
" -d " .. cleanold.data ..
|
||||||
table.concat(updated, " ")
|
table.concat(updated, " ")
|
||||||
local choice = vim.fn.confirm("Update record?", "&Yes\n&No", 2)
|
local choice = vim.fn.confirm("Update record?", "&Yes\n&No", 2)
|
||||||
if choice == 1 then
|
if choice == 1 then
|
||||||
|
@ -169,6 +204,7 @@ end
|
||||||
return require('telescope').register_extension({
|
return require('telescope').register_extension({
|
||||||
exports = {
|
exports = {
|
||||||
add = M.add,
|
add = M.add,
|
||||||
|
delete = M.delete,
|
||||||
update = M.update,
|
update = M.update,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Reference in a new issue