Skip to content

Commit 955243d

Browse files
committed
making bad socket table raise an error; also small style tweaks
1 parent cd1f7b8 commit 955243d

File tree

2 files changed

+12
-17
lines changed

2 files changed

+12
-17
lines changed

‎README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ idea is that the shell library connects to the unix domain socket of sockproc da
1111
sends the command along with any input data that the child program is expecting, and then
1212
reads back the exit code, output stream data, and error stream data of
1313
the child process. Because we use co-socket API, provided by
14-
[lua-nginx-module](https://github.com/chaoslawful/lua-nginx-module),
14+
[lua-nginx-module](https://github.com/openresty/lua-nginx-module),
1515
the nginx worker is never blocked.
1616

1717
More info on sockproc server, including complete source code here:
@@ -30,7 +30,6 @@ In your OpenResty config:
3030
content_by_lua '
3131
local shell = require("resty.shell")
3232

33-
3433
-- define a table to hold arguments with the following elements:
3534
--
3635
-- timeout: timeout for the socket connection

‎lib/resty/shell.lua

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,30 @@ local tonumber = tonumber
99

1010

1111
local shell = {
12-
_VERSION = '0.01'
12+
_VERSION = '0.02'
1313
}
1414

1515
local default_socket = "unix:/tmp/shell.sock"
1616

17+
1718
function shell.execute(cmd, args)
1819
local timeout = args and args.timeout
1920
local input_data = args and args.data or ""
2021
local socket = args and args.socket or default_socket
2122

2223
local is_tcp
23-
24-
if (type(socket) == 'table') then
25-
if (socket.host and socket.port) then
26-
is_tcp = true
27-
else
28-
return -3, nil, 'invalid socket table options passed'
29-
end
30-
elseif (type(socket) == 'string') then
31-
is_tcp = false
32-
else
33-
return -3, nil, 'socket was not a table with tcp options or a string'
34-
end
24+
if type(socket) == 'table' then
25+
if socket.host and tonumber(socket.port) then
26+
is_tcp = true
27+
else
28+
error('socket table must have host and port keys')
29+
end
30+
end
3531

3632
local sock = tcp()
3733
local ok, err
38-
if (is_tcp) then
39-
ok, err = sock:connect(socket.host, socket.port)
34+
if is_tcp then
35+
ok, err = sock:connect(socket.host, tonumber(socket.port))
4036
else
4137
ok, err = sock:connect(socket)
4238
end

0 commit comments

Comments
 (0)