Allow text entry to be automated (env: prpw_*)

This commit is contained in:
Fierelier 2024-10-09 04:19:14 +02:00
parent e0af56ae0b
commit 8aafd2ab88

View File

@ -12,17 +12,23 @@ local function eprint(text)
io.stderr:write(text .. "\n")
end
local function input(text)
local function input(id,text)
if text == nil then text = "" end
io.stderr:write(text)
return io.read()
io.stderr:write("[" ..id.. "] " .. text)
local input = os.getenv("prpw_" .. id)
if input ~= nil then
eprint(input)
return input
else
return io.read()
end
end
local function yn(question)
local function yn(id,question)
io.stderr:write(question .. " - y/n: ")
local inp = false
while true do
inp = string.lower(input())
inp = string.lower(input(id))
if (
inp == "y" or
inp == "n"
@ -31,13 +37,13 @@ local function yn(question)
return (inp == "y")
end
local function menu(choices)
local function menu(id,choices)
for i,v in ipairs(choices) do
eprint(tostring(i).. ": " ..tostring(v))
end
local choice = false
while true do
choice = tonumber(input("Choice: "))
choice = tonumber(input("menu_" ..id,"Choice: "))
if not (
choice == nil or
choice < 1 or
@ -50,12 +56,13 @@ local function menu(choices)
return choice
end
local function emenu(title,choices,functions,lastSelection)
local function emenu(id,title,choices,functions,lastSelection)
if lastSelection == nil then lastSelection = "<- BACK" end
table.insert(choices,lastSelection)
while true do
eprint(title)
local i = menu(choices)
i = menu(id,choices)
if i == #choices then
return
end
@ -65,41 +72,38 @@ local function emenu(title,choices,functions,lastSelection)
end
end
local function printPassword(entry)
print(env.run({"field_get",entry.attrs.id,"password"}))
end
local function fieldPrint(entry,field)
print(env.run({"field_get",entry.attrs.id,field.attrs.name}))
io.write(env.run({"field_get",entry.attrs.id,field.attrs.name}))
eprint("")
end
local function fieldPrintType(entry,field)
print(env.run({"field_get_type",entry.attrs.id,field.attrs.name}))
eprint(env.run({"field_get_type",entry.attrs.id,field.attrs.name}))
end
local function fieldSetText(entry,field)
local text = input("Text: ")
local text = input("field_set_text","Text: ")
makeEntryBackup(entry.attrs.id)
env.run({"field_set",entry.attrs.id,field.attrs.name,text})
env.run({"save"})
end
local function fieldSetType(entry,field)
local tp = input("Type: ")
local tp = input("field_set_type","Type: ")
makeEntryBackup(entry.attrs.id)
env.run({"field_set_type",entry.attrs.id,field.attrs.name,tp})
env.run({"save"})
end
local function fieldRemove(entry,field)
if not yn("Do you want to remove the field '" ..field.attrs.name.. "'?") then return end
if not yn("field_remove","Do you want to remove the field '" ..field.attrs.name.. "'?") then return end
makeEntryBackup(entry.attrs.id)
env.run({"field_remove",entry.attrs.id,field.attrs.name})
env.run({"save"})
end
local function fieldImportFile(entry,field)
local fpath = input("File path: ")
local fpath = input("field_import_file","File path: ")
local fh = io.open(fpath,"r")
if fh == nil then
eprint("Error opening file, make sure to remove any \" or '.")
@ -118,7 +122,7 @@ local function fieldImportFile(entry,field)
end
local function fieldExportFile(entry,field)
local fpath = input("File path: ")
local fpath = input("field_export_file","File path: ")
local fh = io.open(fpath,"r")
if fh ~= nil then
fh:close()
@ -142,7 +146,7 @@ local function fieldExportFile(entry,field)
end
local function fieldRename(entry,field)
local fieldName = input("New field name: ")
local fieldName = input("field_rename","New field name: ")
makeEntryBackup(entry.attrs.id)
env.run({"field_rename",entry.attrs.id,field.attrs.name,fieldName})
env.run({"save"})
@ -150,6 +154,7 @@ end
local function fieldActions(entry,field)
emenu(
"field_action",
"Field '" ..field.attrs.name.. "':",
{
"Print value + exit",
@ -183,41 +188,40 @@ local function fieldsActions(entry)
fields[i] = field.attrs.name;
table.insert(funcs,function() fieldActions(entry,field) end)
end
emenu("Fields:",fields,funcs)
emenu("field_select","Fields:",fields,funcs)
end
local function fieldAdd(entry)
local fieldName = input("Field name: ")
local fieldName = input("field_add","Field name: ")
makeEntryBackup(entry.attrs.id)
env.run({"field_set",entry.attrs.id,fieldName,""})
env.run({"save"})
end
local function entryRename(entry)
local entryName = input("New entry name: ")
local entryName = input("entry_rename","New entry name: ")
makeEntryBackup(entry.attrs.id)
env.run({"entry_rename",entry.attrs.id,entryName})
env.run({"save"})
end
local function entryRemove(entry)
if not yn("Do you want to remove the entry '" ..entry.attrs.name.. "'?") then return end
if not yn("entry_remove","Do you want to remove the entry '" ..entry.attrs.name.. "'?") then return end
env.run({"entry_remove",entry.attrs.id})
env.run({"save"})
end
local function manageEntry(entry)
emenu(
"manage_entry",
"Entry - " ..entry.attrs.name.. ":",
{
"Print password + exit",
"Field add",
"Fields",
"Entry rename",
"Entry remove"
},
{
function() printPassword(entry); os.exit(0) end,
function() fieldAdd(entry) end,
function() fieldsActions(entry) end,
function() entryRename(entry) end,
@ -229,7 +233,7 @@ end
local function searchByName()
local entries = {}
local entriesMenu = {}
local name = string.lower(input("Name: "))
local name = string.lower(input("search_by_name","Name: "))
for i,entry in pairs(env.xmlFindTags(env.db,"entries")[1].children) do
if string.find(string.lower(entry.attrs.name),name) ~= nil then
@ -246,16 +250,12 @@ local function searchByName()
table.insert(entriesMenu,output)
end
end
emenu("Found entries:",entriesMenu,entries)
emenu("entry_select","Found entries:",entriesMenu,entries)
end
local function main(arg)
package.path = basepath .. "/lib/?.lua;" .. basepath .. "/lib/?/main.lua;" .. package.path
require("prpw")
env.cli = false
env.run({"open",arg[1]})
local function search()
emenu(
"search_entry_by",
"Search entry by ...",
{
"Name",
@ -268,6 +268,31 @@ local function main(arg)
searchByFields, -- todo
searchByField, -- todo
listAll -- todo
}
)
end
local function main(arg)
package.path = basepath .. "/lib/?.lua;" .. basepath .. "/lib/?/main.lua;" .. package.path
require("prpw")
env.cli = false
env.run({"open",arg[1]})
if flag_copyonly then
search()
return
end
emenu(
"entries_action",
"Action:",
{
"Search entry",
"Create entry"
},
{
search,
createEntry -- todo
},
"<- EXIT"
)