Add function to substitude commands with placeholders

This commit is contained in:
Daniel Poelzleithner 2022-03-16 17:38:03 +01:00
parent 3d2288dccf
commit c543a21162
2 changed files with 37 additions and 0 deletions

View File

@ -3983,6 +3983,30 @@ function splitQuotedString
return rv
end
function replaceCommand(cmd, data)
assert(type(data) == "table")
local getData = function(arg)
print(arg, data)
local rv = data[arg]
if rv ~= nil then
return rv
else
return ""
end
end
if type(cmd) == "string" then
return string.gsub(cmd, "%${(%w+)}", getData)
elseif type(cmd) == "table" then
local rv = {}
for i, v in ipairs(cmd) do
rv[i] = string.gsub(v, "%${(%w+)}", getData)
end
return rv
else
log("Error", "Unsupported type in replacCommand")
end
end
--
-- Interface to inotify.
--

View File

@ -9,4 +9,17 @@ assert(isTableEqual(
{"-p", "22", "-i", "/home/test/bla blu/id_rsa"}
))
-- test string replacement
local testData = {
localPort = 1234,
localHost = "localhorst"
}
assert(replaceCommand("echo ssh ${localHost}:${localPort}", testData) ==
"echo ssh localhorst:1234")
assert(isTableEqual(
replaceCommand({"-p${doesNotExist}", "2${localHost}2", "-i '${localPort}'"}, testData),
{"-p", "2localhorst2", "-i '1234'"}
))
os.exit(0)