mirror of
https://github.com/octoleo/lsyncd.git
synced 2024-12-04 19:03:17 +00:00
Add function to substitude commands with placeholders
This commit is contained in:
parent
3d2288dccf
commit
c543a21162
24
lsyncd.lua
24
lsyncd.lua
@ -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.
|
||||
--
|
||||
|
@ -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)
|
Loading…
Reference in New Issue
Block a user