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