Skip to content

Commit fa7d589

Browse files
authored
Merge pull request jprjr#6 from olueiro/patch-1
add stdout and stderr callbacks (detached) on exec
2 parents 6d524bc + e2af4a5 commit fa7d589

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

lib/resty/exec.lua

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ function _M.new(address)
4242

4343
local buffer = ""
4444
local ret = {stdout = "", stderr = "", exitcode = "", termsig = ""}
45+
46+
-- detach callbacks from self
47+
local det_stdout, det_stderr
4548

4649
if #args > 0 then
4750
if type(args[1]) == "table" then
@@ -54,6 +57,23 @@ function _M.new(address)
5457
end
5558
if args[1].stdin then self.stdin = args[1].stdin end
5659
if args[1].timeout_fatal then self.timeout_fatal = args[1].timeout_fatal end
60+
61+
if args[1].stdout then
62+
if type(args[1].stdout) == "function" then
63+
det_stdout = args[1].stdout
64+
else
65+
return nil, "invalid argument 'stdout' (requires function)"
66+
end
67+
end
68+
69+
if args[1].stderr then
70+
if type(args[1].stderr) == "function" then
71+
det_stderr = args[1].stderr
72+
else
73+
return nil, "invalid argument 'stderr' (requires function)"
74+
end
75+
end
76+
5777
else
5878
self.argv = args
5979
end
@@ -112,13 +132,17 @@ function _M.new(address)
112132
if not curfield then curfield = v
113133
else
114134
if curfield == "stdout" then
115-
if self.stdout then
135+
if det_stdout then
136+
det_stdout(v)
137+
elseif self.stdout then
116138
self.stdout(v)
117139
else
118140
ret.stdout = ret.stdout .. v
119141
end
120142
elseif curfield == "stderr" then
121-
if self.stderr then
143+
if det_stderr then
144+
det_stderr(v)
145+
elseif self.stderr then
122146
self.stderr(v)
123147
else
124148
ret.stderr = ret.stderr .. v

0 commit comments

Comments
 (0)