94 lines
2.7 KiB
Lua
94 lines
2.7 KiB
Lua
-- Search secrets with lazysecrets (https://github.com/karleinarb/lazysecrets): "s db-password".
|
|
-- Lists only what lazysecrets has cached, so typing never waits on Azure or Databricks;
|
|
-- open lazysecrets now and then to refresh it.
|
|
|
|
-- Running lazysecrets on every keystroke would be wasteful, so the list is kept briefly.
|
|
local cache_seconds = 30
|
|
local cached, cached_at = nil, 0
|
|
|
|
local function secrets()
|
|
if cached and os.time() - cached_at < cache_seconds then
|
|
return cached
|
|
end
|
|
local list = {}
|
|
local output = toolshed.exec({ "lazysecrets", "list", "--kind", "secret", "--cached", "--format", "json" })
|
|
for line in output:gmatch("[^\n]+") do
|
|
local s = toolshed.json.decode(line)
|
|
if s.link and not s.error then
|
|
table.insert(list, {
|
|
name = table.concat(s.path, " / "),
|
|
account = s.account or "",
|
|
starred = s.starred == true,
|
|
link = s.link,
|
|
})
|
|
end
|
|
end
|
|
cached, cached_at = list, os.time()
|
|
return list
|
|
end
|
|
|
|
return toolshed.toolbox {
|
|
key = "secrets",
|
|
title = "i18n:toolbox.title",
|
|
description = "i18n:toolbox.description",
|
|
icon = "key-round",
|
|
tint = "purple",
|
|
requires = { { command = "lazysecrets" } },
|
|
|
|
tools = { {
|
|
key = "search",
|
|
title = "i18n:tool.search.title",
|
|
words = { "s" },
|
|
keywords = { "hemmeligheter", "passord" },
|
|
items = true,
|
|
|
|
search = function(rest)
|
|
local matches = {}
|
|
for _, s in ipairs(secrets()) do
|
|
local score = toolshed.match(rest, s.name)
|
|
if score > 0 then
|
|
table.insert(matches, { score = score + (s.starred and 0.05 or 0), secret = s })
|
|
end
|
|
end
|
|
table.sort(matches, function(a, b) return a.score > b.score end)
|
|
|
|
local items = {}
|
|
for i = 1, math.min(#matches, 30) do
|
|
local s = matches[i].secret
|
|
table.insert(items, {
|
|
title = (s.starred and "★ " or "") .. s.name,
|
|
subtitle = s.account,
|
|
data = { link = s.link },
|
|
})
|
|
end
|
|
return items
|
|
end,
|
|
|
|
actions = {
|
|
{
|
|
key = "open",
|
|
title = "i18n:action.search.open",
|
|
run = function(ctx)
|
|
toolshed.exec({ "lazysecrets", "launch", ctx.data.link })
|
|
end,
|
|
},
|
|
{
|
|
key = "copy-value",
|
|
title = "i18n:action.search.copy-value",
|
|
run = function(ctx)
|
|
toolshed.clipboard.copy_secret(toolshed.exec({ "lazysecrets", "get", ctx.data.link }))
|
|
return toolshed.t("value-copied")
|
|
end,
|
|
},
|
|
{
|
|
key = "copy-link",
|
|
title = "i18n:action.search.copy-link",
|
|
run = function(ctx)
|
|
toolshed.clipboard.copy(ctx.data.link)
|
|
return toolshed.t("link-copied")
|
|
end,
|
|
},
|
|
},
|
|
} },
|
|
}
|